在Excel中存储For循环的值以进行打印。

huangapple 未分类评论50阅读模式
英文:

Store values of For loops to print in Excel

问题

以下是您要翻译的内容:

我实际上是在Java方面新手。我有一个文本区域,我想逐行读取,这个问题我已经解决了。但我想要的是将这些行中的每一行都写入Excel文件中。我已经成功创建了Excel文件并向其中写入内容。下面的代码会逐行读取并将其写入Excel,但我不知道文本区域可以有多少行,所以如果我在下面的代码中在文本区域中写入超过3行,它只会将前3行写入Excel,如果我写入少于3行,就会抛出ArrayIndexOutOfBoundsException错误。我希望将我想要的所有行都写入文本区域,并将它们写入Excel。谢谢

String[] lines = TextArea.getText().split("\\n");
for (int i = 0; i < lines.length; i++) {
    System.out.println(lines[i]);

    String[][] entry = new String[40][40];
    entry[0][0] = "A1";
    entry[1][0] = lines[0];
    entry[2][0] = lines[1];
    entry[3][0] = lines[2];

    String route = "/Users/sushi/Documents/writexcel.xls";
    g.generateExcel(entry, route);
}

请注意,代码中可能有一处错误:entrada应该更正为entry

英文:

I´m actually new in java. I have a textarea and I wanted to read line by line, which I could already solve. But what I want is that each one of those lines write it in an excel file. I have already managed to create an excel file and write to it. What the code below does is that it reads line by line and writes it in excel, but I don't know how many lines the textarea can have, so if in my code below I write more than 3 lines in the textarea, It will only write the first 3 in excel and if I write less than 3 it throws this error: ArrayIndexOutOfBoundsException. I want to write all the lines that I want in the textarea and that they are written in excel. Thanks

String[] lines = TextArea.getText().split(&quot;\\n&quot;);
    for(int i = 0 ; i&lt; lines.length; i++){
        System.out.println( lines[i]);

    String [][] entry = new String [40][40];
    entrada [0] [0]= &quot;A1&quot;;
    entrada [1] [0]= lines[0];
    entrada [2] [0]= lines[1];
    entrada [3] [0]= lines[2];

    String route=&quot;/Users/sushi/Documents/writexcel.xls&quot;;
    g.generateExcel(entry, route);
    }

huangapple
  • 本文由 发表于 2020年5月30日 02:37:20
  • 转载请务必保留本文链接:https://java.coder-hub.com/62092621.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定