JAVA – 从列表中创建一个格式化的Excel表格(.csv)

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

JAVA - Create a formatted table in excel (.csv) from lists

问题

我有一个用Java编写的算法内容太长无法在这里完整展示),它可以将一列数字转换成Excel CSV文档
public static void main(String[] args) throws IOException, InterruptedException{
    SimulationSystem myObj = new SimulationSystem();
    List<Integer> sys1 = new ArrayList<>();

然后这部分内容:

    myObj.SystemAnalysis(1,20,20);
    sys1.add(average);

会以不同的数字重复多次,我希望这些数字成为表格的x和y值。示例:

|   | 20 | 40 | 80  | 160 |
20  |
40  |  **答案放在这里**
80  |
160 |
    System.out.println(sys1);
    PrintWriter outFile = new PrintWriter(new FileWriter("outNumbers.csv"));
    sys1.forEach(i -> outFile.print(i + ", "));
    sys2.forEach(i -> outFile.print(i + ", "));
    outFile.close();

目前代码生成的列表会在CSV文件中成为一行。
我应该如何达到这种格式? 非常感谢您的帮助!


<details>
<summary>英文:</summary>

I have a algorithm written in java (too long to post here) that translates a list of numbers into a excel csv document.

public static void main(String[] args) throws IOException, InterruptedException{
SimulationSystem myObj = new SimulationSystem();
List<Integer> sys1 = new ArrayList<>();

then this bit:
    myObj.SystemAnalysis(1,20,20);
    sys1.add(average);
is repeated many times with different numbers and I want these numbers to be the y and x of the table. Example:

| |20|40|80|160|
20|
40| with the answers in here
80|
160|

    System.out.println(sys1);
    PrintWriter outFile = new PrintWriter(new FileWriter(&quot;outNumbers.csv&quot;));
    sys1.stream().forEach(i-&gt;outFile.print(i + &quot;, &quot;));
    sys2.stream().forEach(i-&gt;outFile.print(i + &quot;, &quot;));
    outFile.close();
right now the code produces the list printed in a straight line in a csv file.
**How do I achieve this format?** Any help will be very appreciated thank you!

</details>


# 答案1
**得分**: 0

尝试在你的两行forEach之前执行`outFile.print("\n");`
"\n"是表示"前往下一行"的字符。

<details>
<summary>英文:</summary>

try to do `outFile.print(&quot;\n&quot;);` before your two line of forEach
&quot;\n&quot; is the character which says &quot;go to the next line&quot;


</details>



huangapple
  • 本文由 发表于 2020年4月8日 04:06:09
  • 转载请务必保留本文链接:https://java.coder-hub.com/61088534.html
匿名

发表评论

匿名网友

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

确定