Экспорт таблицы из html в Excel с помощью js (orentation problem)

#javascript #html #excel

#javascript #HTML #excel

Вопрос:

Не могу передать параметр альбомной ориентации в Excel, хотя могу передать параметр масштабирования просто отлично

Пытался включить решение @page, пытался передать параметр ориентации страницы, пробовал различные примеры и решения. PS: работая с клиентской частью dynamical tables, сама таблица проходит нормально с их значениями и даже проходит параметр масштабирования, проблема с ориентацией отредактированного кода, начало было вырезано из-за проверки кода

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

Ответ №1:

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

Ответ №2:

 <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:"amp;CInventory";  mso-footer-data:"Page amp;P of amp;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>
 

это сработало для меня