阅读货币和使用Java的百分比 [英] Read currency & % using Java

查看:58
本文介绍了阅读货币和使用Java的百分比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Apache Poi(v3.11/XSSFWorkbook)读取Excel文件.我正在阅读特定的工作表{e.x.sheet(0)},它是由同一个文件中大约15张纸中的不同宏组成的.问题是我能够读取未格式化的字符串数据,但无法读取货币或%之类的格式化数据.

I am trying to read an excel file using Apache Poi (v3.11 / XSSFWorkbook ). I am reading a particular sheet {e.x. sheet(0)} which is formed by different macros from around 15 sheets from the same file. The issue is I am able to read the non formatted String data but unable to read formatted data like currency or %.

我还是无法控制excel工作表.我将从网络共享路径中读取文件.

I do not control the excel worksheet anyway. I will be reading the file from a network share path.

XSSFWorkbook myWorkBook = null
    XSSFSheet mySheet = null;
    Row rowHeader = null;
    Row rowData = null;
    FileInputStream fis = new FileInputStream(
            new File(
                    "inputfile.xlsx"));
    myWorkBook = new XSSFWorkbook(fis);
    mySheet = myWorkBook.getSheetAt(0);
    rowHeader = mySheet.getRow(0);
    rowData = mySheet.getRow(1);



        for (Cell s : rowHeader) {  
                System.out.print("|  "+s.getStringCellValue());
                System.out.println(" -->"+getCellValue(myWorkBook,rowData.getCell(s.getColumnIndex())));
    }

    myWorkBook.close();
}
//   $#,##0.00

private static String getCellValue(XSSFWorkbook wb, Cell cell)
{
   XSSFCell x = (XSSFCell)cell;
   return x.getRawValue();
}
}

我有一个如下所示的Excel工作表

I have a excel sheet like below

Name    Amount    Rate
Name1   $1,500  75%

如果我使用此代码阅读了以上表格

If I read the above sheet using this code

名称->名称1

金额-> 1500(请注意缺少的$和)

Amount --> 1500 ( note the missing $ and , )

Rate-> 74.99999998(数字不准确且缺少%符号)

Rate --> 74.99999998 (number inacurate and missing % symbol)

推荐答案

感谢@Gagravarr

Thanks @Gagravarr

下面的代码解决了这个问题

this below code solved the issue

private static String getCellValue(XSSFWorkbook wb, Cell cell)
{
  DataFormatter formatter = new DataFormatter();
  String formattedCellValue = formatter.formatCellValue(cell);
  return formattedCellValue; 
}

这篇关于阅读货币和使用Java的百分比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆