1

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. In the following code I use JFileChooser to allow me to select the Template_RH file to be edited. After I'm done writing a new "Template_RH_updated" is generated.

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 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. If there is any library that cant help me with this task.

Thanks!

2 Answers2

0

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

Amila
  • 5,195
  • 1
  • 27
  • 46
0

Since you are using Spring I suggest you load up file like 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 below links which shows 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
Vikram Palakurthi
  • 2,406
  • 1
  • 27
  • 30