java - 在 IDE 中使用 Apache poi 导出到 excel 是可行的,但是当我创建可运行的 jar 时它不会
问题描述
我正在使用 Netbeans 8.2,我通过单击clean and build
图标创建 jar,可运行的 jar 可以工作,除非我希望将数据导出到 excel。
这是导出到 excel 的逻辑:
public static void exportToExcel(TableView<T> tableView, Stage stage) {
HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
HSSFSheet hssfSheet = hssfWorkbook.createSheet("Sheet1");
HSSFRow firstRow = hssfSheet.createRow(0);
//Getting column width
ExcelPropertiesCustomization.getColumnWidth(hssfSheet);
// Getting title properties
CellStyle naslov = ExcelPropertiesCustomization.getTitleProperties(hssfWorkbook);
//set titles of columns
for (int i = 0; i < tableView.getColumns().size(); i++) {
firstRow.createCell(i).setCellValue(tableView.getColumns().get(i).getText());
firstRow.getCell(i).setCellStyle(naslov);
}
// set cells for rest of the table
for (int i = 0; i < tableView.getItems().size(); i++) {
HSSFRow hssfRow = hssfSheet.createRow(i + 1);
for (int col = 0; col < tableView.getColumns().size(); col++) {
Object celValue = tableView.getColumns().get(col).getCellObservableValue(i).getValue();
try {
if (celValue != null) {
hssfRow.createCell(col).setCellValue(Double.parseDouble(celValue.toString()));
}
} catch (NumberFormatException e) {
hssfRow.createCell(col).setCellValue(celValue.toString());
} catch (NullPointerException e) {
System.out.println(e);
}
}
}
//save excel file and close the workbook
try {
File file = new File("FXdatabase.xls");
FileOutputStream out = new FileOutputStream(file);
hssfWorkbook.write(out);
FileChooser fileChooser = new FileChooser();
FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("XLS files (*.xls)", "*.xls");
fileChooser.getExtensionFilters().add(extFilter);
File dest = fileChooser.showSaveDialog(stage);
if (dest != null) {
try {
Files.copy(file.toPath(), dest.toPath());
} catch (IOException ex) {
System.err.println(ex);
}
}
out.close();
} catch (IOException e) {
System.out.prinln(e);
}
}
这不是我第一次使用 apache poi 导出到 excel,但我从来没有遇到过这个问题(我也有新的笔记本电脑,不知道这是否与问题有关),我怀疑它是IOException
. 我能做些什么?
解决方案
推荐阅读
- sql-server - SqlException:使用 .NET Core 2.1 的 SQL Server 的用户 'DOMAIN\MACHINENAME$' 登录失败
- flutter - Flutter Implementing pause and play..... Streaming Music
- c# - 如何创建 C# 8.0 控制台应用程序?
- c# - Form Recognizer SDK,使用自定义模型分析,文件流问题
- javascript - 剪辑路径的边缘/IE 问题
- javascript - 如何在 JavaScript If 语句中添加多个 CSS 属性?
- google-sheets - 使用条件格式突出显示一组数据是否匹配另一组数据
- java - Bukkit - 输入后在聊天中显示命令
- c++ - Clang tidy:在表达式末尾添加括号
- reactjs - React Redux thunk - 调度完成后渲染应用程序