使用js将表格从html导出到excel(出现问题)

时间:2019-04-16 05:11:27

标签: javascript html excel

不能将方向横向参数传递给excel,尽管可以很好地传递缩放参数

试图包括@page解决方案,试图通过页面方向参数尝试了各种示例和解决方案。 PS:使用动态表客户端时,表本身可以很好地通过它们的值,甚至可以传递缩放参数,这会导致方向问题 编辑代码,由于代码检查器,开始被剪掉

var tableToExcel = (function() {
            var uri = 'data:application/vnd.ms-excel;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">' +
                    '<meta http-equiv="content-type" content="application/vnd.ms-excel; charset=UTF-8"><head>' +
                    '<!--[if gte mso 9]>' +
                    '<xml><x:ExcelWorkbook>' +

                    '<x:ExcelWorksheets>' +
                    '<x:ExcelWorksheet>' +
                    '<x:Name>{worksheet}' +
                    '</x:Name>' +
                    '<x:WorksheetOptions>' +


                    '<x:Zoom>100</x:Zoom>'+

                    '<x:DisplayGridlines/>' +
                    '</x:WorksheetOptions>' +
                    '</x:ExcelWorksheet>' +
                    '</x:ExcelWorksheets>' +
                    '</x:ExcelWorkbook>' +
                    '</xml><![endif]-->' +
                    '</head><body><table><caption>{table}</caption></table></body></html>'
            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(table, name) {
                if (!table.nodeType) table = document.getElementById("examplecopy")
                var ctx = {
                    worksheet: name || 'Worksheet',
                    table: table.innerHTML
                }
                window.location.href = uri + base64(format(template, ctx))
            }
        })()

2 个答案:

答案 0 :(得分:0)

  <script type="text/javascript">
    var tableToExcel = (function() {
      var uri = 'data:application/vnd.ms-excel;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]><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]--><meta http-equiv="content-type" content="text/plain; charset=UTF-8"/></head><body><table>{table}</table></body></html>'
        , 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(table, name) {
        if (!table.nodeType) table = document.getElementById(table)
        var ctx = {worksheet: name || 'Worksheet', table: table.innerHTML}
        window.location.href = uri + base64(format(template, ctx))
      }
    })()
    </script>

答案 1 :(得分:0)

<script>

    var tableToExcel = (function() {

        var uri = 'data:application/vnd.ms-excel;base64,',

            template = '<html xmlns:v="urn:schemas-microsoft-com:vml"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> <meta http-equiv=Content-Type content="text/html; charset=utf-8"> <meta name=ProgId content=Excel.Sheet> <meta name=Generator content="Microsoft Excel 11">'+
           ' <!--[if gte mso 9]><xml><o:DocumentProperties><o:Author>Pat Willener</o:Author><o:Company>Good Day Books</o:Company>'+
      '  </o:DocumentProperties><o:OfficeDocumentSettings><o:RelyOnVML/><o:AllowPNG/></o:OfficeDocumentSettings>'+
      '  </xml><![endif]-->'+
       ' <style><!--table {mso-displayed-decimal-separator:"\.";mso-displayed-thousand-separator:"\,";}  @page {mso-header-data:"&CInventory";  mso-footer-data:"Page &P of &N";'+
           ' margin:.31in 0in .31in 0in;  mso-header-margin:0in;  mso-footer-margin:0in;  mso-page-orientation:landscape;}  --></style>'+
       ' <!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>Inventory</x:Name><x:WorksheetOptions><x:Print><x:ValidPrinterInfo/>'+
       ' <x:PaperSizeIndex>9</x:PaperSizeIndex><x:HorizontalResolution>600</x:HorizontalResolution><x:VerticalResolution>600</x:VerticalResolution></x:Print><x:Selected/>'+
       ' <x:Panes><x:Pane><x:Number>1</x:Number><x:ActiveRow>1</x:ActiveRow></x:Pane></x:Panes><x:ProtectContents>False</x:ProtectContents><x:ProtectObjects>False</x:ProtectObjects>'+
        '<x:ProtectScenarios>False</x:ProtectScenarios></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets><x:WindowHeight>8835</x:WindowHeight><x:WindowWidth>15180</x:WindowWidth>'+
       ' <x:WindowTopX>120</x:WindowTopX><x:WindowTopY>105</x:WindowTopY><x:ProtectStructure>False</x:ProtectStructure><x:ProtectWindows>False</x:ProtectWindows></x:ExcelWorkbook>'+
        '</xml><![endif]-->'+

       ' <title>Inventory</title>'+

                '</head><body><table><caption>{table}</caption></table></body></html>'
        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(table, name) {
            if (!table.nodeType) table = document.getElementById("examplecopy")
            var ctx = {
                worksheet: name || 'Worksheet',
                table: table.innerHTML
            }
            window.location.href = uri + base64(format(template, ctx))
        }
    })()

</script>

这个对我有用