It seems to me that you have two issues here :
You shouldn't have a FileWriter
if you don't want to write to a file - you need to choose the right implementation of the Writer abstract class for your use case (here, you want to chose the one that writes to an OutputStream
, not to a File
).
You're trying to use Writer#write(...)
like HSSFWorkbook#write(java.io.OutputStream)
, but they don't do the same thing at all. In HSSFWorkbook, the write method writes the workbook's content to some OutputStream; the parameter tells the method where you want to write. In Writer, the write method writes something to the writer itself; the parameter tells the method what you want to write.
Based on your link for writing from a HSSFWorkbook, writing a CSV in a similar way could look something like :
public void getReportData() throws IOException {
FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext externalContext = facesContext.getExternalContext();
externalContext.setResponseContentType("text/csv");
externalContext.setResponseHeader("Content-Disposition", "attachment; filename=\"my.csv\"");
OutputStream out = externalContext.getResponseOutputStream());
Writer writer = new OutputStreamWriter(out);
// Let's write the CSV content
try {
writer.write("Line number,Col 1,Col 2");
writer.write("1,Value 1,Value 2");
writer.write("2,Value 3,Value4");
} finally {
if (writer != null {
// Closing the writer also flushes it, and does the same to the underlying OutputStream
writer.close();
}
}
facesContext.responseComplete();
}