gpt4 book ai didi

java - OpenCSV 写入限制

转载 作者:行者123 更新时间:2023-12-02 03:33:37 26 4
gpt4 key购买 nike

我试图使用 openCSV java 库在 csv 文件中写入一些公式数据,但它只写入每个单元格中大约 90% 的字符串。是否有任何已知问题可能导致此行为?

public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException {

FileOutputStream fosW = new FileOutputStream("C:\\Users\\Desktop\\formula.csv");
OutputStreamWriter osw = new OutputStreamWriter(fosW, "UTF-8");
CSVWriter writerCSW = new CSVWriter(osw);

int k = 0;
String[] b = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "AA", "AB", "AC", "AD", "AE", "AF", "AG", "AH", "AI", "AJ", "AK", "AL", "AM", "AN", "AO", "AP", "AQ", "AR", "AS", "AT"};
String[] line = new String[100];
for (int i = 0; i < b.length - 1; i++) {
String a = "=IF(legend!" + b[i] + "$3='Form responses 1'!" + b[i + 1] + "2,legend!" + b[i + 1] + "$3,IF(legend!" + b[i] + "$4='Form responses 1'!" + b[i + 1] + "2,legend!" + b[i + 1] + "$4,IF(legend!" + b[i] + "$5='Form responses 1'!" + b[i + 1] + "2,legend!" + b[i + 1] + "$5,IF(legend!" + b[i] + "$6='Form responses 1'!" + b[i + 1] + "2,legend!" + b[i + 1] + "$6,IF(legend!" + b[i] + "$7='Form responses 1'!" + b[i + 1] + "2,legend!" + b[i + 1] + "$7,IF(legend!" + b[i] + "$8='Form responses 1'!" + b[i + 1] + "2,legend!" + b[i + 1] + "$8,#error#))))))";
System.out.println(a);
line[k] = a;
k++;
}
writerCSW.writeNext(line);

}

最佳答案

我相信您的问题是您错误地使用了该库。 (虽然这可能是我的误解。

您的 B 长度为 46。您的 CSV 文件将包含 45 个条目。

第 [k] 行正好是 1 行。我认为您假设行数组中的每个索引是。这是第一个错误(也许)。

但是您的文件未完全填充的原因是您没有关闭流,因此一旦您的程序存在,数据就会丢失。因此,您需要关闭您的 writer,或者更好的是,使用 try-with-resources block 。我已经重写了您的代码来执行此操作:

public static void main(String[] args) throws IOException {

FileOutputStream fosW = new FileOutputStream("/home/artur/tmp/test.csv");
OutputStreamWriter osw = new OutputStreamWriter(fosW, "UTF-8");
try (CSVWriter writerCSW = new CSVWriter(osw)) {

String[] b = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
"S", "T", "U", "V", "W", "X", "Y", "Z", "AA", "AB", "AC", "AD", "AE", "AF", "AG", "AH", "AI", "AJ",
"AK", "AL", "AM", "AN", "AO", "AP", "AQ", "AR", "AS", "AT" };

System.out.println(b.length);
for (int i = 0; i < b.length - 1; i++) {
String a = "=IF(legend!" + b[i] + "$3='Form responses 1'!" + b[i + 1] + "2,legend!" + b[i + 1]
+ "$3,IF(legend!" + b[i] + "$4='Form responses 1'!" + b[i + 1] + "2,legend!" + b[i + 1]
+ "$4,IF(legend!" + b[i] + "$5='Form responses 1'!" + b[i + 1] + "2,legend!" + b[i + 1]
+ "$5,IF(legend!" + b[i] + "$6='Form responses 1'!" + b[i + 1] + "2,legend!" + b[i + 1]
+ "$6,IF(legend!" + b[i] + "$7='Form responses 1'!" + b[i + 1] + "2,legend!" + b[i + 1]
+ "$7,IF(legend!" + b[i] + "$8='Form responses 1'!" + b[i + 1] + "2,legend!" + b[i + 1]
+ "$8,#error#))))))";
String[] line = a.split(",");
writerCSW.writeNext(line);
System.out.println(i);
}
}

}

注意,我改变了一些东西,所以它可能不完全符合你的要求。不过,您应该能够从这个示例中看出出了什么问题。

请注意:看看我如何用逗号分隔你的字符串 a ?这是为 CSV 输入生成一个数组。它会自动为您插入您选择的分隔符。如果您不想这样做,请摆脱 CSVWriter,并使用普通的 Filewriter。您可以通过构造字符串手动生成 CSV 行。使用 OpenCSV 时不需要这样做。

希望对您有所帮助,

阿图尔

关于java - OpenCSV 写入限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37721680/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com