读取单元格Java POI null错误

时间:2012-05-20 18:04:52

标签: java apache-poi

我正在尝试在电子表格中找到包含文本“期间销售报告”的单元格,单元格中有更多文本,但“期间的销售报告”< / em>是它永远不会改变的一部分,它允许我在每个电子表格中找到特定的单元格。

当我尝试读取该单元格的值时,我得到一个空值。

这是该类的代码:

package excelreader;

import javax.swing.JOptionPane;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.ss.usermodel.Cell;

/**
 *
 * @author LuisLaptop
 */
public class FileInfo {

    // attributes

    private String date;
    private String fileName;
    private String reportCreator;
    private String sheetName;


    // constructor

    public FileInfo(HSSFSheet sheet) {


    }


    // Methods

    public String getSalesPeriod(HSSFSheet sheet) {

        String period = "Sales report for the period";

        HSSFRow row = sheet.getRow(2);
        HSSFCell cell = row.getCell(0);

        if(cell.getCellType() == Cell.CELL_TYPE_STRING)
        {
            if(cell.getStringCellValue().toString().trim().equals(period))
            {
                date = cell.getStringCellValue().toString().trim();
                return date;
            }

        }
        else
        {
            JOptionPane.showMessageDialog(null, "The report has no period", "Period Error", JOptionPane.ERROR_MESSAGE);
        }

        return date;            
    }



    // Getters and setters 

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public String getReportCreator() {
        return reportCreator;
    }

    public void setReportCreator(String reportCreator) {
        this.reportCreator = reportCreator;
    }

    public String getSheetName() {
        return sheetName;
    }

    public void setSheetName(String sheetName) {
        this.sheetName = sheetName;
    }



}

这是主要的:

package excelreader;

import java.io.FileInputStream;
import java.io.IOException;
import javax.swing.JOptionPane;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

/**
 * @author lv10
 */
public class ExcelReader {
    public static void main(String[] args) throws IOException {

        FileManager fm = new FileManager();
        FileInputStream inputFile = new FileInputStream(fm.fileChooser.getSelectedFile());

        HSSFWorkbook wb = new HSSFWorkbook(inputFile);
        HSSFSheet sheet = wb.getSheetAt(0);

        Total t = new Total(sheet);
        FileInfo fi = new FileInfo(sheet);

        String report = "This is a test report"+ t.getTotal() + " from " + fi.getSalesPeriod(sheet) ;

        JOptionPane.showMessageDialog(null, report, "test", JOptionPane.INFORMATION_MESSAGE);

    }
}

这是提供其他值的总类。有了这个课我没有问题,但我添加了它来提供一些背景:

package excelreader;

import java.text.DecimalFormat;
import javax.swing.JOptionPane;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;

/**
 * @author lv10
 */
class Total {

    // -------------------------------------------------------------- Attributes

    String cellContent[] = {"Total US", "Total DE (EUR)", "Total IT (EUR)"};
    private int rownr = 0;
    private int colnr = 0;
    private double total = 0;
    private double sum = 0;

    // ------------------------------------------------------------- Constructor


    public Total(HSSFSheet sheet)
    {
        for( int i=0; i<cellContent.length; i++)
        {
            total = findTotal(sheet, cellContent[i]);
            sum+= total;

        }
    }



    // ----------------------------------------------------------------- Methods 

    public double findTotal(HSSFSheet sheet, String cellContent){

            for(Row rows : sheet){
            for(Cell cell: rows){

                if(cell.getCellType()== Cell.CELL_TYPE_STRING){
                    if(cell.getStringCellValue().toString().trim().equals(cellContent)){
                        rownr = rows.getRowNum();

                        HSSFRow row = sheet.getRow(rownr);
                        colnr = row.getLastCellNum()-1;
                        cell = row.getCell(colnr);

                        return cell.getNumericCellValue();  
                    }
                }
            } // end of cell loop
        } // end of row loop

         return 0;

        }




    // getters and setters

    public double getSum() {
        return sum;
    }

    public void setSum(double sum) {
        this.sum = sum;
    }

    public double getTotal() {
        return total;
    }

    public void setTotal(double total) {
        this.total = total;
    }



}

2 个答案:

答案 0 :(得分:1)

此:

cell.getStringCellValue().toString().trim().equals(period)
如果单元格内容等于period,则

返回true,即正好是"Sales report for the period"

然而,正如你自己所说,“它是部分永远不会改变”;它不是整个内容。

尝试使用indexOf方法,它将返回该子字符串的位置,如果不存在,则使用-1

答案 1 :(得分:1)

我使用contains()

解决了这个问题
if(cell.getStringCellValue().toString().trim().contains(period)) 
{ 
     date = cell.getStringCellValue().toString().trim(); return date; 
}