Exporting a csv using component fileDownload of primefaces generates problems with comma separator when data have commas

huangapple go评论61阅读模式
英文:

Exporting a csv using component fileDownload of primefaces generates problems with comma separator when data have commas

问题

我有以下问题。我正在使用Primefaces、JSF和Java工作。我正在尝试生成一个包含数据库数据的csv文件,并使用Primefaces的p:fileDownload组件导出它。CSV文件可以顺利生成,但其中一些数据是带有逗号的字符串,因此结果是它们被分隔到不同的列中。是否有办法设置另一个分隔符?(例如 Exporting a csv using component fileDownload of primefaces generates problems with comma separator when data have commas )。

生成字节数组的方法如下:

public byte[] generateCsv(){
    StringBuilder data = new StringBuilder();
    data.append("header1, header2, header3\n");
    for (element elem : listElements) {
        data.append(elem.getCode() + "," + elem.getName() + "," + elem.getData() + "\n");
    }
    byte[] bytes = String.valueOf(data).getBytes();
    return bytes;
}

在前端,bean具有以下方法:

public StreamedContent exportCsv() {
    try {
        byte[] cert = businessBean.generateCsv();
        if (cert != null) {
            InputStream targetStream = new ByteArrayInputStream(cert);
            StreamedContent file = new DefaultStreamedContent(targetStream, "application/csv", "Example.csv");
            return file;
        } else {
           //错误处理
        }
    } catch (Exception ex) {
        //异常处理
    }
    return null;
}

生成CSV的按钮位于xhtml中:

<p:fileDownload contentDisposition="inline" value="#{exampleBean.exportCsv()}" />

结果:
如果每列的元素应该是:

column1= elem1
column2= elem2.1,elem2.2
column3=elem3.1,elem3.2

那么在文件中,每个元素都会出现如下:

column1= elem1
column2= elem2.1
column3=elem2.2
column4=elem3.1
column5=elem3.2

是否有办法设置不同的分隔符?
谢谢。

英文:

I have the following problem. I am working with Primefaces, JSF and Java. I am trying to generate a csv file with data of the data base and exporting it with the component p:fileDownload of primefaces. The csv is generated without problems but some of the data are strings with commas init, so the result is that there are separated in different columns. Is there a way of setting another separator? (for example Exporting a csv using component fileDownload of primefaces generates problems with comma separator when data have commas .

The method that generates the byte array is this:

public byte[] generateCsv(){
                StringBuilder data = new StringBuilder();
                data.append(&quot;header1, header2, header3\n&quot;);
                for (element elem : listElements) {
                    data.append(elem.getCode()+&quot;,&quot;+elem.getName()+&quot;,&quot;+elem.getData+&quot;\n&quot;);
                }
                byte[] bytes = String.valueOf(data).getBytes();
                return bytes;
}

In the frontend, the bean has this method:

public StreamedContent exportCsv() {
        try {
            
            byte[] cert = businessBean.generateCsv();
            if (cert != null) {
                InputStream targetStream = new ByteArrayInputStream(cert);
                StreamedContent file = new DefaultStreamedContent(targetStream, &quot;application/csv&quot;, &quot;Example.csv&quot;);
                
                return file;
            } else {
               //ERROR
            }

        } catch (Exception ex) {
            
        }
        return null;
    }

And the button that generates the csv in the xhtml is this one:

&lt;p:fileDownload contentDisposition=&quot;inline&quot;   value=&quot;#{exampleBean.exportCsv()}&quot; /&gt;

Result:
If the elements of each column are supposed to be:

column1= elem1
column2= elem2.1,elem2.2
column3=elem3.1,elem3.2

In the file every element appears like.

column1= elem1
column2= elem2.1
column3=elem2.2
column4=elem3.1
column5=elem3.2

There is a way to set a different separator?
Thanks

答案1

得分: 1

您可以在PF 7.0或更高版本中使用options属性!

已修复:https://github.com/primefaces/primefaces/issues/48

&lt;p:dataExporter type=&quot;csv&quot; target=&quot;tbl&quot; fileName=&quot;cars&quot; options=&quot;#{view.csvOptions}&quot; /&gt;

而您的Options类看起来像这样,以设置冒号(:)为例...

public CSVOptions getCsvOptions() {
    CSVOptions options = new CSVOptions();
    options.setDelimiterChar(&quot;:&quot;);
    return options;
}
英文:

You can in PF 7.0 or higher using the options attribute!

Fixed: https://github.com/primefaces/primefaces/issues/48

&lt;p:dataExporter type=&quot;csv&quot; target=&quot;tbl&quot; fileName=&quot;cars&quot; options=&quot;#{view.csvOptions}&quot; /&gt;

And your Options class looks like this to set to colon (:) for example...

public CSVOptions getCsvOptions() {
    CSVOptions options = new CSVOptions();
    options.setDelimiterChar(&quot;:&quot;);
    return options;
}

huangapple
  • 本文由 发表于 2020年7月31日 06:17:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/63182170.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定