Hssfworkbook WB = new hssfworkbook (); hssfsheet sheet = WB. createsheet (); // create a style hssfcellstyle styleborderthin = WB. createcellstyle (); setborder. setborderbottom (hssfcellstyle. border_thin); // setborder. setborderleft (hssfcellstyle. border_thin); // setborder on the left border. setbordertop (hssfcellstyle. border_thin); // setborder. setborderright (hssfcellstyle. border_thin); // The Right Border hssfrow ROW = sheet. createrow (2); // the third row of sheet. addmergedregion (New cellrangeaddress (2, // first row (0-based) 2, // last row (0-based) 1, // first column (0-based) 5 // last column (0-based); // merge the row of the cell from Column 2nd to column 6th (B to F) in the third row. createcell (1 ). setcellvalue ("Answer options"); // value row. createcell (2); row. createcell (3); row. createcell (4); row. createcell (5); row. createcell (6 ). setcellvalue ("Reply status"); For (cell: Row) {Cell. setcellstyle (styleborderthin );}
Here, we need to explain that 1-6 is a merge cell, and the value is the value of the first cell. Therefore, it doesn't matter if the cell assignment is not assigned a value, but it must be createcell,
Row. createcell (2 );
Row. createcell (3 );
Row. createcell (4 );
Row. createcell (5); this is not meaningless, if removed
The error result is as follows:
The correct result after createcell 2-5 is added:
In addition, I found an interesting place. When operating to merge cells in Excel, if other cells except the first cell to be merged also have values, a prompt will be displayed.
.
After the cells are merged, remove the merged cells. The values are cleared.
However, if you use poi to merge cells
Row. createcell (2). setcellvalue ("222 ");
Row. createcell (3). setcellvalue ("333 ");
Row. createcell (4). setcellvalue ("444 ");
Row. createcell (5). setcellvalue ("555 ");
After exporting the Excel file, you can remove the merged cells and find that these values are retained,