英文:
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("outNumbers.csv"));
sys1.stream().forEach(i->outFile.print(i + ", "));
sys2.stream().forEach(i->outFile.print(i + ", "));
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("\n");` before your two line of forEach
"\n" is the character which says "go to the next line"
</details>
专注分享java语言的经验与见解,让所有开发者获益!
评论