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 in Others by Kithuzzz
• 20,660 points
272 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();

Since you mentioned you do not have much exposure to Java, I suggest you follow the below links which show you how to work on excel and 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
answered Sep 26 by narikkadan
• 37,660 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 in Others by gaurav
• 22,040 points
1,902 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 in Others by narikkadan
• 37,660 points
145 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 in Others by narikkadan
• 37,660 points
232 views
0 votes
1 answer

Codeigniter convert excel file to pdf

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

answered Sep 27 in Others by narikkadan
• 37,660 points
187 views
0 votes
1 answer

Convert Excel to PDF issue with documents4j

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

answered Sep 26 in Others by narikkadan
• 37,660 points
198 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 in Others by narikkadan
• 37,660 points
82 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 in Others by narikkadan
• 37,660 points
63 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 by Sarfaraz 14,064 views
0 votes
1 answer
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 in Others by narikkadan
• 37,660 points
62 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