我想在 Apache-POI(XSSF 和 SAX 事件 API)的帮助下导入 XLSX 文件。
由于 Excel 将数字存储为浮点数,因此需要在 java 中将它们格式化回它们在 Excel 中的原始外观。这可以通过读取单元格格式来实现:
String cellStyle = sheetReader.getAttributeValue(null, "s");
if (cellStyle != null) {
// save the format of the cell for later use.
int styleIndex = Integer.parseInt(cellStyle);
XSSFCellStyle style = stylesTable.getStyleAt(styleIndex);
formatIndex = style.getDataFormat();
formatString = style.getDataFormatString();
if (formatString == null) {
// formatString could not be found, so it must be a builtin format.
formatString = BuiltinFormats.getBuiltinFormat(formatIndex);
}
}
...
// format the floating-point value
String xlsxValue …Run Code Online (Sandbox Code Playgroud)