Javascript - экспортировать JQuery dataTable в проблемы Excel

У меня есть следующий код:

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


person WDrgn    schedule 23.07.2013    source источник
comment
Большое спасибо за: table { mso-displayed-decimal-separator: \.; mso-отображаемый-разделитель-тысяч: ; } Мне ОЧЕНЬ помогло!   -  person Perty    schedule 16.04.2014


Ответы (1)


Я решил это так:

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

person WDrgn    schedule 29.07.2013