使用POI将Excel双格类型转换为字符串类型

时间:2019-01-10 13:28:08

标签: java apache-poi xssf

我正在阅读一个Excel,其中有些单元格为double类型。我想阅读并打印为字符串。

我的示例excel文件具有以下数据。

日期|城市|信用|借方|余额|类型 10-01-2019 |新德里| 1000000.00 || 1000000.00 | CR 10-01-2019 |孟买| 50000000.00 | 40000000.00 | 10000000.00 | CR 10-01-2019 | Chennai || 200000.00 | 200000.00 | DR 10-01-2019 | Kolkatta || 10000.00 | 10000.00 | DR 10-01-2019 |斯利那加| 100000000.00 | 40000000.00 | 60000000.00 | CR

我想将双倍的金额打印为字符串。

我尝试了以下代码:

public static void main(String[] args) throws  FileNotFoundException{
        FileInputStream CA = new FileInputStream(new File("C:/Java projects/RBI/RBI.xlsx"));

        public static void main(String[] args) throws  FileNotFoundException{
        FileInputStream CA = new FileInputStream(new File("C:/Java projects/RBI/RBI.xlsx"));

    try
    {
        XSSFWorkbook CAworkbook = new XSSFWorkbook(CA);
        XSSFSheet sheet = CAworkbook.getSheetAt(0);
        Iterator<Row> rowIterator = sheet.iterator();

        ArrayList<String> CA1 = new ArrayList<String>();

        while (rowIterator.hasNext())
        {
            Row row = rowIterator.next();
            Iterator<Cell> cellIterator = row.cellIterator();
            CA1.add( row.getCell(1) + "|" +row.getCell(2)+ "|" +row.getCell(3)+ "|" + row.getCell(4)+ "|" +row.getCell(5)+ "|" + row.getCell(6));   
            System.out.println( row.getCell(1) + "|" +row.getCell(2)+ "|" +row.getCell(3)+ "|" + row.getCell(4)+ "|" +row.getCell(5)+ "|" + row.getCell(6)); 
        }

    }
    catch (IOException e) {
            e.printStackTrace();
        }

}

输出如下:-

2019年1月10日|新德里| 1000000.0 || 1000000.0 | CR 10-01-2019 |孟买| 5.0E7 | 4.0E7 | 1.0E7 | CR 10-01-2019 | Chennai || 200000.0 | 200000.0 | DR 10-01-2019 | Kolkatta || 10000.0 | 10000.0 | DR 10-01-2019 |斯利那加| 1.0E8 | 4.0E7 | 6.0E7 | CR

3 个答案:

答案 0 :(得分:0)

如果问题是您获得的双打形式,则只需使用String.format("%.2f", row.getCell(n))而不是row.getCell(n) 这不是POI framework的问题,而是Java数据格式的问题。请关注thisthis以获取有关该主题的更多信息

UPD 实际上,row.getCell(n)返回一个XSSFCell实例,因此要正确而显式地获取一个值,您应该使用row.getCell(n).getNumericCellValue()方法。或者,您可以直接调用row.getCell(n).getStringCellValue()直接获取字符串值

答案 1 :(得分:0)

String中制作一个double的方法有几种,但是并非所有方法都总是保留常规的十进制表示形式。

我将使用您的第一个数字(以指数表示形式显示)作为示例数字。

public static void main(String[] args) {
    // your number that causes problems
    double a = 100000000.00;
    // just printing it will give you 1.0E8, not desired
    System.out.println(a);
    // using String.valueOf will output 1.0E8, not desired
    System.out.println(String.valueOf(a));
    // same thing if you box it and use toString(): 1.0E8, not desired
    System.out.println(new Double(a).toString());
    /* 
     * first operation that comes nearer to the desired output
     * gives 100000000.000000, better, but not desired
     */
    System.out.println(String.format("%f", a));
    // applying a specific formatting...
    DecimalFormat decimalFormat = new DecimalFormat("#.00#");
    // ... will give you the output you want to have, which is 100000000.00
    System.out.println(decimalFormat.format(a));
}

结论是在创建String的{​​{1}}表示形式之前,请应用所需的格式。 我的建议是一次创建一个double(甚至可以作为类属性或常量),并在创建DecimalFormat变量以用于后续操作时将其应用于单元格的迭代中。这样,您不必重复调用单元迭代器,从而使代码更易于阅读:

String
  

重要:    DecimalFormat decimalFormat = new DecimalFormat("#.00#") while (rowIterator.hasNext()) { Row row = rowIterator.next(); Iterator<Cell> cellIterator = row.cellIterator(); String date = row.getCell(1); String city = row.getCell(2); // now apply the DecimalFormat String credit = decimalFormat.format(row.getCell(3)); String debit = decimalFormat.format(row.getCell(4)); String balance = decimalFormat.format(row.getCell(5)); String type = row.getCell(6); /* * also you might want to have a single String concatenation * of the other Strings if you use it more than once */ String line = date + "|" + city + "|" + credit + "|" + debit + "|" + balance + "|" + type; CA1.add(line); System.out.println(line); } 似乎取决于系统默认的十进制表示形式,因为它在我的IDE(欧洲Windows安装)上输出100000000,00(注意逗号)。

答案 2 :(得分:0)

简单的解决方案是

BigDecimal.valueOf( cell.getNumericCellValue()).toPlainString();