gpt4 book ai didi

java - 读取单元格 Java POI null 错误

转载 作者:行者123 更新时间:2023-12-02 07:47:03 26 4
gpt4 key购买 nike

我试图在电子表格中查找包含文本“该期间的销售报告”的单元格,该单元格包含更多文本,但是“该期间的销售报告” 是它永远不会改变的部分,可以让我在每个电子表格上找到特定的单元格。

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

这是该类的代码:

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



}

最佳答案

这个:

cell.getStringCellValue().toString().trim().equals(period)

如果单元格内容等于period,即恰好是“该期间的销售报告”,则返回 true。

但是,正如您自己所说,“这是它的部分永远不会改变”;这不是全部内容。

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

关于java - 读取单元格 Java POI null 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10675840/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com