Java 使用POI获取与excel中完全相同的数据或内容

Java 使用POI获取与excel中完全相同的数据或内容,java,excel,apache-poi,Java,Excel,Apache Poi,下面是我的方法的一部分,它在获取CellFormat实例时导致下面给出的异常。请给出提取excel文件中显示的数据的任何解决方案 try { String dataStr = null; style = cell.getCellStyle(); dataStr = style.getDataFormatString(); CellFormat cf = CellFormat.getInstance(da

下面是我的方法的一部分,它在获取CellFormat实例时导致下面给出的异常。请给出提取excel文件中显示的数据的任何解决方案

   try
      {
         String dataStr = null;
         style = cell.getCellStyle();
         dataStr = style.getDataFormatString();
           CellFormat cf = CellFormat.getInstance(dataStr);
            if(cf != null)
                {
                  CellFormatResult result = cf.apply(cell); // -> this is the line which exactly giving exception.
                   if(!result.text.equals(EMPTY))
                        content = result.text;
                 }
                 }
                  catch(Exception e)
                 { 
                   System.out.println("row " + i + " column " + j);
                               e.printStackTrace();
                 }

 java.lang.NullPointerException
            at org.apache.poi.ss.format.CellElapsedFormatter.<init>(CellElapsedFormatter.java:143)
            at org.apache.poi.ss.format.CellFormatType$4.formatter(CellFormatType.java:60)
            at org.apache.poi.ss.format.CellFormatPart.getFormatter(CellFormatPart.java:280)
            at org.apache.poi.ss.format.CellFormatPart.<init>(CellFormatPart.java:172)
            at org.apache.poi.ss.format.CellFormat.<init>(CellFormat.java:157)
            at org.apache.poi.ss.format.CellFormat.getInstance(CellFormat.java:133)
            at com.pawaa.conversion.exceltohtml.ExcelToHtmlConverter.printSheetContentImages(ExcelToHtmlConverter.java:572)
试试看
{
字符串dataStr=null;
style=cell.getCellStyle();
dataStr=style.getDataFormatString();
CellFormat cf=CellFormat.getInstance(dataStr);
如果(cf!=null)
{
CellFormatResult=cf.apply(cell);//->这一行正好给出了异常。
如果(!result.text.equals(空))
content=result.text;
}
}
捕获(例外e)
{ 
系统输出打印项次(“行”+i+“列”+j);
e、 printStackTrace();
}
java.lang.NullPointerException
位于org.apache.poi.ss.format.CellElapsedFormatter。(CellElapsedFormatter.java:143)
位于org.apache.poi.ss.format.CellFormatType$4.formatter(CellFormatType.java:60)
位于org.apache.poi.ss.format.CellFormatPart.getFormatter(CellFormatPart.java:280)
位于org.apache.poi.ss.format.CellFormatPart。(CellFormatPart.java:172)
位于org.apache.poi.ss.format.CellFormat.(CellFormat.java:157)
在org.apache.poi.ss.format.CellFormat.getInstance(CellFormat.java:133)上
位于com.pawaa.conversion.exceltohtml.ExcelToHtmlConverter.printSheetContentImages(ExcelToHtmlConverter.java:572)
实际上,当我调试时,我在CellFormat类的方法中发现了异常

调用cf.apply(cell)方法时,我得到了NullPointerException,因为posNumFmt实例变量为null

     private CellFormatPart getApplicableFormatPart(Object value) {

    if (value instanceof Number) {

        double val = ((Number) value).doubleValue();

        if (formatPartCount == 1) {
            if (!posNumFmt.hasCondition()
                    || (posNumFmt.hasCondition() && posNumFmt.applies(val))) {
                return posNumFmt;
            } else {
                return new CellFormatPart("General");
            }
        } else if (formatPartCount == 2) {
            if ((!posNumFmt.hasCondition() && val >= 0)
                    || (posNumFmt.hasCondition() && posNumFmt.applies(val))) {
                return posNumFmt;
            } else if (!negNumFmt.hasCondition()
                    || (negNumFmt.hasCondition() && negNumFmt.applies(val))) {
                return negNumFmt;
            } else {
                // Return ###...### (255 #s) to match Excel 2007 behaviour
                return new CellFormatPart(QUOTE + INVALID_VALUE_FOR_FORMAT + QUOTE);
            }
        } else {
            if ((!posNumFmt.hasCondition() && val > 0)
                    || (posNumFmt.hasCondition() && posNumFmt.applies(val))) {
                return posNumFmt;
            } else if ((!negNumFmt.hasCondition() && val < 0)
                    || (negNumFmt.hasCondition() && negNumFmt.applies(val))) {
                return negNumFmt;
            // Only the first two format parts can have conditions
            } else {
                return zeroNumFmt;
            }
        }
    } else {
        throw new IllegalArgumentException("value must be a Number");
    }

}
private CellFormatPart GetApplicatableFormatPart(对象值){
if(值instanceof Number){
double val=((数字)值).doubleValue();
如果(formatPartCount==1){
如果(!posNumFmt.hasCondition())
||(posNumFmt.hasCondition()&&posNumFmt.applied(val))){
返回posNumFmt;
}否则{
返回新的CellFormatPart(“通用”);
}
}else if(formatPartCount==2){
如果((!posNumFmt.hasCondition()&&val>=0)
||(posNumFmt.hasCondition()&&posNumFmt.applied(val))){
返回posNumFmt;
}如果(!negNumFmt.hasCondition()
||(negNumFmt.hasCondition()&&negNumFmt.applies(val))){
返回negNumFmt;
}否则{
//返回############(255#s)以匹配Excel 2007行为
返回新的CellFormatPart(QUOTE+无效的\u值\u,对于\u格式+QUOTE);
}
}否则{
如果(!posNumFmt.hasCondition()&&val>0)
||(posNumFmt.hasCondition()&&posNumFmt.applied(val))){
返回posNumFmt;
}如果(!negNumFmt.hasCondition()&&val<0),则为else
||(negNumFmt.hasCondition()&&negNumFmt.applies(val))){
返回negNumFmt;
//只有前两个格式部分可以有条件
}否则{
返回zeroNumFmt;
}
}
}否则{
抛出新的IllegalArgumentException(“值必须是数字”);
}
}

CellElapsedFormatter.java:143的代码是什么?我编辑了代码并提到了出现异常的那一行,很抱歉,我忘记了,ExcelToHtmlConverter类是我的剩余类或所有poi类。您的实际问题是
ExcelToHtmlConverter.java:572
。上面的代码是否包含该行?调试并查看
style
style.getDataFormatString()
是否为
null
。我觉得
style
为空。如果改为使用会发生什么?