/    Sign up×
Community /Pin to ProfileBookmark

Exporting to Excel with Color, Style

I have two scripts, one successfully exports to xlsx with color and style for the rows and columns with both class and in line css. I have a more sophisticated script that exports multiple sheets, that I want to apply style to. This second one is not working though. I have a feeling the issue lies in the javascript, particularly the template portions, I cant see what would be the difference otherwise. Here is the one that works:

“`
<script src=”https://code.jquery.com/jquery-latest.min.js” type=”text/javascript”></script>
<script type=”text/javascript”>
var tableToExcel = (function () {
// Define your style class template.
var style = “<style>.green { background-color: green; }</style>”;
var uri = ‘data:application/vnd.ms-excel;base64,’
, template = ‘<html xmlns:o=”urn:schemas-microsoft-com:office:office” xmlns:x=”urn:schemas-microsoft-com:office:excel” xmlns=”http://www.w3.org/TR/REC-html40″><head><!–[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]–>’ + style + ‘</head><body><table>{table}</table></body></html>’
, base64 = function (s) {
return window.btoa(unescape(encodeURIComponent(s)))
}
, format = function (s, c) {
return s.replace(/{(w+)}/g, function (m, p) { return c[p]; })
}
return function (table, name) {
if (!table.nodeType) table = document.getElementById(table)
var ctx = { worksheet: name || ‘Worksheet’, table: table.innerHTML }
window.location.href = uri + base64(format(template, ctx))
}
})()
</script>
<style type=”text/css”>
.green
{
background-color: green;
}
</style>
<input type=”button” onclick=”tableToExcel(‘testTable’, ‘W3C Example Table’, ‘testTable2’, ‘This is a test’)” value=”Export to Excel” />
<table id=”testTable” summary=”Code page support in different versions of MS Windows.”
rules=”groups” frame=”hsides” border=”2″>
<caption>
CODE-PAGE SUPPORT IN MICROSOFT WINDOWS
</caption>
<colgroup align=”center”></colgroup>
<colgroup align=”left”></colgroup>
<colgroup span=”2″ align=”center”></colgroup>
<colgroup span=”3″ align=”center”></colgroup>
<thead valign=”top”>
<tr>
<th>Code-Page<br />ID</th>
<th>Name</th>
<th>ACP</th>
<th>OEMCP</th>
<th>Windows<br />NT 3.1</th>
<th>Windows<br />NT 3.51</th>
<th>Windows<br />95</th>
</tr>
</thead>
<tbody>
<tr>
<td>1200</td>
<td style=”background-color: #00f; color: #fff”>Unicode (BMP of ISO/IEC-10646)</td>
<td></td>
<td></td>
<td>X</td>
<td>X</td>
<td>*</td>
</tr>
“`

Here is the script for the one that doesnt, no errors, just wont publish the styles on the same html elements:

Head:

“`
<script>
var tablesToExcel = (function() {
var style = “<style>.green { background-color: green; }</style>”;
var uri = ‘data:application/vnd.ms-excel;base64,’

, tmplWorkbookXML = ‘<?xml version=”1.0″?><?mso-application progid=”Excel.Sheet”?><Workbook xmlns=”urn:schemas-microsoft-com:office:spreadsheet” xmlns:ss=”urn:schemas-microsoft-com:office:spreadsheet”>’
+ ‘<DocumentProperties xmlns=”urn:schemas-microsoft-com:office:office”><Author>Seecuring</Author><Created>{created}</Created></DocumentProperties>’
+ ‘<Styles>’
+ ‘<Style ss:ID=”Currency”><NumberFormat ss:Format=”Currency”></NumberFormat></Style>’
+ ‘<Style ss:ID=”Date”><NumberFormat ss:Format=”Medium Date”></NumberFormat></Style>’
+ ‘</Styles>’
+ ‘{worksheets}</Workbook>’
, tmplWorksheetXML = ‘<Worksheet ss:Name=”{nameWS}”><Table>{rows}</Table></Worksheet>’
, tmplCellXML = ‘<Cell{attributeStyleID}{attributeFormula}><Data ss:Type=”{nameType}”>{data}</Data></Cell>’
, base64 = function(s) { return window.btoa(unescape(encodeURIComponent(s))) }
, format = function(s, c) { return s.replace(/{(w+)}/g, function(m, p) { return c[p]; }) }
return function(tables, wsnames, wbname, appname) {
var ctx = “”;
var workbookXML = “”;
var worksheetsXML = “”;
var rowsXML = “”;

for (var i = 0; i < tables.length; i++) {
if (!tables[i].nodeType) tables[i] = document.getElementById(tables[i]);
for (var j = 0; j < tables[i].rows.length; j++) {

rowsXML += ‘<Row>’
for (var k = 0; k < tables[i].rows[j].cells.length; k++) {
var dataType = tables[i].rows[j].cells[k].getAttribute(“data-type”);
var dataStyle = tables[i].rows[j].cells[k].getAttribute(“data-style”);
var dataValue = tables[i].rows[j].cells[k].getAttribute(“data-value”);
dataValue = (dataValue)?dataValue:tables[i].rows[j].cells[k].innerHTML;
var dataFormula = tables[i].rows[j].cells[k].getAttribute(“data-formula”);
dataFormula = (dataFormula)?dataFormula:(appname==’Calc’ && dataType==’DateTime’)?dataValue:null;
ctx = { attributeStyleID: (dataStyle==’Currency’ || dataStyle==’Date’)?’ ss:StyleID=”‘+dataStyle+'”‘:”
, nameType: (dataType==’Number’ || dataType==’DateTime’ || dataType==’Boolean’ || dataType==’Error’)?dataType:’String’
, data: (dataFormula)?”:dataValue
, attributeFormula: (dataFormula)?’ ss:Formula=”‘+dataFormula+'”‘:”
};
rowsXML += format(tmplCellXML, ctx);
}
rowsXML += ‘</Row>’
}
ctx = {rows: rowsXML, nameWS: wsnames[i] || ‘Sheet’ + i};
worksheetsXML += format(tmplWorksheetXML, ctx);
rowsXML = “”;
}

ctx = {created: (new Date()).getTime(), worksheets: worksheetsXML};
workbookXML = format(tmplWorkbookXML, ctx);

console.log(workbookXML);

var link = document.createElement(“A”);
link.href = uri + base64(workbookXML);
link.download = wbname || ‘Oracle Cloud Snapshot Management.xls’;
link.target = ‘_blank’;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);

}
})();

</script>
<style type=”text/css”>
.green
{
background-color: #0F51F6;
}
</style>
</head>
The html:

<table id=”intro” class=”table2excel” rules=”groups” frame=”hsides” border=”2″>
<tr>
<td><!–<img src='<?php //echo $environment; ?>/access/images/erpra.png’ width=’100px’ height=’100px’> –></td>

</tr>
<tr>
<td class=”green”>KPMG – Oracle Cloud ERP/HCM Configuration Management Assessment
<td>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td style=”background-color: #00f; color: #fff”>A review of key Configuration elements within the Oracle Cloud Applications/Fusion suite. <?php if(!isset($_POST[‘no_date’])&& $_POST[‘no_date’]== “Y”){
$CREATION_DATE_POST_DISPLAY_RAW = $_POST[‘creation_from’];
$CREATION_DATE_DISPLAY=date(“m/d/Y”,strtotime($CREATION_DATE_POST_DISPLAY_RAW));

$LAST_UPDATE_DATE_POST_DISPLAY_RAW = $_POST[‘update_from’];
$LAST_UPDATE_DATE_DISPLAY=date(“m/d/Y”,strtotime($LAST_UPDATE_DATE_POST_DISPLAY_RAW));
echo “Creation Date from : $CREATION_DATE_DISPLAY, Last Updated Date from: $LAST_UPDATE_DATE_DISPLAY”;

}
?></td>
“`

Thanks for reading!

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@natelikMay 31.2020 — Writing out html, and letting excel figure out how to display it will never give you full control over the output. I always prefer this to the RenderControl approach.

**Links removed by Site Administrator so it doesn't look like you're spamming us. Please don't post them again.**
×

Success!

Help @genista40 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 3.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,

tipper: Anonymous,
tipped: article
amount: 10 SATS,
)...