0

数値の列データをフォーマットしようとしています。以下は、単一セルで機能するコードです。

//Instantiating a Workbook object
Workbook workbook = new Workbook();

//Accessing the added worksheet in the Excel file
int sheetIndex = workbook.getWorksheets().add();
Worksheet worksheet = workbook.getWorksheets().get(sheetIndex);
Cells cells = worksheet.getCells();

//Adding the current system date to "A1" cell
Cell cell = cells.get("A1");
cell.setValue(Calendar.getInstance());

//Setting the display format of the date to number 15 to show date as "d-mmm-yy"
Style style = cell.getStyle();
style.setCustom("d-mmm-yy");
cell.setStyle(style);

//Adding a numeric value to "A2" cell
cell = cells.get("A2");
cell.setValue(20);

//Setting the display format of the value to number 9 to show value as percentage
style = cell.getStyle();
style.setCustom("0.0%");
cell.setStyle(style);

//Adding a numeric value to "A3" cell
cell = cells.get("A3");
cell.setValue(1546);

//Setting the display format of the value to number 6 to show value as currency
style = cell.getStyle();
style.setCustom("$#,##0;[Red]$-#,##0");
cell.setStyle(style);

//Saving the modified Excel file in default format
workbook.save("C:\\output.xls");

これが列のコードです。以下のコードは機能していません:(

//Instantiating a Workbook object
Workbook workbook = new Workbook();

//Accessing the added worksheet in the Excel file
int sheetIndex = workbook.getWorksheets().add();
Worksheet worksheet = workbook.getWorksheets().get(sheetIndex);

Cells cells = worksheet.getCells();
Column column = cells.getColumns().get(30);

Style style =column.getStyle();
style.setHorizontalAlignment(TextAlignmentType.CENTER);
style.setCustom("$#,##0;[Red]$-#,##0");

StyleFlag styleFlag = new StyleFlag();

//Applying the style to the column
column.applyStyle(style, styleFlag);

//Saving the modified Excel file in default format
workbook.save("C:\\output.xls");

誰でもこれを手伝ってもらえますか。

4

1 に答える 1

0

StyleFlag オブジェクトに関連する書式設定オプションを設定する必要があります。正常に動作する更新されたサンプル コードを参照してください。例: サンプル コード:

 //Instantiating a Workbook object
        Workbook workbook = new Workbook();

        //Accessing the added worksheet in the Excel file
        int sheetIndex = workbook.getWorksheets().add();
        Worksheet worksheet = workbook.getWorksheets().get(sheetIndex);

        Cells cells = worksheet.getCells();
        Column column = cells.getColumns().get(30);

        Style style1 = column.getStyle();
        style1.setHorizontalAlignment(TextAlignmentType.CENTER);
        style1.setCustom("$#,##0;[Red]$-#,##0");

        StyleFlag styleFlag1 = new StyleFlag();
        styleFlag1.setNumberFormat(true);   
        styleFlag1.setHorizontalAlignment(true);    

        //Applying the style to the column
        column.applyStyle(style1, styleFlag1);

        //Saving the modified Excel file in default format
        workbook.save("C:\\output.xls");

私は Aspose の開発者エバンジェリストです。

于 2015-07-16T10:55:01.597 に答える