2010-07-05 73 views
0

有什么简单的方法来使用JavaScript生成一个简单的Excel文件?使用javascript生成Excel文件

最好的问候,

+4

可能重复的[如何通过Javascript生成Excel](http://stackoverflow.com/questions/333537/how-to-generate-excel-through-javascript) – Oddthinking 2010-07-05 05:47:11

+0

为什么不只是做一个ajax调用呢? – Gogol 2015-03-02 10:30:01

回答

1

这将是非常困难的产生的Excel文件,而是一个CSV文件将是容易建立:

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

产生它的JavaScript,并发送作为下载使用Downloadify

Downloadify是一个小小的JavaScript + Flash库,可以生成和保存fi在飞行中,在浏览器中,无需服务器交互。

1

Click Here使用电子表格文档,我们可以使用JavaScript 只要按照文件,并添加语法 你参考上面的XML 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>' 

而且atlast编码为base64,形成一个数据链接开始动态地创建电子表格with'data:application/vnd.ms-excel; base64,{base64 encoded above xml}

Atlast它可能看起来像这样请点击这里下载excel enter code here