Set borders for Apache poi merge Cells

Source: Internet
Author: User
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,

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.