如何在Java中动态将数据打印到MS Word表格中

huangapple go评论101阅读模式
英文:

How to print data to a ms word table dynamically in Java

问题

我有一个 List<String[]>,我从一个CSV文件中填充了它。

  1. List<String[]> csvData = ReadCSV.readAll();

所以我需要将这些数据放入一个表格中,这个表格有很多行,取决于CSV列表的大小,但只有2列。所以我需要将这些数据以“之”字形的顺序打印到一个Word文档中,如下所示:

  1. _______________________________
  2. | 数据一 | 数据二 |
  3. _______________________________
  4. | 数据三 | 数据四 |
  5. _______________________________
  6. | 数据五 | 数据六 |
  7. _______________________________

所以我对如何做到这一点并同时获取两个String[]的数据感到有点困惑。因为 List<String[]> 的结构如下:

  1. [第一个, 数据一]
  2. [第二个, 数据二]
  3. [第三个, 数据三]
  4. [第四个, 数据四]

所以我尝试如下打印数据:

  1. for (int i = 0; i < csvData.size(); i = i + 2) {
  2. String[] arrayOne = csvData.get(i);
  3. String[] arrayTwo = csvData.get(i + 1);
  4. // 在Word文档中打印
  5. table.getRow(i).getCell(0).setParagraph(包含arrayOne数据的段落);
  6. table.getRow(i).getCell(1).setParagraph(包含arrayTwo数据的段落);
  7. }

但是使用这种方法,如果CSV文件中的数据数量为奇数,则会出现错误,并且 table.getRow(i) 也无法正常工作。我还尝试过数组合并,但当CSV列表中的数据数量为奇数时,它不起作用。那么实现我所需的最佳方法是什么?有谁可以帮助我实现这个?提前谢谢。

英文:

I have a List&lt;String[]&gt; and I populate that from a CSV file.

  1. List&lt;String[]&gt; csvData = ReadCSV.readAll();

So I need to put those data into a table that has many rows depending on the CSV list size but there are only 2 columns. So I need to print those data into a word document in the Zig-Zag order as follows.

  1. _______________________________
  2. | Data One | Data Two |
  3. _______________________________
  4. | Data Three | Data Four |
  5. _______________________________
  6. | Data Five | Data Six |
  7. _______________________________

So I am a little confused with this how to do this and get two String[] data at a time. Because the List&lt;String[]&gt; is like the following,

  1. [1st, Data One]
  2. [2nd, Data Two]
  3. [3rd, Data Three]
  4. [4th, Data Four]

So I tried to print the data as follows,

  1. for (int i = 0; i &lt; csvData.size(); i = i + 2) {
  2. String[] arrayOne = csvData.get(i);
  3. String[] arrayTwo = csvData.get(i + 1);
  4. // Print in word
  5. table.getRow(i).getCell(0).setParagraph(paragraph that contains arrayOne data);
  6. table.getRow(i).getCell(1).setParagraph(paragraph that contains arrayTwo data);
  7. }

But with this approach, I am getting errors if there is an odd number of data in the CSV file and the table.getRow(i) is also not working correctly. I tried with array merging also but didn't work when the CSV list has an odd number of data. So what is the best approach to do what I need? Anybody can help me to achieve this? Thanks in advance.

答案1

得分: 2

如果表格应该有两列,那么需要使用以下代码来计算行数:int rows = (int)Math.ceil(csvData.size()/2d);

要填充表格,你需要使用迭代器 r 来循环行,以及迭代器 c 来循环列,还需要使用迭代器 i 来迭代列表。只有当 i &lt; csvData.size() 时才需要设置单元格的值。

完整的工作示例代码如下:

  1. import java.io.FileOutputStream;
  2. import org.apache.poi.xwpf.usermodel.*;
  3. import java.util.*;
  4. public class CreateWordTableFromList {
  5. public static void main(String[] args) throws Exception {
  6. List<String[]> csvData = new ArrayList<String[]>();
  7. csvData.add(new String[]{"1st", "Data One"});
  8. csvData.add(new String[]{"2nd", "Data Two"});
  9. csvData.add(new String[]{"3rd", "Data Three"});
  10. csvData.add(new String[]{"4th", "Data Four"});
  11. csvData.add(new String[]{"5th", "Data Five"});
  12. csvData.add(new String[]{"6th", "Data Six"});
  13. csvData.add(new String[]{"7th", "Data Seven"});
  14. //csvData.add(new String[]{"8th", "Data Eight"});
  15. XWPFDocument document = new XWPFDocument();
  16. XWPFParagraph paragraph = document.createParagraph();
  17. XWPFRun run = paragraph.createRun();
  18. run.setText("The table");
  19. int cols = 2;
  20. int rows = (int)Math.ceil(csvData.size()/2d);
  21. XWPFTable table = document.createTable(rows, cols);
  22. XWPFTableRow row;
  23. XWPFTableCell cell;
  24. int i = 0;
  25. for (int r = 0; r < rows; r++) {
  26. row = table.getRow(r);
  27. for (int c = 0; c < cols; c++) {
  28. cell = row.getCell(c);
  29. if (i < csvData.size()) cell.setText(csvData.get(i)[1]);
  30. i++;
  31. }
  32. }
  33. FileOutputStream out = new FileOutputStream("CreateWordTableFromList.docx");
  34. document.write(out);
  35. out.close();
  36. document.close();
  37. }
  38. }

请注意,这段代码用于创建一个Word文档,并在文档中插入一个包含CSV数据的表格。

英文:

If the table shall have two columns, then it needs int rows = (int)Math.ceil(csvData.size()/2d);.

To fill the table you need iterator r for rows as well as iterator c for columns as well as iterator i for iterating the list. There is only a cell value to set if i &lt; csvData.size().

Complete working example:

  1. import java.io.FileOutputStream;
  2. import org.apache.poi.xwpf.usermodel.*;
  3. import java.util.*;
  4. public class CreateWordTableFromList {
  5. public static void main(String[] args) throws Exception {
  6. List&lt;String[]&gt; csvData = new ArrayList&lt;String[]&gt;();
  7. csvData.add(new String[]{&quot;1st&quot;, &quot;Data One&quot;});
  8. csvData.add(new String[]{&quot;2nd&quot;, &quot;Data Two&quot;});
  9. csvData.add(new String[]{&quot;3rd&quot;, &quot;Data Three&quot;});
  10. csvData.add(new String[]{&quot;4th&quot;, &quot;Data Four&quot;});
  11. csvData.add(new String[]{&quot;5th&quot;, &quot;Data Five&quot;});
  12. csvData.add(new String[]{&quot;6th&quot;, &quot;Data Six&quot;});
  13. csvData.add(new String[]{&quot;7th&quot;, &quot;Data Seven&quot;});
  14. //csvData.add(new String[]{&quot;8th&quot;, &quot;Data Eight&quot;});
  15. XWPFDocument document = new XWPFDocument();
  16. XWPFParagraph paragraph = document.createParagraph();
  17. XWPFRun run = paragraph.createRun();
  18. run.setText(&quot;The table&quot;);
  19. int cols = 2;
  20. int rows = (int)Math.ceil(csvData.size()/2d);
  21. XWPFTable table = document.createTable(rows, cols);
  22. XWPFTableRow row;
  23. XWPFTableCell cell;
  24. int i = 0;
  25. for (int r = 0; r &lt; rows; r++) {
  26. row = table.getRow(r);
  27. for (int c = 0; c &lt; cols; c++) {
  28. cell = row.getCell(c);
  29. if (i &lt; csvData.size()) cell.setText(csvData.get(i)[1]);
  30. i++;
  31. }
  32. }
  33. FileOutputStream out = new FileOutputStream(&quot;CreateWordTableFromList.docx&quot;);
  34. document.write(out);
  35. out.close();
  36. document.close();
  37. }
  38. }

答案2

得分: 1

为了扩展我的评论,您可以通过使用一个 if 语句来避免奇数个数据带来的问题:

  1. for (int i = 0; i < csvData.size(); i = i + 2) {
  2. String[] arrayOne = csvData.get(i);
  3. // 在单元格中打印数据
  4. table.getRow(i).getCell(0).setParagraph(包含 arrayOne 数据的段落);
  5. // 检查行中第二个数据是否存在,否则忽略
  6. if (csvData.size() > i){
  7. String[] arrayTwo = csvData.get(i + 1);
  8. table.getRow(i).getCell(1).setParagraph(包含 arrayTwo 数据的段落);
  9. }
  10. }
英文:

To expand on my comment, you can avoid the issues with an odd number of data by using an if statement:

  1. for (int i = 0; i &lt; csvData.size(); i = i + 2) {
  2. String[] arrayOne = csvData.get(i);
  3. // Print in word
  4. table.getRow(i).getCell(0).setParagraph(paragraph that contains arrayOne data);
  5. //Check if the second bit of data in a row exists, otherwise ignore it
  6. if (csvData.size() &gt; i){
  7. String[] arrayTwo = csvData.get(i + 1);
  8. table.getRow(i).getCell(1).setParagraph(paragraph that contains arrayTwo data);
  9. }
  10. }

huangapple
  • 本文由 发表于 2020年8月19日 07:18:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/63477932.html
匿名

发表评论

匿名网友

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

确定