[英]create csv file java
Im trying to change the print method to csv instead.我试图将打印方法改为 csv。
private static void printGaData(GaData results) {
System.out.println(
"printing results for profile: " + results.getProfileInfo().getProfileName());
if (results.getRows() == null || results.getRows().isEmpty()) {
System.out.println("No results Found.");
} else {
// Print column headers.
for (ColumnHeaders header : results.getColumnHeaders()) {
System.out.printf("%30s", header.getName());
}
System.out.println();
// Print actual data.
for (List<String> row : results.getRows()) {
for (String column : row) {
System.out.printf("%30s", column);
}
System.out.println();
}
System.out.println();
}
}
This method above prints out three columns it works perfect.上面的这个方法打印出三列它完美无缺。 So I try to use bufferedwriter to write those three columns to csv by modifying the code to:
因此,我尝试通过将代码修改为以下内容,使用 bufferedwriter 将这三列写入 csv:
private static void ExportGaData(GaData results,PrintWriter pw) {
System.out.println("printing results for profile: " + results.getProfileInfo().getProfileName());
if (results.getRows() == null || results.getRows().isEmpty()) {
System.out.println("No results Found.");
pw.close();
} else {
// Print column headers.
for (ColumnHeaders header : results.getColumnHeaders()) {
System.out.printf(header.getName() + " ");
}
System.out.println();
for (List<String> row : results.getRows()) {
for (String column : row) {
pw.printf("%30s", column,", ");
//pw.printf(column + ", ");
}
pw.println();
}
pw.println();
}
}
This spit out 1 column instead of 3 columns.这会吐出 1 列而不是 3 列。 The date looks like this:
日期看起来像这样:
columnA's data columnb's data columnC's data
all in column A with lots of spaces.How do i resolve this?全部在有很多空格的 A 列中。我该如何解决这个问题?
I think you want我想你想要
for (String column : row) {
pw.printf(column + ", "); // not row
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.