web项目中导出excel常用几种方法:
1、poi,jxl导出到excel
操作对象然后将结果写到excel中,数据量比较大时容易内存溢出,jxl内存控制方面稍好。防止内存溢出可以分批导出多个excel,最后再打包下载
2、拼excel的html形式文件,将文件扩展名改为xls。Jsp中只要将http头改一下,让客户端浏览器下载生成的.xls文件,而不是让浏览器打开html文件
利用excel能够另存为html格式文件,文件包含excel的头信息。
3、生成符合EXCEL的XML规范的XML文件。生成后缀名为.xls,内容为xml格式的文本,其中的xml必须完全符合excel xml规范。采用DataOutStream流的形式,先将xml格式的内容写到服务器文件系统中,再下载。如果数据量大,可分多个sheet。xml格式如下:
<?xml version="1.0" encoding="GBK" ?>
<?mso-application progid="Excel.Sheet"?>
<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">
<Version>11.9999</Version>
</DocumentProperties>
<ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
<WindowHeight>10005</WindowHeight>
<WindowWidth>10005</WindowWidth>
<WindowTopX>120</WindowTopX>
<WindowTopY>135</WindowTopY>
<ActiveSheet>1</ActiveSheet>
<ProtectStructure>False</ProtectStructure>
<ProtectWindows>False</ProtectWindows>
</ExcelWorkbook>
<Styles>
<Style ss:ID="Default" ss:Name="Normal">
<Alignment ss:Vertical="Center"/>
<Borders/>
<Font ss:FontName="ËÎÌå" x:CharSet="134" ss:Size="12"/>
<Interior/>
<NumberFormat/>
<Protection/>
</Style>
<Style ss:ID="s23">
<Font ss:FontName="ËÎÌå" x:CharSet="134" ss:Size="12" ss:Color="#0000FF"
ss:Bold="1"/>
<Interior ss:Color="#C0C0C0" ss:Pattern="Solid"/>
</Style>
</Styles>
<Worksheet ss:Name="Sheet1">
<Table ss:ExpandedColumnCount="2" ss:ExpandedRowCount="3" x:FullColumns="1"
x:FullRows="1" ss:DefaultColumnWidth="54" ss:DefaultRowHeight="16.5">
<Row ss:AutoFitHeight="0">
<Cell><Data ss:Type="Number">1</Data></Cell>
<Cell><Data ss:Type="String">aaa</Data></Cell>
</Row>
<Row ss:AutoFitHeight="0">
<Cell><Data ss:Type="Number">2</Data></Cell>
<Cell><Data ss:Type="String">bbb</Data></Cell>
</Row>
<Row ss:AutoFitHeight="0">
<Cell><Data ss:Type="Number">3</Data></Cell>
<Cell><Data ss:Type="String">ccc</Data></Cell>
</Row>
</Table>
<WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
<Unsynced/>
<ProtectObjects>False</ProtectObjects>
<ProtectScenarios>False</ProtectScenarios>
</WorksheetOptions>
</Worksheet>
<Worksheet ss:Name="Sheet2">
<Table ss:ExpandedColumnCount="4" ss:ExpandedRowCount="4" x:FullColumns="1"
x:FullRows="1" ss:DefaultColumnWidth="54" ss:DefaultRowHeight="14.25">
<Column ss:AutoFitWidth="0" ss:Width="73.5"/>
<Column ss:AutoFitWidth="0" ss:Width="119.25"/>
<Column ss:AutoFitWidth="0" ss:Width="89.25"/>
<Column ss:AutoFitWidth="0" ss:Width="141"/>
<Row>
<Cell ss:StyleID="s23"><Data ss:Type="String">aaaa</Data></Cell>
<Cell ss:StyleID="s23"><Data ss:Type="String">bbbb</Data></Cell>
<Cell ss:StyleID="s23"><Data ss:Type="String">cccc</Data></Cell>
<Cell ss:StyleID="s23"><Data ss:Type="String">dddd</Data></Cell>
</Row>
<Row>
<Cell><Data ss:Type="String">aaaa</Data></Cell>
<Cell><Data ss:Type="String">bbbb</Data></Cell>
<Cell><Data ss:Type="String">cccc</Data></Cell>
<Cell><Data ss:Type="String">dddd</Data></Cell>
</Row>
<Row>
<Cell><Data ss:Type="String">aaaa</Data></Cell>
<Cell><Data ss:Type="String">bbbb</Data></Cell>
<Cell><Data ss:Type="String">cccc</Data></Cell>
<Cell><Data ss:Type="String">dddd</Data></Cell>
</Row>
<Row>
<Cell><Data ss:Type="String">aaaa</Data></Cell>
<Cell><Data ss:Type="String">bbbb</Data></Cell>
<Cell><Data ss:Type="String">cccc</Data></Cell>
<Cell><Data ss:Type="String">dddd</Data></Cell>
</Row>
</Table>
<WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
<Print>
<ValidPrinterInfo/>
<PaperSizeIndex>9</PaperSizeIndex>
<HorizontalResolution>600</HorizontalResolution>
<VerticalResolution>600</VerticalResolution>
</Print>
<Selected/>
<ProtectObjects>False</ProtectObjects>
<ProtectScenarios>False</ProtectScenarios>
</WorksheetOptions>
</Worksheet>
<Worksheet ss:Name="Sheet3">
<Table ss:ExpandedColumnCount="0" ss:ExpandedRowCount="0" x:FullColumns="1"
x:FullRows="1" ss:DefaultColumnWidth="54" ss:DefaultRowHeight="16.5"/>
<WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
<Unsynced/>
<ProtectObjects>False</ProtectObjects>
<ProtectScenarios>False</ProtectScenarios>
</WorksheetOptions>
</Worksheet>
</Workbook>