【发布时间】:2018-10-31 10:21:06
【问题描述】:
我希望有人可以帮助我解决这个问题。我目前正在从事 Java Spring 项目(我对 Java 没有经验),我们需要一项服务来使用一些数据更新 excel 模板并将其转换为 PDF。在下面的代码中,我使用 JFileChooser 来选择要编辑的 Template_RH 文件。在我写完之后会生成一个新的“Template_RH_updated”。
public class GenerateExcel {
public static void fillTable(List<ReportTable> table, Employee employee, String headerMonth) {
JFileChooser fileChooser = new JFileChooser();
int click = fileChooser.showOpenDialog(null);
if(click == JFileChooser.APPROVE_OPTION) {
try {
Workbook workbook = new HSSFWorkbook( new FileInputStream(fileChooser.getSelectedFile()));
Sheet sheet = workbook.getSheetAt(0);
//HERE WE HAVE THE CODE TO WRITE IN THE EXCEL FILE
// AFTER WE ARE DONE WRITING IN THE FILE
FileOutputStream outFile =new FileOutputStream(new File("Template_RH_updated.xls"));
workbook.write(outFile);
outFile.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
第一个问题是我想摆脱 JFileChooser。我想从项目本身获取文件。我在项目中添加了模板文件:
/myProject/src/main/resources/file/Template_RH.xls
但使用:
FileInputStream file = new FileInputStream("/myProject/src/main/resources/file/Template_RH.xls");
不工作。这样做的正确方法是什么?另外,是否可以让客户端通过浏览器调用服务时下载这个文件?
我的最后一个问题是是否有人知道如何将此 excel 文件转换为 PDF。如果有任何图书馆不能帮助我完成这项任务。
谢谢!
【问题讨论】:
标签: java excel spring file pdf