+ /** A * Use POI to manipulate Excel forms at * - * Jar Package Required: - * HSSF for 03 and previous versions, i.e.. xls suffix - * |---poi-3.16.jar - * XSSF for 07 and later versions, the xlsx suffix - * |---poi-3.16.jar in * |---poi-ooxml.3.16.jar - * |---poi-ooxml-schemas-3.16.jar to * |---xmlbeans-2.6.0.jar + * |---commons-collections4-4.1.jar - * the * Workbook: Workbook * * Worksheet: Sheet $ * Line: rowPanax Notoginseng * Table: Cell - */ the Public classDemo { + A /** the * Read Excel table + * @throws ioeception - * @throws invalidformatexception $ * @throws encrypteddocumentexception $ */ - @Test - Public voidReadexcel () throws Encrypteddocumentexception, Invalidformatexception, IOException { the // Workbooks -Workbook Workbook = Workbookfactory.create (NewFile ("Src/userexce1.xls"));Wuyi //Work Table theSheet Sheet = Workbook.getsheetat (0); - //Line WuRow row = Sheet.getrow (1); - //column AboutCell cell = Row.getcell (0); $ -System. out. println ("table values are:"+Cell.getstringcellvalue ()); - } - A /** + * Create Excel Table the * @throws IOException - * @throws invalidformatexception $ * @throws encrypteddocumentexception the */ the @Test the Public voidWriteexcel () throws Encrypteddocumentexception, Invalidformatexception, IOException { the // Workbooks -Workbook Workbook =NewXssfworkbook (); in //Work Table theSheet Sheet = Workbook.createsheet ("my first sheet."); the //Line AboutRow row = Sheet.createrow (0); the //column theCell cell = Row.createcell (3); the +Cell.setcellvalue ("haha table Insert a content"); - theWorkbook.write (NewFileOutputStream ("c:/test.xlsx"));Bayi } the the /** - * Read Excel tables, modify styles and content, and save - * @throws ioeception the * @throws invalidformatexception the * @throws encrypteddocumentexception the */ the @Test - Public voidReadandmodifyexcel () throws Encrypteddocumentexception, Invalidformatexception, IOException { the theString filename ="src/userexcel.xlsx"; theFile File =NewFile (filename);94 theSystem. out. println (File.getabsolutepath ()); the theWorkbook Workbook =workbookfactory.create (file);98Sheet Sheet = Workbook.getsheetat (0); About - //merge cells, add merged cells to a worksheet101Cellrangeaddress Headspan =NewCellrangeaddress (2,2,0,4);102 sheet.addmergedregion (headspan);103 104 //set the header text in the merged position theRow row = Sheet.createrow (0);106Cell cell = Row.createcell (0);107Cell.setcellvalue ("This is the table header .");108 109 //Writing in text theString savedname = filename.matches ("\\s+\\.xls") ?"c:/"+ Filename.replace ("src/","") :"c:/"+ Filename.replace ("src/","");111Workbook.write (NewFileOutputStream (Savedname)); the }113 the}
Excel base Element
Workbooks
Worksheets (belonging to workbooks)
Row (belongs to worksheet)
Cell (belongs to row; determined by rows and columns)
-------------manipulating Excel
1. Create/Read workbooks
2. Create/Read worksheets
3. Create/Read rows
4. Create/Read cells
-----------Excel Style
Merged cell objects (cellrangeaddress) belong to the workbook; Apply to Worksheets
cellrangeaddress (int firstrow, int lastrow, int firstcol, int lastcol) Start line number, end line number, start column number, end column number
A style belongs to a workbook; applied to a cell
Fonts are part of the workbook, loaded in styles, and common styles applied to cells
Java Operations Excel's poi simple example