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!