简体   繁体   English

Java Spring-编写Excel文件并转换为PDF

[英]Java Spring - Writing Excel file and converting to PDF

I hope someone can help me with this problem. 我希望有人可以帮助我解决这个问题。 I'm currently working in a Java Spring project (I'm not experienced with Java) and we need a service to update a excel template with some data and convert it to PDF. 我目前在Java Spring项目中工作(我没有Java经验),我们需要一项服务来使用一些数据更新excel模板并将其转换为PDF。 In the following code I use JFileChooser to allow me to select the Template_RH file to be edited. 在以下代码中,我使用JFileChooser允许我选择要编辑的Template_RH文件。 After I'm done writing a new "Template_RH_updated" is generated. 完成后,将生成一个新的“ 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();
        }

    }
}

The first problem is that I wanted to get rid of the JFileChooser. 第一个问题是我想摆脱JFileChooser。 I wanted to get the file from the project itself. 我想从项目本身获取文件。 I added the template file to the project in: 我将模板文件添加到项目中:

/myProject/src/main/resources/file/Template_RH.xls /myProject/src/main/resources/file/Template_RH.xls

But using: 但是使用:

FileInputStream file = new FileInputStream("/myProject/src/main/resources/file/Template_RH.xls");

is not working. 不管用。 What is the correct way to do this? 正确的方法是什么? Also, is it possible to make the client download this file when calling the service through the browser? 另外,通过浏览器调用服务时,可以使客户端下载此文件吗?

My last question is if anyone knows how to convert this excel file to PDF. 我的最后一个问题是,是否有人知道如何将此excel文件转换为PDF。 If there is any library that cant help me with this task. 如果有无法帮助我完成此任务的库。

Thanks! 谢谢!

Try: 尝试:

InputStream stream = this.getClass().getResourceAsStream("file/Template_RH.xls");

Yes, it's possible. 是的,有可能。 See: Downloading a file from spring controllers 请参阅: 从弹簧控制器下载文件

You can try: Java Apache POI Excel save as PDF (I haven't tried it myself). 您可以尝试: Java Apache POI Excel另存为PDF (我自己还没有尝试过)。

Since you are using Spring I suggest you load up file like below. 由于您使用的是Spring,因此建议您加载如下文件。

Path to the folder within your project, let's assume your project structure is src/main/java -> contains a package sructure com/aol/app/ src/main/resources -> contains a folder structure similar to above com/aol/app/resources -> where are your files are located, below the path points to the resources folder. 项目中文件夹的路径,假设您的项目结构为src / main / java->包含软件包com / aol / app / src / main / resources->包含与上述com / aol / app类似的文件夹结构/ resources->文件所在的位置,在路径下方指向资源文件夹。

public static final String FILE_CLASSPATH_RESOURCE = "com/aol/app/resources/";

Next use org.springframework.core.io.Resource and org.springframework.core.io.ClassPathResource to load the files, note: this can be any resource. 接下来,使用org.springframework.core.io.Resource和org.springframework.core.io.ClassPathResource加载文件,请注意:这可以是任何资源。

Resource resource = new ClassPathResource(FILE_CLASSPATH_RESOURCE + fileName);

Once the resource is loaded you can now use the stream to generate the pdf using apache poi. 加载资源后,您现在可以使用流使用apache poi生成pdf。

InputStream is = resource.getInputStream();

Since you mentioned you do not have much exposure to Java, I suggest you follow below links which shows you how to work on excel and pdf. 由于您提到过您对Java的了解不多,因此建议您点击以下链接,该链接向您展示如何使用excel和pdf。

http://www.baeldung.com/java-microsoft-excel
https://aboullaite.me/spring-boot-excel-csv-and-pdf-view-example/
http://www.baeldung.com/java-pdf-creation

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM