I currently have an XSSFWorkbook
and would like to cast it or somehow change it to File
within the Java code. Is there any way of doing so?
Use XSSFWorkbook.write(java.io.OutputStream stream) to write the content to a file.
FileOutputStream out = new FileOutputStream("yourFileName.xls");
Workbook wb = new XSSFWorkbook();
//do your stuff ...
wb.write(out);
Bellow code is used and tested.
private Response sendExcelFile(Locale locale, Optional<List<List<String>>> consumersReportData) {
XSSFWorkbook workBook = ExportToExcelUtils.prepareWorkBook(consumersReportData.get(), "data");
String DisplayFileName = "Consumers-Report-" + DateUtils.getLocalDateInString(DateUtils.now());
String fileName = "/tmp/fileName.xlsx";
// Created file object here
File file = new File(fileName);
try {
FileOutputStream outputStream = new FileOutputStream(file);
workBook.write(outputStream);
} catch (FileNotFoundException e) {
LOGGER.error("File not found : fileName {} Exception details:{} ", fileName, e);
} catch (IOException e) {
LOGGER.error("IO exception : fileName {} Exception details:{} ", fileName, e);
}
ResponseBuilder responseBuilder = Response.ok((Object) file);
responseBuilder.header("Content-Disposition", "attachment; filename=" + DisplayFileName + EXCEL_FILE_EXTENSTION);
return responseBuilder.build();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With