First, Introduction
HSSF is the implementation of the POI project's pure Java EXCEL97 (-2007) file format. XSSF is a Java implementation of the POI project's Excel OOXML (. xlsx) file format. HSSF and XSSF provides several ways to read spreadsheets to create, modify, read, and write XLS spreadsheets.
It provides the following features:
- Low-level structure for special needs
- An efficient read-only access event model API
- An API that provides a full user model for creating, reading, and modifying XLS files.
The project URL is as follows: Http://poi.apache.org/spreadsheet/index.html.
Second, examples
1. Related dependencies are as follows:
<dependency> <groupId>org.apache.poi</groupId> <artifactid>poi</artifactid > <version>3.12</version></dependency><dependency> <groupId> org.apache.poi</groupid> <artifactId>poi-ooxml</artifactId> <version>3.12 </version></dependency>
2. The sample code is as follows:
Package Com.test.excel;import Java.io.fileoutputstream;import java.util.arraylist;import java.util.List;import Org.apache.poi.ss.usermodel.cell;import Org.apache.poi.ss.usermodel.row;import Org.apache.poi.ss.usermodel.Sheet ; Import Org.apache.poi.xssf.streaming.SXSSFWorkbook;/** * *excle export*/ Public classExcelcreate {/** * Excel generated*/ Public Static voidMain (string[] args) throws Throwable {//Table HeaderList<string> headerlist =NewArraylist<> (Ten); for(inti =1; I <=Ten; i++) {Headerlist.add ("Table Header"+i); } //DataList<string[]> DataRowList2 =NewArraylist<string[]> (Ten); for(inti =1; I <= headerlist.size (); i++) {Datarowlist2.add (NewString[] {"Data"+ I,"Data"+ I,"Data"+ I,"Data"+ I,"Data"+ I,"Data"+ I,"Data"+ I,"Data"+ I,"Data"+ I,"Data"+i}); } Sxssfworkbook Workbook=NewSxssfworkbook ( -); Sheet Sheet= Workbook.createsheet ("Export"); intRowNum =0; //Table HeaderRow row = Sheet.createrow (rownum++); for(inti =0; I < headerlist.size (); i++) {cell cell=Row.createcell (i); Cell.setcellvalue (headerlist.Get(i)); } //Data for(string[] array:datarowlist2) {row= Sheet.createrow (rownum++); String[] Arraya=(string[]) array; for(inti =0; I < datarowlist2.size (); i++) {cell cell=Row.createcell (i); Cell.setcellvalue (Arraya[i]); }} fileoutputstream os=NewFileOutputStream ("c:/test/testexcel.xlsx"); //The following code is available for file download//Response.reset ();//Response.setcontenttype ("Application/octet-stream; Charset=utf-8 ");//Response.setheader ("content-disposition", "attachment; Filename= "+encodes.urlencode (" testexcel.xlsx "));//Write (Response.getoutputstream ());workbook.write (OS); Workbook.dispose (); }}
3. The result of operation is as follows:
POI Export Excel