Methods used to merge cells:
Sheet.addmergedregion (cellrangeaddress cellrangeaddress); The method of constructing a Cellrangeaddress object requires passing in the first row, last row, first column, and last column of the merged cell. Cellrangeaddress cra=new cellrangeaddress (0, 3, 3, 9); How to write data to a merged cell
- The first thing to look at is the Firstcol index of your cellrangeaddress construction method
- Creating a Firstcol Cell object
- The set method of the cell writes the data
Write data in the next position of a merged cell
- View Lastcol Index of the Cellrangeaddress construction method
- Create Lastcol+1 cell
- The set method of the cell writes the data
Here is the demo:
1FileOutputStream fos=NewFileOutputStream ("D:\\13.xls"); 2 3Workbook wb=NewHssfworkbook (); 4 5Sheet sheet=Wb.createsheet (); 6 /* 7 * Set the range of merged cell ranges8 * FirstRow 0-based9 * LastRow 0-basedTen * Firstcol 0-based One * Lastcol 0-based A */ -Cellrangeaddress cra=NewCellrangeaddress (0, 3, 3, 9); - the //add merged cells in sheet - sheet.addmergedregion (CRA); - -Row row = Sheet.createrow (0); + -Cell Cell_1 = Row.createcell (3); + ACell_1.setcellvalue ("When you ' re right, no one remembers, when you ' re wrong, no one forgets."); at - //cell position 3-9 is merged into a single cell, no matter how you create a 4th cell or a 5th cell ... Then write the data. are not writable. -Cell cell_2 = Row.createcell (10); - -Cell_2.setcellvalue ("What's Up!")); - in wb.write (FOS); - toFos.close ();
Apache POI Merge Cell--Simple explanation version with demo