英文:
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文件可以顺利生成,但其中一些数据是带有逗号的字符串,因此结果是它们被分隔到不同的列中。是否有办法设置另一个分隔符?(例如 )。
生成字节数组的方法如下:
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 .
The method that generates the byte array is this:
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;
}
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, "application/csv", "Example.csv");
return file;
} else {
//ERROR
}
} catch (Exception ex) {
}
return null;
}
And the button that generates the csv in the xhtml is this one:
<p:fileDownload contentDisposition="inline" value="#{exampleBean.exportCsv()}" />
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
<p:dataExporter type="csv" target="tbl" fileName="cars" options="#{view.csvOptions}" />
而您的Options类看起来像这样,以设置冒号(:)为例...
public CSVOptions getCsvOptions() {
CSVOptions options = new CSVOptions();
options.setDelimiterChar(":");
return options;
}
英文:
You can in PF 7.0 or higher using the options
attribute!
Fixed: https://github.com/primefaces/primefaces/issues/48
<p:dataExporter type="csv" target="tbl" fileName="cars" options="#{view.csvOptions}" />
And your Options class looks like this to set to colon (:) for example...
public CSVOptions getCsvOptions() {
CSVOptions options = new CSVOptions();
options.setDelimiterChar(":");
return options;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论