在 Java 中导出到 CSV/Excel

发布于 2024-12-01 08:21:28 字数 1205 浏览 1 评论 0原文

我正在尝试通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

浮光之海 2024-12-08 08:21:28

我建议使用像 opencsv 这样的框架。它还为您进行转义和引用。

I would recommend using a framework like opencsv for that. It also does escaping and quoting for you.

沐歌 2024-12-08 08:21:28

如果没有收到错误,请检查代码所在的目录。如果没有特定路径,您的文件将保存在那里。

编辑:由于文件正在保存并且您希望它自动打开,请使用

Runtime.getRuntime().exec("results.csv"); 

(对于 Windows - 在 csv 文件的默认应用程序中打开 csv 文件)

Runtime.getRuntime().exec("open results.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

Runtime.getRuntime().exec("results.csv"); 

(For Windows - opens the csv file in the default application for csv files)

Runtime.getRuntime().exec("open results.csv"); 

(For Mac - opens the csv file in the default application for csv files)

你在我安 2024-12-08 08:21:28

为此,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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文