Java Try/Catch不';无法在尝试打开excel工作簿时激活
使用apache POI打开excel工作簿或创建一个不存在的工作簿。 由于某些原因,正在打开的工作簿已损坏,导致在带有错误注释的行上发生错误 不知何故,这段代码的try/catch似乎没有激活。你知道为什么,我怎样才能正确处理这些错误吗? 此外,如果(file.exists()&&file.length()!=0){有条件,是否有任何方法可以在myJava Try/Catch不';无法在尝试打开excel工作簿时激活,java,excel,error-handling,apache-poi,Java,Excel,Error Handling,Apache Poi,使用apache POI打开excel工作簿或创建一个不存在的工作簿。 由于某些原因,正在打开的工作簿已损坏,导致在带有错误注释的行上发生错误 不知何故,这段代码的try/catch似乎没有激活。你知道为什么,我怎样才能正确处理这些错误吗? 此外,如果(file.exists()&&file.length()!=0){有条件,是否有任何方法可以在my期间检查文件的完整性 public XSSFWorkbook OpenWB(String directory, String name) {
期间检查文件的完整性
public XSSFWorkbook OpenWB(String directory, String name) {
File file = new File(directory + "\\" + name + ".xlsx");
FileInputStream fIP;
if(file.exists() && file.length() != 0) {
try {
fIP = new FileInputStream(file);
//Get the workbook instance for XLSX file
workbook = new XSSFWorkbook(fIP); //*********error occurs here**********
fIP.close();
System.out.println(name + ".xlsx file open successfully.");
return workbook;
} catch (IOException e) {
e.printStackTrace();
System.out.println("Error to open " + name + ".xlsx file, creating blank");
//Create Blank workbook
workbook = new XSSFWorkbook();
Integer i = 0;
while (file.isFile() && file.exists()) {
name = name.concat(i.toString());
file = new File(directory + "\\" + name + ".xlsx");
i++;
}
return workbook;
}
} else {
System.out.println("Error to open " + name + ".xlsx file, creating blank");
//Create Blank workbook
workbook = new XSSFWorkbook();
return workbook;
}
}
试试这段代码,它会给你一个错误
“打开random.xlx文件时出错,创建空白”,这意味着您的try-catch正在工作。您似乎忘记初始化变量“工作簿”
package stackoverflow;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class Solution {
public XSSFWorkbook OpenWB(String directory, String name) {
File file = new File(directory + "\\" + name + ".xlsx");
FileInputStream fIP;
XSSFWorkbook workbook;
if(file.exists() && file.length() != 0) {
try {
fIP = new FileInputStream(file);
//Get the workbook instance for XLSX file
workbook = new XSSFWorkbook(fIP); //*********error occurs here**********
fIP.close();
System.out.println(name + ".xlsx file open successfully.");
return workbook;
} catch (IOException e) {
e.printStackTrace();
System.out.println("Error to open " + name + ".xlsx file, creating blank");
//Create Blank workbook
workbook = new XSSFWorkbook();
Integer i = 0;
while (file.isFile() && file.exists()) {
name = name.concat(i.toString());
file = new File(directory + "\\" + name + ".xlsx");
i++;
}
return workbook;
}
} else {
System.out.println("Error to open " + name + ".xlsx file, creating blank");
//Create Blank workbook
workbook = new XSSFWorkbook();
return workbook;
}
}
public static void main(String args[]) {
Solution s = new Solution();
s.OpenWB("D://", "random.xlx");
}
}
您可以根据需要修改解决方案类部件。试试这段代码,它会给您一个错误
“打开random.xlx文件时出错,创建空白”,这意味着您的try-catch正在工作。您似乎忘记初始化变量“工作簿”
package stackoverflow;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class Solution {
public XSSFWorkbook OpenWB(String directory, String name) {
File file = new File(directory + "\\" + name + ".xlsx");
FileInputStream fIP;
XSSFWorkbook workbook;
if(file.exists() && file.length() != 0) {
try {
fIP = new FileInputStream(file);
//Get the workbook instance for XLSX file
workbook = new XSSFWorkbook(fIP); //*********error occurs here**********
fIP.close();
System.out.println(name + ".xlsx file open successfully.");
return workbook;
} catch (IOException e) {
e.printStackTrace();
System.out.println("Error to open " + name + ".xlsx file, creating blank");
//Create Blank workbook
workbook = new XSSFWorkbook();
Integer i = 0;
while (file.isFile() && file.exists()) {
name = name.concat(i.toString());
file = new File(directory + "\\" + name + ".xlsx");
i++;
}
return workbook;
}
} else {
System.out.println("Error to open " + name + ".xlsx file, creating blank");
//Create Blank workbook
workbook = new XSSFWorkbook();
return workbook;
}
}
public static void main(String args[]) {
Solution s = new Solution();
s.OpenWB("D://", "random.xlx");
}
}
您可以根据需要修改解决方案类零件。您会遇到什么异常?您会遇到什么异常?