POI read/write Excel-operations include merge Cell operations, poiexcel-
In the previous blog, I wrote about parsing Excel operations into related classes. Next I will write an Excel file to read and write Excel tables.
For Excel table operations, the most important thing is to create a workBook. The operation sequence is:
1. Obtain the WorkBook instance;
Workbook workbook = WorkbookFactory.create(file);
2. Obtain each Sheet object, that is, each Sheet in an Excel table.
for (int i = 0; i < workbook.getNumberOfSheets(); i++) { Sheet sheet = workbook.getSheetAt(i);}
3. Obtain each row in the sheet.
for (int i = sheet.getFirstRowNum() ; i < sheet.getLastRowNum(); i++){ Row row = sheet.getRow(i);}
4. Obtain cell data for each row
for (int i = row.getFirstCellNum(); i < row.getLastCellNum(); i++) { Cell cell = row.getCell(i);}
5. parse the data of each cell
Cell merging:
Use the addMergedRegion method of sheet:
Sheet. addMergedRegion (new CellRangeAddress (int startRow, int endRow, int startCol, int endCol); // set cell Merging
Note the following:
1. to use this method, you must first create the merged unit row, that is, each row from startRow to EndRow.
2. startRow should be greater than or equal to endRow
3. Create a cell using sheet. getRow (startRow). createCell (startCol );
For specific implementation, see the code on GitHub: https://github.com/JinGangRed/poi-exceluntil