Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert XSSFWorkbook to File

Tags:

java

file

eclipse

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?

like image 853
stef52 Avatar asked Dec 15 '22 21:12

stef52


2 Answers

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);
like image 146
Seb Avatar answered Dec 17 '22 09:12

Seb


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();
}
like image 24
Sameer Kazi Avatar answered Dec 17 '22 10:12

Sameer Kazi