I've been banging my head against the wall on this problem for a while now, and I've done my fair share of looking through existing StackOverflow questions that are similar to my predicament, but haven't found a solution that works for me yet.
I've got existing code that takes an HttpServletResponse and exports it as an .xls attachment for the user to download. This code is presented here:
import java.io.IOException;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletResponse;
/**
* Action class to export data as a excel sheet.
*
*/
public class ForecastReportExcel extends PageCodeBase {
public String exportExcel() throws IOException {
HttpServletResponse response = (HttpServletResponse) FacesContext
.getCurrentInstance().getExternalContext().getResponse();
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition",
"attachment; filename=forecastReports.xls");
return "success";
}
}
My requirements for this project are simply to change the exported file from an .xls file to an .xlsx file. Which seemed like a pretty simple undertaking.
My first attempt was to simply change the MimeType to application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
and the filename to forecastReports.xlsx
. AFter downloading the exported file from the webapp, I get the error - Excel cannot open the file '[file].xlsx' because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file.
So, I went to searching and came across some threads where people suggested using the Apache POI libraries, specifically the XSSF Library for .xlsx files, and writing to the OutputStream. So, I gave this a whirl:
import java.io.IOException;
import java.io.OutputStream;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
/**
* Action class to export data as a excel sheet.
*
*/
public class ForecastReportExcel extends PageCodeBase {
public String exportExcel() throws IOException {
XSSFWorkbook workbook = new XSSFWorkbook();
HttpServletResponse response = (HttpServletResponse) FacesContext
.getCurrentInstance().getExternalContext().getResponse();
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setHeader("Content-Disposition",
"attachment; filename=forecastReports.xlsx");
OutputStream outStream = response.getOutputStream();
workbook.write(outStream);
outStream.flush();
outStream.close();
return "success";
}
}
When I download the exported file and run it in Excel, I get the message We found a problem with some content in '[file].xlsx'. Do you want us to try to recover as much as we can? If you trust the source of this workbook, click Yes.
Upon clicking 'Yes', I get The workbook cannot be opened or repaired by Microsoft Excel because it is corrupt.
Also, when I take a look at my developer console, I'm seeing an error printing out that says java.lang.IllegalStateException: Cannot forward. Response already committed.
I've done some searching on what that means, but I'm not finding how it relates to this. Now, I'm super confused. Does anyone see anything I'm not seeing? Can someone give me some direction on what I'm doing wrong?
Thank you all so much for any help or ideas thrown my way!