Published 2005-03-27 11:54:49
<workbook filename="test.xls">The page then uses javascript to load this template (xmlhttprequest etc.) and starts adding <cell elements to represent the data.)
<format name="greyhead"
Align="center"
Bold="1"
FgColor="silver"
Size="10"
/>
<format name="dottedDate"
NumFormat='dd"."mm"."yy' />
<format name="percent"
NumFormat="0.00%" />
<format name="dollarvalue"
NumFormat='$"#,##0.00_);[Red]("$"#,##0.00)' />
<worksheet name="sheet1" active="true">
<column firstcol="0" lastcol="0" width="25"/>
<column firstcol="1" lastcol="40" width="15" />
</worksheet>
</workbook>
<script type="application/x-javascript">
function download_as_excel()
{
var form = document.getElementById('_post');
if (!form) {
alert('not loaded yet');
return;
}
var wbq = new phpRequest(rooturl +
'/FlexyShipping/templates/spreadsheets/base_excel.xml');
var wbd = wbq.getXML();
var ws = wbd.getElementsByTagName('worksheet')[0];
var wb = wbd.getElementsByTagName('workbook')[0];
wb.setAttribute('filename','summary-'+
document.getElementById('date').childNodes[0].nodeValue +'.xls');
ws.setAttribute('name',
document.getElementById('title').childNodes[0].nodeValue);
var cell = wbd.createElement('cell');
cell.setAttribute('row',0);
cell.setAttribute('col',0);
var cell_value = wbd.createTextNode(
'Summary for ' +
document.getElementById('title').childNodes[0].nodeValue +
' as of ' + document.getElementById('date').childNodes[0].nodeValue
);
cell.appendChild(cell_value);
ws.appendChild(cell);
// now the rows..
// <cell col="A" row="1">Test< / cell>
// <cell col="B" row="2" type="Number" format="test1">30< / cell>
var rows = document.getElementById('datagrid').getElementsByTagName('tr');
//alert(rows.length);
for(var row=0;row<rows.length;row++) {
cols = rows[row].getElementsByTagName('td');
for(var col=0;col < cols.length; col++) {
if (!cols[col].childNodes.length) {
continue;
}
// skip blannk cells
if (!cols[col].childNodes[0].nodeValue.
replace(/^\s*|\s*$/g,"").length) {
continue;
}
var cell = wbd.createElement('cell');
cell.setAttribute('row',row+1);
cell.setAttribute('col',col);
//fixme
if (cols[col].getAttribute('xls:type')) {
cell.setAttribute('type',
cols[col].getAttribute('xls:type'));
}
if (cols[col].getAttribute('xls:format')) {
cell.setAttribute('format',
cols[col].getAttribute('xls:format'));
}
var cell_value = wbd.createTextNode(
cols[col].childNodes[0].nodeValue);
if (cols[col].getAttribute('xls:formula')) {
var s = cols[col].getAttribute('xls:formula');
s = s.replace(/#row#/g,(row + 2 ));
//alert(s);return;
cell_value.nodeValue = s;
}
if (cols[col].getAttribute('xls:percent')) {
cell_value.nodeValue = 0.01 * cell_value.nodeValue;
}
cell.appendChild(cell_value);
ws.appendChild(cell);
}
}
var ser = new XMLSerializer();
var str = ser.serializeToString(wbd);
//alert(str);
document.getElementById('_xml').setAttribute('value',str);
form.submit();
}
</script>
<table id="datagrid">The final step of posting it to a url, that runs the library
<!-- header row -->
<tr class="head">
<td xls:format="greyhead">Customer</td>
<td xls:format="greyhead">Order Date</td>
<td xls:format="greyhead">delivery</td>
<td xls:format="greyhead">Last ETD</td>
<td xls:format="greyhead">Days Late</td>
<td xls:format="greyhead">Supplier</td>
<td xls:format="greyhead">Item</td>
<td xls:format="greyhead">Qty</td>
<td xls:format="greyhead">Del. Perf.</td>
<td xls:format="greyhead">Ord. Ammount</td>
<td xls:format="greyhead">Order Num</td>
</tr>
<tr flexy:foreach="results,r,row">
<td>{row.customer_name}</td>
<td xls:type="Date" xls:format="dottedDate">{row.ordered_date}</td>
<td xls:type="Date" xls:format="dottedDate">{row.delivery_date}</td>
<td xls:type="Date" xls:format="dottedDate">{row.getMaxShipDate()}</td>
<td xls:format="numbervalue" xls:type="Number">{row.shipmentLateDays}</td>
<td >{row.supplier_name}</td>
<td>{row.getFirstProductName()}</td>
<td xls:format="numbervalue" xls:type="Number">{row.getTotalQty()}</td>
<td> </td>
<td xls:format="dollarvalue" xls:type="Number">{row.getTotalBuyCost()}</td>
<td>{row.order_number}</td>
</tr>
</table>