在 Java 中导出到 CSV/Excel
我正在尝试通过 Java 将数据导出到 CSV 文件,并且我有一些代码可以执行此操作,但它似乎没有输出 CSV 文件。有人能告诉我出了什么问题吗?我想做的不是将文件保存在某个地方,而是希望将其直接导出给用户。
编辑:以防万一不清楚,我不希望将文件保存在任何地方,但希望将其自动输出给用户,即他们单击导出并获取“运行/保存结果.csv”窗口并打开文件。目前该文件正在保存,所以我知道该方法似乎有效,只是以与我想要的相反的方式。
public static void writeToCSV(List<Map> objectList) {
String CSV_SEPARATOR = ",";
try {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream("results.csv"), "UTF-8"));
for (Map objectDetails : objectList) {
StringBuffer oneLine = new StringBuffer();
Iterator it = objectDetails.values().iterator();
while (it.hasNext()) {
Object value = it.next();
if(value !=null){
oneLine.append(value.toString());
}
if (it.hasNext()) {
oneLine.append(CSV_SEPARATOR);
}
}
bw.write(oneLine.toString());
bw.newLine();
}
bw.flush();
bw.close();
} catch (UnsupportedEncodingException e) {
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
}
I'm trying to export data into a CSV file through Java and I've got some code to do it but it doesn't seem to be outputting the CSV file. Could someone tell me what's wrong? What I would like to do is rather than saving the file somewhere, I would like it to be directly exported to the user.
EDIT: Just in case it's not clear, I don't want the file to be saved anywhere but would like it to be outputted automatically to the user i.e. they click export and get the "Run/Save results.csv" window and they open the file. Currently the file is getting saved so I know that the method seems to work, just in the opposite way that I want it to.
public static void writeToCSV(List<Map> objectList) {
String CSV_SEPARATOR = ",";
try {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream("results.csv"), "UTF-8"));
for (Map objectDetails : objectList) {
StringBuffer oneLine = new StringBuffer();
Iterator it = objectDetails.values().iterator();
while (it.hasNext()) {
Object value = it.next();
if(value !=null){
oneLine.append(value.toString());
}
if (it.hasNext()) {
oneLine.append(CSV_SEPARATOR);
}
}
bw.write(oneLine.toString());
bw.newLine();
}
bw.flush();
bw.close();
} catch (UnsupportedEncodingException e) {
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我建议使用像 opencsv 这样的框架。它还为您进行转义和引用。
I would recommend using a framework like opencsv for that. It also does escaping and quoting for you.
如果没有收到错误,请检查代码所在的目录。如果没有特定路径,您的文件将保存在那里。
编辑:由于文件正在保存并且您希望它自动打开,请使用
(对于 Windows - 在 csv 文件的默认应用程序中打开 csv 文件)
(对于 Mac - 在 csv 文件的默认应用程序中打开 csv 文件)
If you're not getting errors, check the directory where your code is. Without a specific path, your file is being saved there.
EDIT: Since the file is being saved and you want it to open automatically, use
(For Windows - opens the csv file in the default application for csv files)
(For Mac - opens the csv file in the default application for csv files)
推荐
HSSFWorkbook
轻松读写excel文件。http://poi.apache.org/apidocs/org /apache/poi/hssf/usermodel/HSSFWorkbook.html
recommend
HSSFWorkbook
to easily read and write excel files.http://poi.apache.org/apidocs/org/apache/poi/hssf/usermodel/HSSFWorkbook.html
为此,CSV 读取器需要读取程序的内存。这是一件有点复杂的事情。因此,请将文件保存在临时文件夹中。做这种事情是没有问题的。
To do that, the CSV reader need to read the memory of your program. This is a little complex thing to do. So, save the file in a temp folder instead. There is no problem to do this sort of thing.