【问题标题】:Generate Excel file using javascript使用 javascript 生成 Excel 文件
【发布时间】:2010-07-05 05:37:50
【问题描述】:

有没有什么简单的方法可以用 JavaScript 生成一个简单的 excel 文件?

最好的问候,

【问题讨论】:

标签: javascript excel


【解决方案1】:

Click Here使用电子表格文档我们可以使用javascript动态创建电子表格 只需遵循文档并添加语法 你参考this

'<?xml version="1.0"?>',
            '<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:html="http://www.w3.org/TR/REC-html40">',
            '<DocumentProperties xmlns="urn:schemas-microsoft-com:office:office"><Title>' + theTitle + '</Title></DocumentProperties>',
            '<OfficeDocumentSettings xmlns="urn:schemas-microsoft-com:office:office"><AllowPNG/></OfficeDocumentSettings>',
            '<ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">',
            '<WindowHeight>' + worksheet.height + '</WindowHeight>',
            '<WindowWidth>' + worksheet.width + '</WindowWidth>',
            '<ProtectStructure>False</ProtectStructure>',
            '<ProtectWindows>False</ProtectWindows>',
            '</ExcelWorkbook>',

            '<Styles>',

            '<Style ss:ID="Default" ss:Name="Normal">',
            '<Alignment ss:Vertical="Bottom"/>',
            '<Borders/>',
            '<Font ss:FontName="Calibri" x:Family="Swiss" ss:Size="12" ss:Color="#000000"/>',
            '<Interior/>',
            '<NumberFormat/>',
            '<Protection/>',
            '</Style>',

            '<Style ss:ID="title">',
            '<Borders />',
            '<Font ss:Bold="1" ss:Size="18" />',
            '<Alignment ss:Horizontal="Center" ss:Vertical="Center" ss:WrapText="1" />',
            '<NumberFormat ss:Format="@" />',
            '</Style>',

            '<Style ss:ID="headercell">',
            '<Font ss:Bold="1" ss:Size="10" />',
            '<Alignment ss:Horizontal="Center" ss:WrapText="1" />',
            '<Interior ss:Color="#A3C9F1" ss:Pattern="Solid" />',
            '</Style>',


            '<Style ss:ID="even">',
            '<Interior ss:Color="#CCFFFF" ss:Pattern="Solid" />',
            '</Style>',


            '<Style ss:ID="evendate" ss:Parent="even">',
            '<NumberFormat ss:Format="yyyy-mm-dd" />',
            '</Style>',


            '<Style ss:ID="evenint" ss:Parent="even">',
            '<Numberformat ss:Format="0" />',
            '</Style>',

            '<Style ss:ID="evenfloat" ss:Parent="even">',
            '<Numberformat ss:Format="0.00" />',
            '</Style>',

            '<Style ss:ID="odd">',
            '<Interior ss:Color="#CCCCFF" ss:Pattern="Solid" />',
            '</Style>',

            '<Style ss:ID="groupSeparator">',
            '<Interior ss:Color="#D3D3D3" ss:Pattern="Solid" />',
            '</Style>',

            '<Style ss:ID="odddate" ss:Parent="odd">',
            '<NumberFormat ss:Format="yyyy-mm-dd" />',
            '</Style>',

            '<Style ss:ID="oddint" ss:Parent="odd">',
            '<NumberFormat Format="0" />',
            '</Style>',

            '<Style ss:ID="oddfloat" ss:Parent="odd">',
            '<NumberFormat Format="0.00" />',
            '</Style>


            '</Styles>
            worksheet.xml,
            '</Workbook>'

最后把xml上面编码成base64,形成一个data-url,以'data:application/vnd.ms-excel;base64,{base64 encoding above xml}开始

最后可能是这个样子 请点击这里下载excelenter code here

【讨论】:

    【解决方案2】:

    生成 Excel 文件会非常困难,但生成 CSV 文件会很容易:

    field1,field2,field3
    value1,value2,value3
    value1,value2,value3
    

    在 JavaScript 中生成它,并使用Downloadify 将其作为下载发送给用户:

    Downloadify 是一个小型 JavaScript + Flash 库,可以在浏览器中动态生成和保存文件,无需服务器交互。

    【讨论】:

    • 投反对票 - 这不是问题所在。
    猜你喜欢
    • 1970-01-01
    • 2018-05-20
    • 2011-02-14
    • 1970-01-01
    • 2012-10-25
    • 1970-01-01
    • 2018-05-01
    • 2012-03-25
    • 1970-01-01
    相关资源
    最近更新 更多