Recently done a small project, in the middle of a link is to save the data in the collection to a local Excel file, the local Excel file read into the collection
POI '
To put it simply: POI is provided by Apache and provides read and write support for documents in Microsoft Office format!
- HSSF-provides the ability to read and write files in Microsoft Excel format.
- XSSF-provides the ability to read and write Microsoft Excel ooxml format files.
- HWPF-provides the ability to read and write files in Microsoft Word format.
- HSLF-provides the ability to read and write files in Microsoft PowerPoint format.
- HDGF-provides the ability to read and write Microsoft Visio format files.
To read Excel, you first have to download the rack package: Poi-**-**.jar The two businesses I've encountered in the project: 1. Import data into Excel
1**2*Excel Tools Class3*available: Import Excel and Export Excel methods4*@author Marvel5*6*/7 Public classExceltool {8 Private StaticHssfcell cell;//column9 Private StaticHssfworkbook HWB;//Excel ObjectTen Private StaticHssfsheet sheet;//Work Folders One Private StaticHssfrow Row;//Line A - - /** the * Import Excel - * @paramSTRs Title - * @paramList Collection - * @paramPath/file name is saved. xls + * @throwsException - */ + Public StaticInteger toexcel (list<object[]> list,string path)throwsexception{ A //Create an Excel document with content in memory at this time athwb=NewHssfworkbook (); - //sheet corresponding to create a work folder; -Sheet=hwb.createsheet ("Debug.factory"); - //Print Line - for(intI=0;i<list.size (); i++){ - //Create a row inrow=Sheet.createrow (i); - //get a row of data toObject []objs=(object[]) list.get (i); + //Print Columns - for(inta=0;a<objs.length;a++){ the //Create a column *Cell=Row.createcell (a); $ //Set ValuePanax Notoginseng Cell.setcellvalue (objs[a].tostring ()); - } the } + //Create a file output stream, prepare the output spreadsheet AOutputStream out =NewFileOutputStream (path); the Hwb.write (out); + out.close (); - returnHwb.getsheetat (0). Getlastrownum (); $ } $}
Test code:
1 Packagecom.dev4j.debug.test;2 3 Import Staticorg.junit.assert.*;4 5 Importjava.util.ArrayList;6 Importjava.util.List;7 8 ImportOrg.junit.Before;9 Importorg.junit.Test;Ten One ImportCom.dev4j.debug.tool.ExcelTool; A - /** - * the * @authorMarvel - * - */ - Public classExceltooltest { + - + @Test A Public voidTesttoexcel ()throwsException { atList<object[]> objs=NewArraylist<object[]>(); -Object []obj=Newobject[]{"Zhang San", "18", "Male"}; -Objs.add (Newobject[]{"Name", "Age", "gender"}); - objs.add (obj); - - //because Getlastrownum (); Returns the subscript for the last row of data, starting at 0, so it returns a subscript of 1. inAssertequals ((Long) 1, (Long) Exceltool.toexcel (OBJS, "E://test2.xls")); - } to +}
2. Export Excel and Append records
1 /**2 * Common needs3 * @paramlists4 */5 Public Static voidWrite (Integer startrow,list<object[]>lists) {6 for(intI=0;i<lists.size (); i++){7 //Create a row8Row=sheet.createrow (startrow+i);9 //get a row of dataTenObject []objs=(object[]) lists.get (i); One //Print Columns A for(inta=0;a<objs.length;a++){ - //Create a column -Cell=Row.createcell (a); the //Set Value - Cell.setcellvalue (objs[a].tostring ()); - } - } + - } + A at - /** - * Open File and append record - * @paramList - * @paramPath - * @return in * @throwsException - */ to Public StaticInteger appendexcel (list<object[]> list,string path)throwsexception{ + //input Stream Loading -InputStream is =NewFileInputStream (path); the //Create a Hwb object *HWB =NewHssfworkbook (IS); $ //record the last line record subscript, +1 update the next line to write subscriptPanax Notoginseng intLastrow=hwb.getsheetat (0). Getlastrownum (+1); - //Looping Create the write (lastrow,list); + //Create a file output stream, prepare the output spreadsheet AOutputStream out =NewFileOutputStream (path); the Hwb.write (out); + out.close (); - returnHwb.getsheetat (0). Getlastrownum (); $}
The previously written code contains duplicate code snippets, extracted, written in the Write (integer,list<object[]>) method
This is a simple Excel read and write operation, fast get it!
Poi-microsoft Office read/write support