【问题标题】:Export to CSV JSF and PrimeFaces导出到 CSV JSF 和 PrimeFaces
【发布时间】:2014-09-29 02:44:21
【问题描述】:

Export to Excel JSF and PrimeFaces

我正在尝试下载在运行时创建的 CSV 文件。这个链接对 excel 很有用,我需要对 CSV 做同样的事情。 HSSFWorkbook 用于 excel,但我将 FileWriter 用于 CSV。我需要使用一行而不是 workbook.write(externalContext.getResponseOutputStream()); 我不能使用 writer.write(externalContext.getResponseOutputStream()); writer 是 FileWriter 变量并且不接受 outputStream 作为参数。

【问题讨论】:

    标签: java excel jsf csv primefaces


    【解决方案1】:

    在我看来你有两个问题:

    • 如果您不想写入文件,则不应使用 FileWriter - 您需要为您的用例选择正确的 Writer 抽象类实现(在这里,您要选择写信给OutputStream,而不是写给File)。

    • 您正在尝试像 HSSFWorkbook#write(java.io.OutputStream) 一样使用 Writer#write(...),但它们根本不会做同样的事情。在 HSSFWorkbook 中,write 方法将工作簿的内容写入到某个 OutputStream;参数告诉方法在哪里你想写。在 Writer 中, write 方法向 writer 本身写入内容;参数告诉方法你想写什么

    根据您从 HSSFWorkbook 编写的链接,以类似方式编写 CSV 可能类似于:

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

    【讨论】:

    • 您可以在每行的末尾添加换行符:"\n" 用于 UNIX 样式的换行 (LF),"\r\n" 用于 Windows 样式的回车 - 换行 (CRLF)。如果您想为服务器系统使用标准换行符,请参阅this question
    【解决方案2】:

    您可以使用的整个工作副本;

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-29
      • 2013-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多