Skip to content

Instantly share code, notes, and snippets.

@emamut
Last active September 6, 2021 09:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emamut/11182654 to your computer and use it in GitHub Desktop.
Save emamut/11182654 to your computer and use it in GitHub Desktop.
Export JSON array to MS Excel
<script type='text/javascript' src='script.js'></script>
<a id="test" href="">Test.xls</a>
// Test script to generate a file from JavaScript such
// that MS Excel will honor non-ASCII characters.
testJson = [
{
"name": "Tony Peña",
"city": "New York",
"country": "United States",
"birthdate": "1978-03-15",
"amount": 42
},
{
"name": "Ζαλώνης Thessaloniki",
"city": "Athens",
"country": "Greece",
"birthdate": "1987-11-23",
"amount": 42
}
];
// Simple type mapping; dates can be hard
// and I would prefer to simply use `datevalue`
// ... you could even add the formula in here.
testTypes = {
"name": "String",
"city": "String",
"country": "String",
"birthdate": "String",
"amount": "Number"
};
emitXmlHeader = function () {
return '<?xml version="1.0"?>\n' +
'<ss:Workbook xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">\n' +
'<ss:Worksheet ss:Name="Sheet1">\n' +
'<ss:Table>\n\n';
};
emitXmlFooter = function() {
return '\n</ss:Table>\n' +
'</ss:Worksheet>\n' +
'</ss:Workbook>\n';
};
jsonToSsXml = function (jsonObject, testTypes) {
var row;
var col;
var xml;
var data = typeof jsonObject != "object"
? JSON.parse(jsonObject)
: jsonObject;
var data_header = typeof testTypes != "object"
? JSON.parse(testTypes)
: testTypes;
xml = emitXmlHeader();
xml += '<ss:Row>\n';
$.each(data_header, function(index, value) {
xml += ' <ss:Cell>\n';
xml += ' <ss:Data ss:Type="String">';
xml += index + '</ss:Data>\n';
xml += ' </ss:Cell>\n';
});
xml += '</ss:Row>\n';
for (row = 0; row < data.length; row++) {
xml += '<ss:Row>\n';
for (col in data[row]) {
xml += ' <ss:Cell>\n';
xml += ' <ss:Data ss:Type="' + testTypes[col] + '">';
xml += data[row][col] + '</ss:Data>\n';
xml += ' </ss:Cell>\n';
}
xml += '</ss:Row>\n';
}
xml += emitXmlFooter();
return xml;
};
download = function (content, filename, contentType) {
if (!contentType) contentType = 'application/octet-stream';
var a = document.getElementById('export');
var blob = new Blob([content], {
'type': contentType
});
a.href = window.URL.createObjectURL(blob);
a.download = filename;
};
@KabakiAntony
Copy link

Hey am trying to work with this script and I am having a challenge triggering the script to work for or where could I be going wrong.

@emamut
Copy link
Author

emamut commented Sep 3, 2021

Hey am trying to work with this script and I am having a challenge triggering the script to work for or where could I be going wrong.

Hi, I haven't used this script for a while, could you show me what error it generates?

@KabakiAntony
Copy link

KabakiAntony commented Sep 6, 2021 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment