Java Spring - Writing Excel file and converting to PDF

0 votes

I'm hoping someone can offer assistance to me. Despite my lack of Java knowledge, I'm currently working on a Java Spring project where we require a service to update an Excel template with certain data and convert it to PDF. The Template RH file can be chosen to be edited using JFileChooser in the code below. A new "Template RH updated" is generated after I'm through writing.

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. 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

But using:

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

is ineffective. What is the proper technique to carry this out? Is it also possible to require the client to download this file while using a browser to call the service?

If anyone knows how to convert this excel sheet to a PDF, please let me know. If there is a library that can't assist me, please let me know.

Sep 25, 2022 in Others by Kithuzzz
• 38,000 points
2,610 views

1 answer to this question.

0 votes

Since you are using Spring I suggest you load up the file below.

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.

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.

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.

InputStream is = resource.getInputStream();

answered Sep 26, 2022 by narikkadan
• 63,600 points

Related Questions In Others

0 votes
1 answer

datatable remove column on export to pdf and excel

When we are using jquery datatable for ...READ MORE

answered Feb 17, 2022 in Others by gaurav
• 23,260 points
4,218 views
0 votes
1 answer

Convert Excel and Word files to PDF Using ruby

 You can combine some: For excel files - ...READ MORE

answered Sep 26, 2022 in Others by narikkadan
• 63,600 points
1,331 views
0 votes
1 answer

Converting all tabs of excel sheet to PDF

Using VBA, try it like this, for ...READ MORE

answered Sep 26, 2022 in Others by narikkadan
• 63,600 points
1,731 views
0 votes
1 answer

Codeigniter convert excel file to pdf

This is a basic php script for ...READ MORE

answered Sep 27, 2022 in Others by narikkadan
• 63,600 points
1,766 views
0 votes
1 answer

Convert Excel to PDF issue with documents4j

MS Excel may not always be used ...READ MORE

answered Sep 26, 2022 in Others by narikkadan
• 63,600 points
1,506 views
0 votes
1 answer

Using Java to create PDF graphs based on Excel formulas

The Apache POI project, an API for ...READ MORE

answered Oct 22, 2022 in Others by narikkadan
• 63,600 points
827 views
0 votes
1 answer

How can I convert a Word document to PDF?

This is a difficult task, made even ...READ MORE

answered Oct 31, 2022 in Others by narikkadan
• 63,600 points
1,324 views
0 votes
1 answer

Concern and cross-cutting concern in Spring AOP

The concern is the behavior we want ...READ MORE

answered Nov 16, 2018 in Java by code.reaper12
• 3,500 points

edited Mar 4, 2022 by Sarfaraz 18,349 views
0 votes
1 answer

How can I convert excel to PDF by Libreoffice and keep all format from excel file?

"Times New Roman" typeface does not have ...READ MORE

answered Oct 3, 2022 in Others by narikkadan
• 63,600 points
1,649 views
0 votes
1 answer

Uipath(RPA) : read data from the PDF file and write to Excel file

If you want to use UiPath and ...READ MORE

answered Oct 17, 2022 in Others by narikkadan
• 63,600 points
2,154 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP