I am making an electron app (using Node Js) where I need to export HTML table data to a .csv file. I followed this tutorial to implement this. The function I used is as follows.
$(document).ready(function () {
function exportTableToCSV($table, filename) {
var $rows = $table.find('tr:has(td)'),
// Temporary delimiter characters unlikely to be typed by keyboard
// This is to avoid accidentally splitting the actual contents
tmpColDelim = String.fromCharCode(11), // vertical tab character
tmpRowDelim = String.fromCharCode(0), // null character
// actual delimiter characters for CSV format
colDelim = '","',
rowDelim = '"\r\n"',
// Grab text from table into CSV formatted string
csv = '"' + $rows.map(function (i, row) {
var $row = $(row),
$cols = $row.find('td');
return $cols.map(function (j, col) {
var $col = $(col),
text = $col.text();
return text.replace(/"/g, '""'); // escape double quotes
}).get().join(tmpColDelim);
}).get().join(tmpRowDelim)
.split(tmpRowDelim).join(rowDelim)
.split(tmpColDelim).join(colDelim) + '"';
// Deliberate 'false', see comment below
if (false && window.navigator.msSaveBlob) {
var blob = new Blob([decodeURIComponent(csv)], {
type: 'text/csv;charset=utf8'
});
window.navigator.msSaveBlob(blob, filename);
} else if (window.Blob && window.URL) {
// HTML5 Blob
var blob = new Blob([csv], { type: 'text/csv;charset=utf8' });
var csvUrl = URL.createObjectURL(blob);
$(this).attr({
'download': filename,
'href': csvUrl
});
} else {
// Data URI
var csvData = 'data:application/csv;charset=utf-8,' + encodeURIComponent(csv);
$(this).attr({
'download': filename,
'href': csvData,
'target': '_blank'
});
}
}
// This must be a hyperlink
$(".export").on('click', function (event) {
// CSV
var args = [$('#dvData>table'), 'export.csv'];
exportTableToCSV.apply(this, args);
// If CSV, don't do event.preventDefault() or return false
// We actually need this to be a typical hyperlink
});
});
For the output csv file, I have given a default name "export.csv".
The problem that I am facing is as follows.
When I run this electron app in Windows (I have used electron-packager to package it, Ubuntu being my host computer),
In the Windows dialog box that appears asking me where to save the document

If I don't change the default name "export.csv" present in the "File name" field, I get a valid export.csv file as the output. A screenshot is attached below for reference

But If I change the name present in the "File name" field to a different name (ex : temp), I don't get a .csv file . Instead I get a file as shown in the image below.

Can someone please tell me how can I get a valid .csv file as my output file even if the user changes the output "File Name"?
You haven't shown the code you're using to display the Windows dialog box, but I think the simplest thing to do is to make it default to a .csv extension.
You can do this by setting the filters in your call to showSaveDialog.
For example:
var filename = dialog.showSaveDialog({
filters: [
{ name: 'CSV files', extensions: ['csv'] }
]
});
That way, when the user enters their own filename, the result will default to a .csv extension.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With