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

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

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

问题

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

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

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

_______________________________
|   数据一      |   数据二      |
_______________________________
|   数据三      |   数据四      |
_______________________________
|   数据五      |   数据六      |
_______________________________

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

[第一个, 数据一]
[第二个, 数据二]
[第三个, 数据三]
[第四个, 数据四]

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

for (int i = 0; i < csvData.size(); i = i + 2) {
    String[] arrayOne = csvData.get(i);
    String[] arrayTwo = csvData.get(i + 1);

    // 在Word文档中打印
    table.getRow(i).getCell(0).setParagraph(包含arrayOne数据的段落);
    table.getRow(i).getCell(1).setParagraph(包含arrayTwo数据的段落);
}

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

英文:

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

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.

_______________________________
|   Data One   |   Data Two   |
_______________________________
|   Data Three |   Data Four  |
_______________________________
|   Data Five  |   Data Six   |
_______________________________

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,

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

So I tried to print the data as follows,

for (int i = 0; i &lt; csvData.size(); i = i + 2) {
    String[] arrayOne = csvData.get(i);
    String[] arrayTwo = csvData.get(i + 1);

    // Print in word
    table.getRow(i).getCell(0).setParagraph(paragraph that contains arrayOne data);
    table.getRow(i).getCell(1).setParagraph(paragraph that contains arrayTwo data);
}

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() 时才需要设置单元格的值。

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

import java.io.FileOutputStream;
import org.apache.poi.xwpf.usermodel.*;
import java.util.*;

public class CreateWordTableFromList {

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

  List<String[]> csvData = new ArrayList<String[]>();
  csvData.add(new String[]{"1st", "Data One"});
  csvData.add(new String[]{"2nd", "Data Two"});
  csvData.add(new String[]{"3rd", "Data Three"});
  csvData.add(new String[]{"4th", "Data Four"});
  csvData.add(new String[]{"5th", "Data Five"});
  csvData.add(new String[]{"6th", "Data Six"});
  csvData.add(new String[]{"7th", "Data Seven"});
  //csvData.add(new String[]{"8th", "Data Eight"});

  XWPFDocument document = new XWPFDocument();

  XWPFParagraph paragraph = document.createParagraph();
  XWPFRun run = paragraph.createRun();
  run.setText("The table");

  int cols = 2;
  int rows = (int)Math.ceil(csvData.size()/2d);
  XWPFTable table = document.createTable(rows, cols);
  XWPFTableRow row;
  XWPFTableCell cell;
  int i = 0;
  for (int r = 0; r < rows; r++) {
   row = table.getRow(r);
   for (int c = 0; c < cols; c++) {
    cell = row.getCell(c);
    if (i < csvData.size()) cell.setText(csvData.get(i)[1]);
    i++;
   }
  }

  FileOutputStream out = new FileOutputStream("CreateWordTableFromList.docx");
  document.write(out);
  out.close();
  document.close();

 }
}

请注意,这段代码用于创建一个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:

import java.io.FileOutputStream;
import org.apache.poi.xwpf.usermodel.*;
import java.util.*;
public class CreateWordTableFromList {
public static void main(String[] args) throws Exception {
List&lt;String[]&gt; csvData = new ArrayList&lt;String[]&gt;();
csvData.add(new String[]{&quot;1st&quot;, &quot;Data One&quot;});
csvData.add(new String[]{&quot;2nd&quot;, &quot;Data Two&quot;});
csvData.add(new String[]{&quot;3rd&quot;, &quot;Data Three&quot;});
csvData.add(new String[]{&quot;4th&quot;, &quot;Data Four&quot;});
csvData.add(new String[]{&quot;5th&quot;, &quot;Data Five&quot;});
csvData.add(new String[]{&quot;6th&quot;, &quot;Data Six&quot;});
csvData.add(new String[]{&quot;7th&quot;, &quot;Data Seven&quot;});
//csvData.add(new String[]{&quot;8th&quot;, &quot;Data Eight&quot;});
XWPFDocument document = new XWPFDocument();
XWPFParagraph paragraph = document.createParagraph();
XWPFRun run = paragraph.createRun();  
run.setText(&quot;The table&quot;);
int cols = 2;
int rows = (int)Math.ceil(csvData.size()/2d);
XWPFTable table = document.createTable(rows, cols);
XWPFTableRow row;
XWPFTableCell cell;
int i = 0;
for (int r = 0; r &lt; rows; r++) {
row = table.getRow(r);
for (int c = 0; c &lt; cols; c++) {
cell = row.getCell(c);
if (i &lt; csvData.size()) cell.setText(csvData.get(i)[1]);
i++;
}
}
FileOutputStream out = new FileOutputStream(&quot;CreateWordTableFromList.docx&quot;);
document.write(out);
out.close();
document.close();
}
}

答案2

得分: 1

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

for (int i = 0; i < csvData.size(); i = i + 2) {
    String[] arrayOne = csvData.get(i);
    // 在单元格中打印数据
    table.getRow(i).getCell(0).setParagraph(包含 arrayOne 数据的段落);

    // 检查行中第二个数据是否存在,否则忽略
    if (csvData.size() > i){
        String[] arrayTwo = csvData.get(i + 1);
        table.getRow(i).getCell(1).setParagraph(包含 arrayTwo 数据的段落);
    }
}
英文:

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

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

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:

确定