作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试创建一个 excel 文件(.xlsx)并使用 Apache POI 用数据填充它,并将一些单元格格式化为会计格式,使用:
CellStyle CellAccounting = spreadsheet.getWorkbook().createCellStyle();
CreationHelper ch = workbook.getCreationHelper();
CellAccounting.setDataFormat(ch.createDataFormat().getFormat("_ [$€-nl-NL] * #,##0.00_ ;_ [$€-nl-NL] * -#,##0.00_ ;_ [$€-nl-NL] * \"-\"??_ ;_ @_ "));
Feb 18, 2020 1:40:10 AM org.apache.poi.ss.format.CellFormat <init>
WARNING: Invalid format: "_ [$€-nl-NL] * "-"??_ ;"
java.lang.IllegalArgumentException: Unsupported [] format block '[' in '_ [$€-nl-NL] * "-"??_' with c2: null
at org.apache.poi.ss.format.CellFormatPart.formatType(CellFormatPart.java:373)
at org.apache.poi.ss.format.CellFormatPart.getCellFormatType(CellFormatPart.java:287)
at org.apache.poi.ss.format.CellFormatPart.<init>(CellFormatPart.java:191)
at org.apache.poi.ss.format.CellFormat.<init>(CellFormat.java:189)
at org.apache.poi.ss.format.CellFormat.getInstance(CellFormat.java:163)
at org.apache.poi.ss.usermodel.DataFormatter.getFormat(DataFormatter.java:343)
at org.apache.poi.ss.usermodel.DataFormatter.getFormat(DataFormatter.java:309)
at org.apache.poi.ss.usermodel.DataFormatter.getFormattedNumberString(DataFormatter.java:868)
at org.apache.poi.ss.usermodel.DataFormatter.formatCellValue(DataFormatter.java:1021)
at org.apache.poi.ss.usermodel.DataFormatter.formatCellValue(DataFormatter.java:971)
at org.apache.poi.ss.util.SheetUtil.getCellWidth(SheetUtil.java:170)
at org.apache.poi.ss.util.SheetUtil.getColumnWidthForRow(SheetUtil.java:300)
at org.apache.poi.ss.util.SheetUtil.getColumnWidth(SheetUtil.java:254)
at org.apache.poi.ss.util.SheetUtil.getColumnWidth(SheetUtil.java:233)
at org.apache.poi.xssf.usermodel.XSSFSheet.autoSizeColumn(XSSFSheet.java:555)
at org.apache.poi.xssf.usermodel.XSSFSheet.autoSizeColumn(XSSFSheet.java:537)
at MyClass.createExcel(MyClass.java:325)
at MyClass.saveFile(MyClass.java:82)
at ClickListener.actionPerformed(ClickListener.java:95)
at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1967)
at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2308)
at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279)
at java.desktop/java.awt.Component.processMouseEvent(Component.java:6636)
at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3342)
at java.desktop/java.awt.Component.processEvent(Component.java:6401)
at java.desktop/java.awt.Container.processEvent(Container.java:2263)
at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5012)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2321)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4844)
at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4918)
at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4547)
at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4488)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2307)
at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2762)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4844)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:743)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742)
at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
spreadsheet.autoSizeColumn(1);
最佳答案
Excel
本身没有使用像 nl-NL
这样的数字格式的语言标签。相反,它使用 Windows Language Code Identifier (LCID) 。这是 413
的 nl-NL
。Sheet.autoSizeColumn
需要知道数字格式来计算所需的列宽。因此,它尝试解析您的数字格式,其中包含 [$€-nl-NL]
并失败。使用 [$€-413]
,也是一样的,会成功的。
以下对我有用:
import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
class CreateExcelCellAccountingStyle {
public static void main(String[] args) throws Exception {
try (Workbook workbook = new XSSFWorkbook();
FileOutputStream fileout = new FileOutputStream("Excel.xlsx") ) {
Object[][] data = new Object[][] {
new Object[] {"Text", "Value"},
new Object[] {"Value 1", 123.456789},
new Object[] {"Value 2", 1234.56789}
};
DataFormat dataFormat = workbook.createDataFormat();
CellStyle cellAccountingStyle = workbook.createCellStyle();
//cellAccountingStyle.setDataFormat(dataFormat.getFormat("_ [$€-nl-NL] * #,##0.00_ ;_ [$€-nl-NL] * -#,##0.00_ ;_ [$€-nl-NL] * \"-\"??_ ;_ @_ "));
cellAccountingStyle.setDataFormat(dataFormat.getFormat("_ [$€-413] * #,##0.00_ ;_ [$€-413] * -#,##0.00_ ;_ [$€-413] * \"-\"??_ ;_ @_ "));
Sheet sheet = workbook.createSheet();
for (int r = 0; r < data.length; r++) {
Row row = sheet.createRow(r);
for (int c = 0; c < data[0].length; c++) {
Cell cell = row.createCell(c);
if (data[r][c] instanceof String) {
cell.setCellValue((String)data[r][c]);
} else if (data[r][c] instanceof Double) {
cell.setCellValue((Double)data[r][c]);
cell.setCellStyle(cellAccountingStyle);
}
}
}
for (int c = 0; c < data[0].length; c++) {
sheet.autoSizeColumn(c);
}
workbook.write(fileout);
}
}
}
关于java - Apache POI - CellDataFormat 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60272283/
我正在尝试创建一个 excel 文件(.xlsx)并使用 Apache POI 用数据填充它,并将一些单元格格式化为会计格式,使用: CellStyle CellAccounting = sp
我是一名优秀的程序员,十分优秀!