-
August 21st, 2019, 09:40 AM
#1
POI and setting cells to VerticalAlignment.MIDDLE
Hey all I am trying to get my cell to vertical line instead of just being aligned by the left side using POI.
This is my java code:
Code:
static CellStyle headerCellStyle = workbook.createCellStyle();
headerCellStyle = workbook.createCellStyle();
Row headerRow = null;
sheet = workbook.createSheet("String " + sheetname);
headerCellStyle.setWrapText(true);
headerCellStyle.setAlignment(HorizontalAlignment.LEFT);
headerCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
headerCellStyle.setVerticalAlignment(VerticalAlignment.MIDDLE);
// Create a Row
headerRow = sheet.createRow(0);
However, the line headerCellStyle.setVerticalAlignment(VerticalAlignment.MIDDLE); has an error of:
The method setVerticalAlignment(org.apache.poi.ss.usermodel.VerticalAlignment) in the type CellStyle is not applicable for the arguments (org.apache.poi.sl.usermodel.VerticalAlignment)
How can i go about getting this to work if I have already defined it as an static CellStyle headerCellStyle = workbook.createCellStyle();?
-
October 3rd, 2019, 02:59 AM
#2
Re: POI and setting cells to VerticalAlignment.MIDDLE
There's nothing wrong with your code. Strange.. maybe that's some other problem...
-
November 12th, 2020, 01:32 AM
#3
Re: POI and setting cells to VerticalAlignment.MIDDLE
Hi,I would suggest that you try the following code snippet to align text vertically wihtin a cell. It requires spire.xls.jar (free) to be referenced as a dependency.
Code:
import com.spire.xls.*;
public class AlignText {
public static void main(String[] args){
//Create a workbook
Workbook workbook = new Workbook();
//Load an Excel file
workbook.loadFromFile("Sample.xlsx");
//Get the first worksheet
Worksheet sheet = workbook.getWorksheets().get(0);
//Set the vertical alignment to Center
sheet.getCellRange("B2").getCellStyle().setVerticalAlignment(VerticalAlignType.Center);
//Save the result file
workbook.saveToFile("AlignText.xlsx", ExcelVersion.Version2010);
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|