CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: POI and setting cells to VerticalAlignment.MIDDLE

  1. #1
    Join Date
    Aug 2008
    Posts
    107

    Question 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();?

  2. #2
    Join Date
    Oct 2019
    Posts
    11

    Re: POI and setting cells to VerticalAlignment.MIDDLE

    There's nothing wrong with your code. Strange.. maybe that's some other problem...

  3. #3
    Join Date
    Apr 2014
    Posts
    23

    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)