JSON to .xls: Unable to match format and extension - javascript

I modified this code to convert JSON to .xls format. The code actually works, but while opening the file in MS Excel 2013, it throws a warning that the file format and extension do not match.
This is what I have so far:
var json3 = { "d": "[{\"Id\":1,\"UserName\":\"Sam Smith\"},{\"Id\":2,\"UserName\":\"Fred Frankly\"},{\"Id\":3,\"UserName\":\"Zachary Zupers\"}]" }
DownloadJSON2CSV(json3.d);
function DownloadJSON2CSV(objArray)
{
var array = typeof objArray != 'object' ? JSON.parse(objArray) : objArray;
var str = '';
for (var i = 0; i < array.length; i++) {
var line = '';
for (var index in array[i]) {
line += array[i][index] + '\t';
}
line.slice(0,line.Length-1);
str += line + '\r\n';
}
window.open( "data:application/vnd.ms-excel;charset=utf-8," + escape(str));
}
What am I missing?
jsFiddle

The .xls format is a far more complex (and for that matter, proprietary) file format - the modification you made only modifies the mime type, not the actual content of the file. In other words, the file is still a CSV file inside, but you just tricked your browser into thinking it's an XLS file.
More info on mime types: http://en.wikipedia.org/wiki/Internet_media_type
For a solution to your problem, if you really, REALLY need XLS, the best idea is to find an online service that offers an API which converts CSV to XLS (googling "CSV to XLS online" might help).

Related

How can I get the file encoding format with Js

When I use readAsText(file,'utf-8'), I ran into a problem. Because I use the utf8 format to parse the file, when the user file is not in utf8 encoding format, the parsing result is likely to be wrong. How can I use Js to get the encoding format of the user file to remind the user to upload the utf8 file? Under Windows, I judge whether the first 3 bytes of the file are BOM, but Mac will not add BOM to utf8 files, so can anyone help? thank you very much
const encodeReader = new FileReader();
encodeReader.onload = function (e) {
const view = new Uint8Array(e.target.result as ArrayBuffer);
let zwsp = "";
for (const num of view) {
zwsp += num.toString(16);
}
if (zwsp.toUpperCase() === "EFBBBF") {
console.log("it is utf-8");
}
};
encodeReader.readAsArrayBuffer(file.slice(0, 3));

NodeJS : smart JSON conversion to Excel file

I'm working in NodeJS and I would like to export a JSON-format object to an Excel file.
I am well aware that there are (at least) three npm packages for that purpose, but so far none of these gave me the output I'm dreaming of.
Here is the javascript object I have :
var myObject =
{
hashkey1 : {
keyA : dataA1,
keyB : dataB2
}
hashkey2 : {
keyA : dataA2,
keyB : dataB2
}
};
The .xls (or .xlsx)(or any spreadsheet format) of my dreams has one line for each hashkey. On each line : first column would be the hashkeyX, second column would be the dataAX, third column would be the dataBX.
Is it possible to achieve such a result using available tools, or do I have to code it from scratch ? Any advice to get anywhere near this result ?
You can write to csv (comma-separated values) text file without any additional library. This extension open in Excel by default.
var fs = require('fs');
var file = fs.createWriteStream('file.csv', {'flags': 'w', autoClose: true});
var result = '';
for (var hashkey in myObject)
result += hashkey + ';' + myObject[hashkey].keyA + ';' + myObject[hashkey].keyB + '\n';
file.write(result);

Json to excel file convert using javascript

Please find the following code for more information.
function dataToCSVTry(arr) {
var fileName = "CSVFile";
var data = "";
for (var i = 0; i < arr.length; i++) {
data += (arr[i].id + " , " + arr[i].time + "\r\n");
}
var url = 'data:text/csv;charset=utf8,' + encodeURI(data);
window.open(url, '_blank');
window.download = (url + ".txt");
var encodedUri = encodeURI(url);
var link = document.createElement("a");
link.setAttribute("href", encodedUri);
link.setAttribute("download", "my_data.csv");
link.click();
};
`
This is a function to which I'm providing JSON data as input and after that trying to convert it in to a CSV(Comma Separated Values) using a for loop.
After that i am trying to save it in both .txt and .csv format. As .txt is getting saved easily, the problem comes in excel file where the data comes like :
"1%20%2C%20161.963%0A%0D%0A2%20%2C%20473.222%0A%0D%0A3%20%2C%20error%0A%0D%0A"
where some code (from what I think) is for blank space("%20%2C%20") and some other code("%0A%0D%0A") is for newline characters. What needs to be done in order to create Excel file in the same CSV format? Is there any problem with the encodeURI part that I am using ?
Data url content isn't URI encoded, it is base-64 encoded. You should use btoa or a similar solution to create your data from the CSV string you built.

Open .csv file in MS Excel gives wrong Utf-8 formatting (Javascript)

I have been searching forever for an answer for this and now I think I have ended up in an infinite loop of googling.
I am using javascript to export data to a .csv file. No issues here, it opens flawless in Apple's Numbers & Google spreadsheet. Unfortunately for me my customer is using MS Excel on both Mac & Windows and is, to say the least, not very tech savvy. So using the "import" option in Excel is to really push their boundaries. Although it gives the correct formatting after selecting the right options.
So, the problem is: .csv file is not opening with correct formatting in Excel when double clicking or choosing "open with..". "Ö" is being displayed as "ö" for example. I have come across a lot of answers suggesting adding a BOM to the file, however I haven't found the correct way of doing this. Is this a string value I put in the first cell, or is this something that needs to be in the javascript code while writing the file?
I am using "," as separator in the .csv file.
Any help that is deriving to the solution of this issue will result in eternal gratitude and appreciation.
The application is a iOS and Android written in javascript in Appcelerator Titanium. The code for writing the .csv file is as follows, where "input" is an array:
exports.exportCsvData = function(input)
{
var rowTxt = "";
for(var i=0;i < input.length; i++){ // row iteration
for(var j = 0; j < input[i].length; j++){ // column iteration
rowTxt += '"' + input[i][j] + '"';
if(j < (input[i].length-1))
{
rowTxt += ',';
}
}
rowTxt += '\n';// adding new line at end of row
}
// creating output file in application data directory
var outputFile = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory,'min_traningsdagbok.txt');
// writing data in output file
outputFile.write(rowTxt);
if(outputFile.exists){
Ti.API.info("CSV generated!!!");
}
// return output file path
return outputFile.nativePath;
};
For anyone ending up here for the same reason:
Needless to say there is a huge issue regarding Utf-8 formatting in CSV files on different spreedsheet clients. Especially MS Excell. My solution was to give up the CSV file and output a .html file, containing a html table. This opens correctly on both Windows and Mac MS Excel.
var htmlHead = '<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> </head> <body><table style="width:300px"><table border="1" cellspacing="1" cellpadding="5">';
var str = "";
str += htmlHead;
for(var i = 0; i < input.length; i++){
str += '<tr>';
for (var j = 0; j < input[i].length; j++){
str += '<td>' + input[i][j] + '</td>';
}
str += '</tr>';
}
str += '</table>';
var htmlEnd = '</body> </html>';
outputFileHtml.write(htmlHead+str+htmlEnd);

Saving binary data in a browser without it getting UTF8 encoded on download

My web app receives data in the form of a base64 encoded string, which is decodes using atob, and stores via URL.createObjectURL(). This data is then downloaded via the right-click save-as dialog. The downloaded filed always matches the source file when the source file is ascii encoded. However this isn't the case when the source file is just plain binary data. A diff of a non ascii encoded downloaded file vs its source file appears to show that the downloaded file is UTF-8 encoded. How can this problem be fixed? Please note, I'm locked into using firefox 10.
Convert the string to a Arraybuffer and it should work. If there is any way that you can get the data into an array buffer directly without passing a sting that would be the best solution.
The following code is tested in FF10, and are using the now obsolete MozBlobBuilder.
fiddle
var str="",
idx, len,
buf, view, blobbuild, blob, url,
elem;
// create a test string
for (var idx = 0; idx < 256; ++idx) {
str += String.fromCharCode(idx);
}
// create a buffer
buf = new ArrayBuffer(str.length);
view = new Uint8Array(buf);
// convert string to buffer
for (idx = 0, len = str.length; idx < len; ++idx) {
view[idx] = str.charCodeAt(idx);
}
blobbuild = new MozBlobBuilder();
blobbuild.append(buf);
blob = blobbuild.getBlob('application/octet-stream');
url = URL.createObjectURL(blob);
elem = document.createElement('a');
elem.href = url;
elem.textContent = 'Test';
document.body.appendChild(elem);

Categories