Hi,
Thanks for the template files.
I have checked your template files. Well, your desired file has many merged cells with different alignment and other formatting settings. Moreover, different merged cells have equal widths of the corresponding columns involved, different merged cells have different row heights as well. You have to apply different formattings to different merged cells as per your expected file.
I have written a sample code for your reference. In the example, I pick the A8 merged cell and specify equal width of each column involved in the merged cell’s range. Also, I specify the font attributes and alignment settings to match with your desired file’s formatting a bit. Please refer to the code segment and write your own code to apply formatting for other cells in accordance with your desired formatting in your expected file.
e.g
Sample code:
Workbook workbook = new Workbook(“f:\files\What+i+am+getting.xlsx”);
Worksheet workSheet = workbook.getWorksheets().get(0);
//Get the cell
Cell cell = workSheet.getCells().get(“A8”);
if(cell.isMerged())
{
Style style = cell.getStyle();
//Set the font attributes
style.getFont().setName(“Tamoma”);
style.getFont().setSize(8);
//Set the alignment settings
style.setHorizontalAlignment(TextAlignmentType.GENERAL);
style.setVerticalAlignment(TextAlignmentType.TOP);
//Apply the style
cell.setStyle(style);
//get the merged cell’s range
Range range = cell.getMergedRange();
int sectionStartColumn = range.getFirstColumn();
int sectionEndColumn = range.getColumnCount() - range.getFirstColumn() + 1;
//Iterate through the columns to specify the width equally for each column in the merged cell’s range.
for (int i = sectionStartColumn; i <= sectionEndColumn; i++)
{
workSheet.getCells().setColumnWidth(i, 8.29);
}
}
workbook.save(“f:\files\out1.xlsx”);
Hope, this helps a bit.
Thank you.