gpt4 book ai didi

jquery - Javascript - 将 JQuery 数据表导出到 Excel 问题

转载 作者:行者123 更新时间:2023-12-01 08:03:58 25 4
gpt4 key购买 nike

我有以下代码:

<a id="dlink"  style="display:none;"></a>
<input type="button" onclick="tableToExcel('cereriTable', 'Tabel Date', 'myfile.xls')" value="Export to Excel">

<script Language="javascript">
var tableToExcel = (function() {
var uri = 'data:application/vnd.ms-excel;charset=UTF-8;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]>' +

'<xsl:template name="styles">' +
'<style>' +
'table {' +
'mso-displayed-decimal-separator:"\,";' +
'mso-displayed-thousand-separator:" ";' +
'}' +

'@page {' +
'margin:.25in .25in .25in .25in;' +
'mso-header-margin:.15in;' +
'mso-footer-margin:.15in;' +
'mso-page-orientation:landscape;' +
'}' +

'tr {' +
'mso-height-source:auto;' +
'}' +

'col {' +
'mso-width-source:auto;' +
'}' +

'br {' +
'mso-data-placement:same-cell;' +
'}' +

'td {' +
'mso-style-parent:style0;' +
'padding-top:1px;' +
'padding-right:1px;' +
'padding-left:1px;' +
'mso-ignore:padding;' +
'color:windowtext;' +
'font-size:11.0pt;' +
'font-weight:300;' +
'font-style:normal;' +
'text-decoration:none;' +
'font-family:Calibri;' +
'mso-generic-font-family:auto;' +
'mso-font-charset:0;' +
'mso-number-format:General;' +
'text-align:general;' +
'vertical-align:bottom;' +
'border:none;' +
'mso-background-source:auto;' +
'mso-pattern:auto;' +
'mso-protection:locked visible;' +
'white-space:wrap;' +
'mso-rotate:0;' +
'}' +

'.longdate {' +
'mso-style-parent:style0;' +
'mso-number-format:"General Date";' +
'}' +

'.shortdate {' +
'mso-style-parent:style0;' +
'mso-font-charset:0;' +
'mso-number-format:"Short Date";' +
'white-space:normal;' +
'}' +

'.number {' +
'mso-style-parent:style0;' +
'mso-font-charset:0;' +
'mso-number-format:"0";' +
'white-space:normal;' +
'}' +

'.center {' +
'mso-style-parent:style0;' +
'font-weight:700;' +
'text-align:center;' +
'}' +

'.right {' +
'mso-style-parent:style0;' +
'text-align:right;' +
'}' +

'.header {' +
'mso-style-parent:style0;' +
'font-weight:700;' +
'}' +
'</style>' +

'<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]-->' +
'</head>' +
'<body><table>{table}</table></body>' +
'</html>'
, base64 = function(s) {
while (s.indexOf('ş') != -1) s = s.replace('ş','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, filename) {
if (!table.nodeType) table = document.getElementById(table)
var ctx = {worksheet: name || 'Worksheet', table: table.innerHTML}
document.getElementById("dlink").href = uri + base64(format(template, ctx));
document.getElementById("dlink").download = filename;
document.getElementById("dlink").click();
}
})();
</script>

我有一个数据表:

dataTable = $('#cereriTable').dataTable({...

我想要:

1) 从表中删除一列 - 在导出的 Excel 中(最后一列)

2)为每列设置单元格格式(文本/日期/数字),不通用...

3)mso-page-orientation:landscape 不起作用,我需要它......

4) 我在数据表中有多个页面,但这仅导出所选页面上的记录。

5)我想设置每列的宽度...(在导出的 Excel 中)

最佳答案

我是这样解决的:

                   <table id="cereriTable" style="text-align:center">
<thead>
<tr>
<th>Operator economic</th>
<th>Punct de lucru</th>
<th>Cod Fiscal</th>
<th>Judet</th>
<th>Data Introducere</th>
<th>Domeniu Acces</th>
<th>Tip cerere</th>
<th>Status</th>
<th>Username</th>
<th>Detalii</th>
</tr>
</thead>

<a id="dlink" style="display:none;"></a>
<input type="button" onclick="tableToExcel('cereriTable', 'Tabel Date', 'myfile.xls')"
title="If you are using Office 2007, please select 'Yes' from the resulting dialog."
value="Export to Excel">

<script Language="javascript">
var tableToExcel = (function() {
var uri = 'data:application/vnd.ms-excel;charset=UTF-8;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]>' +

'<xsl:template name="styles">' +
'<style>' +
'@page {' +
'margin:.25in .25in .25in .25in;' +
'mso-header-margin:.15in;' +
'mso-footer-margin:.15in;' +
'mso-page-orientation:landscape;' +
'}' +

'tr { mso-height-source:auto; }' +
'col { mso-width-source:auto; }' +
'br { mso-data-placement:same-cell; }' +

'td {' +
'mso-style-parent:style0;' +
'padding-top:1px;' +
'padding-right:1px;' +
'padding-left:1px;' +
'mso-ignore:padding;' +
'color:windowtext;' +
'font-size:11.0pt;' +
'font-weight:300;' +
'font-style:normal;' +
'text-decoration:none;' +
'font-family:Calibri;' +
'mso-generic-font-family:auto;' +
'mso-font-charset:0;' +
'mso-number-format:General;' +
'text-align:general;' +
'vertical-align:bottom;' +
'border:none;' +
'mso-background-source:auto;' +
'mso-pattern:auto;' +
'mso-protection:locked visible;' +
'white-space:wrap;' +
'mso-rotate:0;' +
'}' +

'.header {' +
'mso-style-parent:style0;' +
'font-weight:700;' +
'}' +

'.text {mso-number-format:"@";}' +
'.number {mso-number-format:"0";}' +
'.date {mso-number-format:"dd-mm-yyyy";text-align:left;}' +

'</style>' +

'<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]-->' +
'</head>' +
'<body>' +
'<table>{table}</table>' +
'</body>' +
'</html>'
, base64 = function(s) {
while (s.indexOf('â') != -1) s = s.replace('â','a');
while (s.indexOf('ş') != -1) s = s.replace('ş','s');
while (s.indexOf('ă') != -1) s = s.replace('ă','a');
while (s.indexOf('ţ') != -1) s = s.replace('ţ','t');
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, filename) {
if (!table.nodeType) table = document.getElementById(table)

var output = '';
$("[id=cereriTable]").each(function(){
output += $(this).find("thead").html();//capul de tabel
$('#cereriTable tbody tr').each(function() {
var j=0;
output += '<tr class="">';
$.each(this.cells, function(){ //parcurg celulele de pe rand
j++;
if(j<10)//mai putin coloana de detalii
{
if(j==5)//coloana de data
output += '<td class="date">';
else if(j==3) //coloana de cod fiscal
output += '<td class="number">';
else//restul coloanelor sunt text
output += '<td class="text">';
output += $(this).html();
output += '</td>';
}
});
output += '</tr>';
});
});

var ctx = {worksheet: name || 'Worksheet', table: output}//table.innerHTML}
document.getElementById("dlink").href = uri + base64(format(template, ctx));
document.getElementById("dlink").download = filename;
document.getElementById("dlink").click();
}
})();
</script>

关于jquery - Javascript - 将 JQuery 数据表导出到 Excel 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17803261/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com