0
Я пытаюсь форматировать данные столбца для числа. Ниже приведен код, который работает для Single cell.column.applyStyle (style, 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();
//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");
Вот код column.The ниже код не работает :(
//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");
Может кто-нибудь помочь в этом.
Если я добавлю это «styleFlag1.setNumberFormat (true); styleFlag1.setHorizontalAlignment (true);» два, как тогда. должен ли мой код работать нормально? – Ajit
Ну, в вашем коде есть еще одна проблема, поэтому любезно измените следующие строки кода: i.e., Стиль style = column.getStyle(); style.setHorizontalAlignment (TextAlignmentType.CENTER); style.setCustom ("$ #, ## 0; [Red] $ - #, ## 0"); до: Стиль style1 = column.getStyle(); style1.setHorizontalAlignment (TextAlignmentType.CENTER); style1.setCustom ("$ #, ## 0; [Red] $ - #, ## 0"); как вы применяете style1 для метода Column.ApplyStyle(). Вы должны запустить мой обновленный сегмент кода, который в порядке. –
Это всего лишь ошибка типа, я тоже пытался это сделать. – Ajit