1

我有以下代码:

<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) 我在 DataTable 中有多个页面,但这只会导出所选页面上的记录。

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

4

1 回答 1

3

我是这样解决的:

                   <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>

于 2013-07-29T08:48:43.463 回答