简体   繁体   English

在java poi 4.1.2中从excel表中读取国际货币符号

[英]Read International Currency symbols from excel sheet in java poi 4.1.2

I am unable to read international currency and accounting number format from excel sheet into java standalone application using poi 4.1.2 (XSSF).我无法使用 poi 4.1.2 (XSSF) 将国际货币和会计编号格式从 Excel 工作表读取到 java 独立应用程序中。 Only able to read US locale symbol but unable to read other currency symbols in java.只能读取美国语言环境符号,但无法读取 java 中的其他货币符号。 Using currency symbol only some formats cell value is displayed in java DataFormatter, other formats are displayed with ?(Eg: I/P: $10.00 O/P: ?10.00).使用货币符号仅某些格式的单元格值显示在 java DataFormatter 中,其他格式显示为?(例如:I/P:$10.00 O/P:?10.00)。

Accounting Number format unable to read Euro currency symbol(Exception: Illegal Argument Exception) and some currency symbols displaying cell data(currency symbol and value).会计编号格式无法读取欧元货币符号(例外:非法参数例外)和一些显示单元格数据的货币符号(货币符号和值)。 code:代码:

for(int i=1;i<=rows;i++){ for(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;
                    }       
                }
            }

} }

Exception:例外:

  1. € 100.00 - Euro € 100.00 - 欧元
    from Excel sheet Accounting-> currency type as Euro -> after running class I am getting below exception.从 Excel 表会计-> 货币类型为欧元-> 在运行课程后我得到以下异常。

org.apache.poi.ss.format.CellFormat WARNING: Invalid format: "_ [$€-2]\\ * #,##0.00_ ;" org.apache.poi.ss.format.CellFormat 警告:格式无效:“_ [$€-2]\\ * #,##0.00_;” java.lang.IllegalArgumentException: Unsupported [] format block '[' in '_ [$€-2]\\ * #,##0.00_' with c2: null java.lang.IllegalArgumentException: Unsupported [] format block '[' in '_ [$€-2]\\ * #,##0.00_' with c2: null

Similarly, I am getting exception for some other Accounting format currency symbols also.同样,我也遇到了其他一些会计格式货币符号的例外情况。 Input (Excel sheet)-输入(Excel表)- 在此处输入图片说明 output(Java) - 100输出(Java) - 100

Excel sheet(info.xlsx): Excel表(info.xlsx): 在此处输入图片说明

Output should display without exception and with cell data(symbol and numeric value) in java.输出应无一例外地显示,并在 java 中显示单元格数据(符号和数值)。

There really seems to be a bug in DataFormatter when currency € (EURO) is used with € sign placed in front of the value like € 1,234.56 .当使用货币 € (EURO) 并将 € 符号放置在值之前,例如€ 1,234.56时, DataFormatter似乎确实存在错误。 All default Euro using states normally don't do so.所有默认使用欧元的国家通常都不会这样做。 They are writing currency like 1,234.56 € , so € sign placed behind the value.他们写的货币是1,234.56 € ,所以 € 符号放在价值后面。

If the Number format Currency or Accounting using symbol € Euro (€ 123) is used in Excel , then Excel creates a number format like [$€-2]\\ #,##0.00 .如果在Excel使用使用符号€ Euro (€ 123)的数字格式货币或会计,则Excel会创建类似[$€-2]\\ #,##0.00的数字格式。 That means currency symbol € placed in front of the value.这意味着货币符号 € 放在值前面。 But apache poi interprets that 2 as a country code like in [$£-809]#,##0.00 where the 809 means Great Britain.但是apache poi2解释为国家代码,如[$£-809]#,##0.00中的809表示英国。 So because of that misinterpretation it fails since it does not find a country for code 2 .因此,由于这种误解,它失败了,因为它没有找到代码2的国家/地区。

A workaround could be replacing all "[$\€-2]" in data-format string with "\€".解决方法可能是将数据格式字符串中的所有“[$\€-2]”替换为“\€”。 That is replacing all [$€-2] with only the sign.那就是用符号替换所有[$€-2]

Example:例子:

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();
 }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM