user3766629
user3766629

Reputation: 1

Export to CSV JSF and PrimeFaces

Export to Excel JSF and PrimeFaces

I am trying to download CSV file which is created in runtime. This link is useful for excel and I need to do the same for CSV. HSSFWorkbook is used for excel but I am using FileWriter for CSV. I need a line to use rather than workbook.write(externalContext.getResponseOutputStream()); I cannot use writer.write(externalContext.getResponseOutputStream()); writer is FileWriter variable and does not accept outputStream as parameter.

Upvotes: 0

Views: 5154

Answers (2)

newuserua_ext
newuserua_ext

Reputation: 512

Whole working copy that you may use ;

String csvFileName = "mydoc.csv";

    FileWriter writer = new FileWriter(csvFileName);
    int columnNameSize = activeTab.getColumnNames().size();

    for (int i = 0; i < columnNameSize; i++) {
        writer.append(activeTab.getColumnNames().get(i));
        if (i != (columnNameSize - 1)) {
            if (delimiterType.equalsIgnoreCase(TAB_DELIMITER_VALUE_NAME)) {
                writer.append('\t');
            } else {
                writer.append(delimiterType);
            }
        }
    }
    writer.append("\n");

    for (DBData[] temp : activeTab.getTabularData()) {
        int tempSize = temp.length;
        for (int k = 0; k < tempSize; k++) {
            writer.append(temp[k].toFullString());
            if (k != (tempSize - 1)) {
                if (delimiterType.equalsIgnoreCase(TAB_DELIMITER_VALUE_NAME)) {
                    writer.append('\t');
                } else {
                    writer.append(delimiterType);
                }
            }
        }
        writer.append("\n");
    }

    writer.flush();
    writer.close();

    InputStream stream = new BufferedInputStream(new FileInputStream(csvFileName));
    exportFile = new DefaultStreamedContent(stream, "application/csv", csvFileName);

Upvotes: 0

Cy&#228;egha
Cy&#228;egha

Reputation: 4251

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();
}

Upvotes: 2

Related Questions