How to auto adjust the column in excel in apache POI

storyteller

I am creating an excel file with apache poi the excel is generated but i can not adjust the column with according to the cell values i am posting the code what i have done so far

This is how i have created the headers in excel

HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet();
        sheet.protectSheet("password");
        sheet.autoSizeColumn(15);
        HSSFFont hSSFFont = wb.createFont();
        hSSFFont.setFontName(HSSFFont.FONT_ARIAL);
        hSSFFont.setFontHeightInPoints((short) 8);

        CellStyle style = wb.createCellStyle();
        /* cell style for locking */
        CellStyle lockedCellStyle = wb.createCellStyle();
        lockedCellStyle.setLocked(true);

        HSSFRow row = null;

        HSSFCell cell = null;

        row = sheet.createRow(0);
        int headercolumnNo = 0;



        //1st Column Header for Indicator
        cell = row.createCell(headercolumnNo);
        cell.setCellValue(new HSSFRichTextString(listOfActiveCarrierUserHeader.get(0)));
        style.setWrapText(true);
        style.setFillForegroundColor(IndexedColors.LIGHT_CORNFLOWER_BLUE.getIndex());
        style.setFont(hSSFFont);
        style.setFillPattern(CellStyle.SOLID_FOREGROUND);
        cell.setCellStyle(style);
        headercolumnNo = 1;

        cell = row.createCell(headercolumnNo);  //2nd Column Header for Firstname
        cell.setCellValue(new HSSFRichTextString(listOfActiveCarrierUserHeader.get(1)));
        style.setWrapText(true);
        style.setFillForegroundColor(IndexedColors.LIGHT_CORNFLOWER_BLUE.getIndex());
        style.setFont(hSSFFont);
        cell.setCellStyle(style);
        headercolumnNo = headercolumnNo + 1;

        cell = row.createCell(headercolumnNo);  //2nd Column Header for Firstname
        cell.setCellValue(new HSSFRichTextString(listOfActiveCarrierUserHeader.get(2)));
        style.setWrapText(true);
        style.setFillForegroundColor(IndexedColors.LIGHT_CORNFLOWER_BLUE.getIndex());
        style.setFont(hSSFFont);
        cell.setCellStyle(style);
        headercolumnNo = headercolumnNo + 1;

and this is how i have  populated the values in that excel file 

for(CarrierActiveUser carrierActiveUser : listOfCarrierUser){

            int columnNo = 0;
            row = sheet.createRow(j + 1);

            cell = row.createCell(columnNo);
            if(null != carrierActiveUser.getFistName()){

                cell.setCellValue(new HSSFRichTextString(carrierActiveUser.getFistName()));
                 lockedCellStyle.setFont(hSSFFont);
                 cell.setCellStyle(lockedCellStyle);

            }else{
                cell.setCellValue(new HSSFRichTextString(" "));
                cell.setCellStyle(lockedCellStyle);
            }

            columnNo = columnNo + 1;
            cell = row.createCell(columnNo);
            if(null != carrierActiveUser.getLastName()){

                cell.setCellValue(new HSSFRichTextString(carrierActiveUser.getLastName()));
                   lockedCellStyle.setFont(hSSFFont);
                cell.setCellStyle(lockedCellStyle);

            }else{
                cell.setCellValue(new HSSFRichTextString(" "));
                cell.setCellStyle(lockedCellStyle);
            }

            columnNo = columnNo + 1;
            cell = row.createCell(columnNo);
            if(null != carrierActiveUser.getLastName()){

                cell.setCellValue(new HSSFRichTextString(carrierActiveUser.getEmailId()));
                lockedCellStyle.setFont(hSSFFont);
                cell.setCellStyle(lockedCellStyle);

            }else{
                cell.setCellValue(new HSSFRichTextString(" "));
                cell.setCellStyle(lockedCellStyle);
            }

Please someone help me to adjust the columns , i am new to apache poi

Current output

Ravikumar

You can use HSSFSheet.autoSizeColumn(columnNumber) method to align the columns perfectly.

This method adjusts the column width to fit the contents, read the doc.

After setting all cell values for all columns you can use this method, in your current code call this method after for loop.

Sample code

sheet.autoSizeColumn(1);
sheet.autoSizeColumn(2);

Note - You have to do this separately for all columns which you want to be aligned and the call to sheet.autoSizeColumn(columnNumber) should be made after populating the data into the excel. Calling before populating data will not have any effect.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Writing to an excel using Apache POI corrupts the excel file

分類Dev

How to manipulate content of a comment with Apache POI

分類Dev

How to create a circle using GeneralPath and apache POI

分類Dev

How to auto scale apache zookeeper

分類Dev

Excel Apache Poiのトルコ通貨

分類Dev

Apache POI jar is not able to write a special character in excel file

分類Dev

Accessing Excel Sheets in Java using Apache POI with Eclipse IDE

分類Dev

Cannot read from Excel file using apache poi

分類Dev

Auto Size/Adjust Userform

分類Dev

Auto Adjust Textview Android

分類Dev

How to make my path relative in selenium apache poi while I input my Excel file? and How to write the test result in excel after performing testing

分類Dev

Excel 2013: how to format a column of cells to a predetermined amount of digits with auto-fill

分類Dev

How to autosize the "auto group column"?

分類Dev

Using Apache POI how retrieve a numeric cell value as an integer

分類Dev

Apache Poi Date値

分類Dev

Apache POI例外

分類Dev

Set margins with Apache poi

分類Dev

Java Apache POI

分類Dev

Apache POI ClassNotFoundException

分類Dev

Apache POIを使用してExcelで棒グラフを作成する

分類Dev

Apache Poiの数式でExcelを保存する方法は?

分類Dev

Java Apache POIでの既存のExcelファイルの更新

分類Dev

Apache POIを使用したExcelファイルの更新

分類Dev

Java Apache POIを使用してExcelに行を挿入する

分類Dev

apache poi Excelの大きな自動列幅

分類Dev

Apache POIを使用して特定のExcel列を読み取る方法

分類Dev

Apache POIを使用してExcelシートを削除する

分類Dev

Apache POIでの自動折り返し(Excel)

分類Dev

Apache POIを使用してExcelでセルを結合する

Related 関連記事

  1. 1

    Writing to an excel using Apache POI corrupts the excel file

  2. 2

    How to manipulate content of a comment with Apache POI

  3. 3

    How to create a circle using GeneralPath and apache POI

  4. 4

    How to auto scale apache zookeeper

  5. 5

    Excel Apache Poiのトルコ通貨

  6. 6

    Apache POI jar is not able to write a special character in excel file

  7. 7

    Accessing Excel Sheets in Java using Apache POI with Eclipse IDE

  8. 8

    Cannot read from Excel file using apache poi

  9. 9

    Auto Size/Adjust Userform

  10. 10

    Auto Adjust Textview Android

  11. 11

    How to make my path relative in selenium apache poi while I input my Excel file? and How to write the test result in excel after performing testing

  12. 12

    Excel 2013: how to format a column of cells to a predetermined amount of digits with auto-fill

  13. 13

    How to autosize the "auto group column"?

  14. 14

    Using Apache POI how retrieve a numeric cell value as an integer

  15. 15

    Apache Poi Date値

  16. 16

    Apache POI例外

  17. 17

    Set margins with Apache poi

  18. 18

    Java Apache POI

  19. 19

    Apache POI ClassNotFoundException

  20. 20

    Apache POIを使用してExcelで棒グラフを作成する

  21. 21

    Apache Poiの数式でExcelを保存する方法は?

  22. 22

    Java Apache POIでの既存のExcelファイルの更新

  23. 23

    Apache POIを使用したExcelファイルの更新

  24. 24

    Java Apache POIを使用してExcelに行を挿入する

  25. 25

    apache poi Excelの大きな自動列幅

  26. 26

    Apache POIを使用して特定のExcel列を読み取る方法

  27. 27

    Apache POIを使用してExcelシートを削除する

  28. 28

    Apache POIでの自動折り返し(Excel)

  29. 29

    Apache POIを使用してExcelでセルを結合する

ホットタグ

アーカイブ