poi生成6行4列的表格
时间: 2024-02-11 10:06:31 浏览: 22
可以使用以下代码生成一个6行4列的表格:
```java
import java.io.FileOutputStream;
import org.apache.poi.xwpf.usermodel.*;
public class TableExample {
public static void main(String[] args) throws Exception {
XWPFDocument doc = new XWPFDocument();
// 创建一个6行4列的表格
int rows = 6;
int cols = 4;
XWPFTable table = doc.createTable(rows, cols);
// 设置表格样式
table.getCTTbl().addNewTblGrid().addNewGridCol().setW(BigInteger.valueOf(1500));
table.getCTTbl().getTblGrid().addNewGridCol().setW(BigInteger.valueOf(1500));
table.getCTTbl().getTblGrid().addNewGridCol().setW(BigInteger.valueOf(1500));
table.getCTTbl().getTblGrid().addNewGridCol().setW(BigInteger.valueOf(1500));
// 设置表格内容
for (int i = 0; i < rows; i++) {
XWPFTableRow row = table.getRow(i);
for (int j = 0; j < cols; j++) {
XWPFTableCell cell = row.getCell(j);
cell.setText("Row " + (i+1) + ", Col " + (j+1));
}
}
// 将文档写入到文件中
FileOutputStream out = new FileOutputStream("table.docx");
doc.write(out);
out.close();
System.out.println("表格生成成功!");
}
}
```
这段代码将生成一个包含6行4列的表格,每一列的宽度都设置为1500,每个单元格的内容为“Row i, Col j”,最后将文档写入到名为“table.docx”的文件中。