我有一个包含三个表的小型HTML表单。每个表都有选择标记。我的目标是将HTML表导出到excel文件中,并且只导出选定的值。在excel文件中,每个表都应该有自己的选项卡。
我唯一的问题是所选值的问题。当我将其导出到excel文件时,所有的选项值都会导出,并且我希望只导出选定的值。
下面是我的代码:
<!-- 1 -->
<table id="table1">
<p>Table 1</p><hr>
<tr><td>Question 1:</td>
<td><select>
<option value="0">0</option>
<option value="1">1</option>
</select></td></tr>
<tr><td>Question 2:</td>
<td><select>
<option value="0">0</option>
<option value="1">1</option>
</select></td></tr>
</table>
<!-- 2 -->
<p>Table 2</p><hr>
<table id="table2">
<tr><td>Question 3:</td>
<td><select>
<option value="0">0</option>
<option value="1">1</option>
</select></td></tr>
<tr><td>Question 4:</td>
<td><select>
<option value="0">0</option>
<option value="1">1</option>
</select></td></tr>
</table>
<!-- 3 -->
<p>Table 3</p><hr>
<table id="table3">
<tr><td>Question 5:</td>
<td><select>
<option value="0">0</option>
<option value="1">1</option>
</select></td></tr>
<tr><td>Question 6:</td>
<td><select>
<option value="0">0</option>
<option value="1">1</option>
</select></td></tr>
</table>
<button onclick="tablesToExcel(['table1','table2','table3'], ['Summary','SectionA', 'SectionB'], 'TestBook.xls', 'Excel')">Export to Excel</button>
<script>
var tablesToExcel = (function() {
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>Axel Richter</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 || 'Workbook.xls';
link.target = '_blank';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
var rows = document.querySelectorAll("table tr");
})();
</script>发布于 2019-12-17 00:27:38
出现这个问题的原因是:tables[i].rows[j].cells[k].innerHTML是单元格的全部内容,也就是整个选择元素(其中k= 1),而不仅仅是所选值。
HTML Select元素的选定值可以通过e.options[e.selectedIndex].value获得,其中e是您选择的元素。
通常,可以使用parent.getElementsByTagName('select')[0]或通过为每个选择分配一个Id并使用document.getElementById('YourAssignedId')从父元素parent中提取第一个选择元素parent.getElementsByTagName('select')[0],当然,您将需要处理单元格中没有选择元素的情况,就像k= 0时的情况一样。
https://stackoverflow.com/questions/59359873
复制相似问题