Java Apache POI无法使代码正常工作
编辑-感谢您的回复。已经进行了更改,并根据建议发现问题是现在空行上的问题 我正在编写一个程序,加载各种excel表格,并根据某些标准提供输出。请参阅下面的代码。我的问题是代码没有写入或保存到显示页。我意识到下面的代码不是特别整洁,但是我粘贴了副本,以检查我是否可以保存任何内容。如果我注释掉循环中的所有内容,并在保存之前尝试写入最后的单元格,它将工作并显示在工作表中。如果我不注释掉数据格式化程序部分,即使在保存输出文件的部分之前尝试写入单元格,它也不会工作。我猜问题是由数据格式化程序引起的,但我不知道为什么Java Apache POI无法使代码正常工作,java,apache-poi,Java,Apache Poi,编辑-感谢您的回复。已经进行了更改,并根据建议发现问题是现在空行上的问题 我正在编写一个程序,加载各种excel表格,并根据某些标准提供输出。请参阅下面的代码。我的问题是代码没有写入或保存到显示页。我意识到下面的代码不是特别整洁,但是我粘贴了副本,以检查我是否可以保存任何内容。如果我注释掉循环中的所有内容,并在保存之前尝试写入最后的单元格,它将工作并显示在工作表中。如果我不注释掉数据格式化程序部分,即使在保存输出文件的部分之前尝试写入单元格,它也不会工作。我猜问题是由数据格式化程序引起的,但我不
public void checkForChanges() {
try {
FileInputStream fsIP = new FileInputStream("Change.xls");
HSSFWorkbook fWorkbook = new HSSFWorkbook(fsIP);
HSSFSheet recipeSheet = fWorkbook.getSheet("RECIPE STEPS");
HSSFSheet fromSheet = fWorkbook.getSheet("FROM FORMAT");
HSSFSheet toSheet = fWorkbook.getSheet("TO FORMAT");
HSSFSheet displaySheet = fWorkbook.getSheet("DISPLAY");
for (int i = 0; i < 30; i++) {
DataFormatter recipeFormatter = new DataFormatter();
HSSFRow recipeRow = recipeSheet.getRow(i);
HSSFCell recipeCellsColumnA = recipeRow.getCell(0);
String recipeCellValueColumnA = recipeFormatter.formatCellValue(recipeCellsColumnA);
System.out.println(recipeCellValueColumnA);
HSSFCell recipeCellsColumnB = recipeRow.getCell(1);
String recipeCellValueColumnB = recipeFormatter.formatCellValue(recipeCellsColumnB);
System.out.println(recipeCellValueColumnB);
DataFormatter fromFormatter = new DataFormatter();
HSSFRow fromRow = fromSheet.getRow(i);
HSSFCell fromCells = fromRow.getCell(0);
String fromCellValue = fromFormatter.formatCellValue(fromCells);
System.out.println(fromCellValue);
DataFormatter toFormatter = new DataFormatter();
HSSFRow toRow = toSheet.getRow(i);
HSSFCell toCells = toRow.getCell(0);
String toCellValue = toFormatter.formatCellValue(toCells);
System.out.println(toCellValue);
if (recipeCellValueColumnB.equals("YES") && !fromCellValue.equals(toCellValue)) {
System.out.println("PUT VALUE FROM 'TO FORMAT' COLUMN A ROW I INTO 'DISPLAY' SHEET CELL COLUMN B ROW I");
System.out.println("PUT VALUE FROM 'RECIPE STEPS' COLUMN A ROW I INTO 'DISPLAY' SHEET CELL COLUMN A ROW I");
Row row = displaySheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("TEST");
} else if (recipeCellValueColumnB.equals("YES") && fromCellValue.equals(toCellValue)) {
System.out.println("SET CELL IN 'DISPLAY' SHEET COLUMN A ROW I TO '' ");
System.out.println("SET CELL IN 'DISPLAY' SHEET COLUMN B ROW I TO '' ");
Row row = displaySheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("TEST");
} else if (recipeCellValueColumnB.equals("NO")) {
System.out.println("PUT VALUE FROM 'TO FORMAT' COLUMN A ROW I INTO 'DISPLAY' SHEET CELL COLUMN B ROW I");
System.out.println("PUT VALUE FROM 'RECIPE STEPS' COLUMN A ROW I INTO 'DISPLAY' SHEET CELL COLUMN A ROW I");
Row row = displaySheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("TEST");
}else if (recipeCellValueColumnA.equals("Step Name") && recipeCellValueColumnB.equals("Always Compare?")) {
System.out.println("SET CELL IN DISPLAY COLUMN A ROW I TO 'REQUIRED STEPS'");
System.out.println("PUT VALUE FROM 'TO FORMAT' COLUMN A ROW I INTO 'DISPLAY' SHEET CELL COLUMN B ROW I");
Row row = displaySheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("TEST");
}
}
FileOutputStream output_file = new FileOutputStream(new File("Change.xls"));
BufferedOutputStream bos = new BufferedOutputStream(output_file);
fWorkbook.write(output_file);
fWorkbook.close();
bos.close();
output_file.close();
}catch(Exception e){
}
}
public void checkForChanges(){
试一试{
FileInputStream fsIP=新的FileInputStream(“Change.xls”);
HSSF工作手册=新的HSSF工作手册(fsIP);
HSSFSheet recipeSheet=fWorkbook.getSheet(“配方步骤”);
HSSFSheet fromSheet=fWorkbook.getSheet(“来自格式”);
HSSFSheet toSheet=fWorkbook.getSheet(“格式化”);
HSSFSheet displaySheet=fWorkbook.getSheet(“显示”);
对于(int i=0;i<30;i++){
DataFormatter recipeFormatter=新的DataFormatter();
HSSFRow recipeRow=recipeSheet.getRow(i);
HSSFCell recipeCellsColumnA=recipeRow.getCell(0);
字符串recipeCellValueColumnA=recipeFormatter.formatCellValue(recipeCellsColumnA);
System.out.println(recipeCellValueColumnA);
HSSFCell recipeCellsColumnB=recipeRow.getCell(1);
字符串recipeCellValueColumnB=recipeFormatter.formatCellValue(recipeCellsColumnB);
System.out.println(recipeCellValueColumnB);
DataFormatter fromFormatter=新的DataFormatter();
HSSFRow fromRow=fromSheet.getRow(i);
HSSFCell fromCells=fromRow.getCell(0);
字符串fromCellValue=fromFormatter.formatCellValue(fromCells);
System.out.println(fromCellValue);
DataFormatter toFormatter=新的DataFormatter();
HSSFRow toRow=toSheet.getRow(i);
HSSFCell-toCells=toRow.getCell(0);
字符串toCellValue=toFormatter.formatCellValue(toCells);
系统输出打印项次(toCellValue);
如果(recipeCellValueColumnB.equals(“YES”)&&!fromCellValue.equals(toCellValue)){
System.out.println(“将值从'TO FORMAT'列A行I放入'DISPLAY'表单元格列B行I”);
System.out.println(“将‘配方步骤’列A行I中的值放入‘显示’表单元格列A行I”);
Row Row=displaySheet.createRow(0);
Cell Cell=row.createCell(0);
cell.setCellValue(“测试”);
}else if(recipeCellValueColumnB.equals(“是”)和&fromCellValue.equals(toCellValue)){
System.out.println(“将“显示”工作表列A行I中的单元格设置为“””;
System.out.println(“将“显示”表B列第I行中的单元格设置为“””;
Row Row=displaySheet.createRow(0);
Cell Cell=row.createCell(0);
cell.setCellValue(“测试”);
}否则如果(recipeCellValueColumnB.equals(“否”)){
System.out.println(“将值从'TO FORMAT'列A行I放入'DISPLAY'表单元格列B行I”);
System.out.println(“将‘配方步骤’列A行I中的值放入‘显示’表单元格列A行I”);
Row Row=displaySheet.createRow(0);
Cell Cell=row.createCell(0);
cell.setCellValue(“测试”);
}否则如果(recipeCellValueColumnA.equals(“步骤名称”)&recipeCellValueColumnB.equals(“始终比较?”){
System.out.println(“将显示列A行I中的单元格设置为“所需步骤”);
System.out.println(“将值从'TO FORMAT'列A行I放入'DISPLAY'表单元格列B行I”);
Row Row=displaySheet.createRow(0);
Cell Cell=row.createCell(0);
cell.setCellValue(“测试”);
}
}
FileOutputStream output_file=newfileoutputstream(新文件(“Change.xls”));
BufferedOutputStream bos=新的BufferedOutputStream(输出文件);
写入(输出_文件);
fWorkbook.close();
bos.close();
output_file.close();
}捕获(例外e){
}
}
对不起,解释得不好,已经很晚了,我又累又沮丧
谢谢您的代码存在以下问题:
catch(Exception e)
{
}
这是说“不要告诉我任何例外情况”。这是异常挤压。。。这是可怕的,懒惰的,完全是错误的
在代码中的某个地方,可能有某种东西直接抛出异常,或者导致POI(或某些东西)抛出异常。那是一只虫子。但是你可怕的异常挤压是扔掉证据,这将允许你识别并修复错误
我猜问题是由数据格式化程序引起的,但我不知道为什么
public void checkForChanges() {
try {
FileInputStream fsIP = new FileInputStream("Change.xls");
HSSFWorkbook fWorkbook = new HSSFWorkbook(fsIP);
HSSFSheet recipeSheet = fWorkbook.getSheet("RECIPE STEPS");
HSSFSheet fromSheet = fWorkbook.getSheet("FROM FORMAT");
HSSFSheet toSheet = fWorkbook.getSheet("TO FORMAT");
HSSFSheet displaySheet = fWorkbook.getSheet("DISPLAY");
for (int i = 0; i < 30; i++) {
DataFormatter recipeFormatter = new DataFormatter();
HSSFRow recipeRow = recipeSheet.getRow(i);
HSSFCell recipeCellsColumnA = recipeRow.getCell(0);
String recipeCellValueColumnA = recipeFormatter.formatCellValue(recipeCellsColumnA);
System.out.println(recipeCellValueColumnA);
HSSFCell recipeCellsColumnB = recipeRow.getCell(1);
String recipeCellValueColumnB = recipeFormatter.formatCellValue(recipeCellsColumnB);
System.out.println(recipeCellValueColumnB);
DataFormatter fromFormatter = new DataFormatter();
HSSFRow fromRow = fromSheet.getRow(i);
HSSFCell fromCells = fromRow.getCell(0);
String fromCellValue = fromFormatter.formatCellValue(fromCells);
System.out.println(fromCellValue);
DataFormatter toFormatter = new DataFormatter();
HSSFRow toRow = toSheet.getRow(i);
HSSFCell toCells = toRow.getCell(0);
String toCellValue = toFormatter.formatCellValue(toCells);
System.out.println(toCellValue);
if (recipeCellValueColumnB.equals("YES") && !fromCellValue.equals(toCellValue)) {
System.out.println("PUT VALUE FROM 'TO FORMAT' COLUMN A ROW I INTO 'DISPLAY' SHEET CELL COLUMN B ROW I");
System.out.println("PUT VALUE FROM 'RECIPE STEPS' COLUMN A ROW I INTO 'DISPLAY' SHEET CELL COLUMN A ROW I");
Row row = displaySheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("TEST");
} else if (recipeCellValueColumnB.equals("YES") && fromCellValue.equals(toCellValue)) {
System.out.println("SET CELL IN 'DISPLAY' SHEET COLUMN A ROW I TO '' ");
System.out.println("SET CELL IN 'DISPLAY' SHEET COLUMN B ROW I TO '' ");
Row row = displaySheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("TEST");
} else if (recipeCellValueColumnB.equals("NO")) {
System.out.println("PUT VALUE FROM 'TO FORMAT' COLUMN A ROW I INTO 'DISPLAY' SHEET CELL COLUMN B ROW I");
System.out.println("PUT VALUE FROM 'RECIPE STEPS' COLUMN A ROW I INTO 'DISPLAY' SHEET CELL COLUMN A ROW I");
Row row = displaySheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("TEST");
}else if (recipeCellValueColumnA.equals("Step Name") && recipeCellValueColumnB.equals("Always Compare?")) {
System.out.println("SET CELL IN DISPLAY COLUMN A ROW I TO 'REQUIRED STEPS'");
System.out.println("PUT VALUE FROM 'TO FORMAT' COLUMN A ROW I INTO 'DISPLAY' SHEET CELL COLUMN B ROW I");
Row row = displaySheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("TEST");
}
}
FileOutputStream output_file = new FileOutputStream(new File("Change.xls"));
BufferedOutputStream bos = new BufferedOutputStream(output_file);
fWorkbook.write(output_file);
fWorkbook.close();
bos.close();
output_file.close();
}catch(Exception e){
}
}
可能有很多事情。。。包括一些愚蠢的东西,比如NPE或传递不正确文件名的超出范围的索引
解决方案:
- 摆脱try-catch,并允许异常传播到调用方
- 在调用方(或堆栈的更上层)中,通过打印或记录stacktrace并导致应用程序失败来处理意外异常
- 运行修改后的程序
- 当它失败时(可能会),阅读异常消息和stacktrace,找出根本的问题是什么;i、 你可怕的异常挤压隐藏的问题