我无法使用POI4.1.2 (XSSF)从excel表读取国际货币和会计编号格式到java独立应用程序。只能读取美国语言环境符号,但无法在java中读取其他货币符号。使用货币符号,java DataFormatter中只显示一些格式单元格值,其他格式显示在?(例如: I/P:$10.00 O/P:?10.00)。
会计编号格式无法读取欧元货币符号(例外:非法参数异常)和一些显示单元格数据的货币符号(货币符号和值)。代码:
(int i=1;i<=rows;i++){
String ExcelFilename = sheet.getRow(i).getCell(<cell no of file>).getRichStringCellValue().getString().trim();
if(ExcelFilename.equals("<file name>")) {
for(int j=0;j<columns;j++) {
DataFormatter formatter = new DataFormatter();
cell = sheet.getRow(i).getCell(j,Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
String check= "";
switch (cell.getCellType()) {
case BLANK:
check= formatter.formatCellValue(cell);
break;
case NUMERIC:
check= formatter.formatCellValue(cell);
break;
case BOOLEAN:
check =formatter.formatCellValue(cell);
break;
case STRING:
check=formatter.formatCellValue(cell);
break;
case FORMULA:
check= formatter.formatCellValue(cell);
break;
default:
break;
}
}
}}
例外:
从Excel会计->货币类型作为欧元->在运行类后,我得到了以下异常.
org.apache.poi.ss.format.CellFormat警告:无效格式:"_ $Euro-2\* #,##0.00_ ;“java.lang.IllegalArgumentException:不支持的[]格式块'[‘在'_ $Euro-2\* #,##0.00_’与c2: null
类似地,我也得到了一些其他会计格式货币符号的例外。输入(Excel表格)-

输出(Java)- 100
Excel工作表(info.xlsx):

输出应该毫无例外地以java中的单元数据(符号和数值)显示。
发布于 2021-01-10 09:16:20
当货币欧元(欧元)和欧元的标志放在DataFormatter (如€ 1,234.56 )的前面时,似乎确实存在一个缺陷。所有使用默认欧元的国家通常都不会这么做。他们正在写像1,234.56 €这样的货币,所以欧元符号被放在了价值的后面。
如果数字格式货币或使用符号€ Euro (€ 123)的会计在Excel中使用,那么Excel将创建类似于[$€-2]\ #,##0.00的数字格式。这意味着货币符号欧元放在价值的前面。但apache poi将2解释为一种国家代码,就像[$£-809]#,##0.00中809的意思是大不列颠。因此,由于这种误解,它失败了,因为它找不到代码2的国家。
解决办法可能是将数据格式字符串中的所有“$\\u20ac-2”替换为"\u20AC“。这意味着将所有[$€-2]替换为€符号。
示例:
import org.apache.poi.ss.usermodel.*;
import java.io.FileInputStream;
import java.io.PrintWriter;
class ReadExcelUsingDataFormatter {
public static void main(String[] args) throws Exception {
Workbook workbook = WorkbookFactory.create(new FileInputStream("ExcelExample.xlsx"));
DataFormat format = workbook.createDataFormat();
DataFormatter dataFormatter = new DataFormatter();
PrintWriter writer = new PrintWriter("Result.txt", "UTF-8");
Sheet sheet = workbook.getSheetAt(0);
for (Row row : sheet) {
for (Cell cell : row) {
try {
String dataFormatString = cell.getCellStyle().getDataFormatString();
if (dataFormatString.contains("[$\u20AC-2]")) {
System.out.println(dataFormatString);
dataFormatString = dataFormatString.replace("[$\u20AC-2]", "\u20AC");
System.out.println(dataFormatString);
cell.getCellStyle().setDataFormat(format.getFormat(dataFormatString));
}
String value = dataFormatter.formatCellValue(cell);
System.out.println(value); //This might not be printed correctly because of unicode deficiency of `System.out`. But `Result.txt` should contain it corrcetly.
writer.print(value + "\t\t");
} catch (Exception ex) {
ex.printStackTrace();
}
}
writer.println();
}
writer.close();
workbook.close();
}
}https://stackoverflow.com/questions/65599926
复制相似问题