本帖最后由 据守 于 2024-8-12 16:40 编辑
Java swt 项目 JDK17
引用外部库:
org.eclipse.swt.win32.win32.x86_64_3.120.0.v20220530-1036.jar
poi-4.0.0.jar
poi-ooxml-4.0.0.jar
poi-ooxml-schemas-4.0.0.jar
xmlbeans-3.0.1.jar
commons-collections4-4.2.jar
commons-compress-1.18.jar
出问题代码的功能是选择excel文件并读取内容。
其中报错在:创建XSSFWorkbook对象
这是UI界面的代码:
[Java] 纯文本查看 复制代码 //“导入账号”按钮,设置文本、尺寸及监听事件
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
Button buttonlord = new Button(shell, SWT.PUSH);
buttonlord.setText("导入账号");
buttonlord.setBounds(350 ,10 ,100, 40);
buttonlord.addListener(SWT.Selection, event -> {
FileDialog dialog = new FileDialog(shell, SWT.OPEN);
dialog.setText("选择Excel文件");
dialog.setFilterExtensions(new String[] {"*.xlsx"});
String path = dialog.open();
if(path!=null) {
//System.out.println(path);
try {
snum[0] = GetStudent.getstu(path);
} catch (FileNotFoundException e1) {
// TODO 自动生成的 catch 块
e1.printStackTrace();
} catch (InvalidFormatException e1) {
// TODO 自动生成的 catch 块
e1.printStackTrace();
} catch (IOException e1) {
// TODO 自动生成的 catch 块
e1.printStackTrace();
}
}
}
});
GetStudent.java 代码:
[Java] 纯文本查看 复制代码 public class GetStudent {
public static int getstu(String path) throws FileNotFoundException, IOException, InvalidFormatException {
Charset charset = Charset.forName("UTF-8"); //设置BIG5也没用
FileInputStream fis = new FileInputStream(path);
InputStreamReader isr = new InputStreamReader(fis);
OPCPackage pkg = OPCPackage.open(fis);
// 创建XSSFWorkbook对象,下面这一句总是报错
XSSFWorkbook Workbook = new XSSFWorkbook(pkg);
......
}
}
最开始创建XSSFWorkbook对象的代码,还是报错:
[Java] 纯文本查看 复制代码 public class GetStudent {
public static int getstu(String path) throws FileNotFoundException, IOException{
XSSFWorkbook Workbook = new XSSFWorkbook(new FileInputStream(path));
......
}
}
在eclipse中运行正常,可以读取excel数据。导出jar转成exe后,导入空表格不报错。
但是表格有任何数据,则“创建XSSFWorkbook对象”就报错。
报错信息如下:
[Java] 纯文本查看 复制代码 java.lang.ExceptionInInitializerError
at org.apache.poi.ss.util.CellReference.<init>(CellReference.java:109)
at org.apache.poi.xssf.usermodel.XSSFCell.<init>(XSSFCell.java:116)
at org.apache.poi.xssf.usermodel.XSSFRow.<init>(XSSFRow.java:74)
at org.apache.poi.xssf.usermodel.XSSFSheet.initRows(XSSFSheet.java:269)
at org.apache.poi.xssf.usermodel.XSSFSheet.read(XSSFSheet.java:232)
at org.apache.poi.xssf.usermodel.XSSFSheet.onDocumentRead(XSSFSheet.java:219)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.parseSheet(XSSFWorkbook.java:452)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.onDocumentRead(XSSFWorkbook.java:417)
at org.apache.poi.ooxml.POIXMLDocument.load(POIXMLDocument.java:184)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:286)
at com.jushou.cc.GetStudent.getstu(GetStudent.java:30)
at com.jushou.cc.SWTBrowser.lambda$0(SWTBrowser.java:157)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:89)
at org.eclipse.swt.widgets.Display.sendEvent(Display.java:4251)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1066)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4068)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3645)
at com.jushou.cc.SWTBrowser.main(SWTBrowser.java:776)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at com.exe4j.runtime.LauncherEngine.launch(LauncherEngine.java:84)
at com.exe4j.runtime.WinLauncher.main(WinLauncher.java:94)
Caused by: java.nio.charset.UnsupportedCharsetException: Big5
at java.base/java.nio.charset.Charset.forName(Charset.java:528)
at org.apache.poi.util.StringUtil.<clinit>(StringUtil.java:39)
... 24 more
|