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.