C# npoi向word插入表格
时间: 2023-07-12 14:08:12 浏览: 340
C# NPOI生成word插入图片和表格
4星 · 用户满意度95%
可以使用 NPOI 库来向 Word 文档中插入表格。以下是一个示例代码:
```csharp
using System;
using System.IO;
using NPOI.XWPF.UserModel;
using NPOI.OpenXmlFormats.Wordprocessing;
namespace InsertTableInWord
{
class Program
{
static void Main(string[] args)
{
// 创建 Word 文档对象
XWPFDocument doc = new XWPFDocument();
// 创建一个段落对象
XWPFParagraph paragraph = doc.CreateParagraph();
// 创建一个表格对象
XWPFTable table = doc.CreateTable(3, 3);
// 设置表格样式
CT_TblPr tblPr = table.GetCTTbl().tblPr;
tblPr.tblBorders = new CT_TblBorders();
tblPr.tblBorders.top = new CT_Border();
tblPr.tblBorders.top.val = ST_Border.single;
tblPr.tblBorders.bottom = new CT_Border();
tblPr.tblBorders.bottom.val = ST_Border.single;
tblPr.tblBorders.left = new CT_Border();
tblPr.tblBorders.left.val = ST_Border.single;
tblPr.tblBorders.right = new CT_Border();
tblPr.tblBorders.right.val = ST_Border.single;
tblPr.tblLayout = new CT_TblLayoutType();
tblPr.tblLayout.type = ST_TblLayoutType.autofit;
// 设置表格内容
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
// 获取单元格对象
XWPFTableCell cell = table.GetRow(i).GetCell(j);
// 设置单元格内容
cell.SetText("Row " + i + ", Column " + j);
}
}
// 将表格添加到段落中
paragraph = doc.CreateParagraph();
paragraph.Alignment = ParagraphAlignment.CENTER;
paragraph.VerticalAlignment = TextAlignment.CENTER;
XWPFRun run = paragraph.CreateRun();
run.SetText("Table Title");
run.IsBold = true;
run.FontSize = 14;
run.FontFamily = "Times New Roman";
run.SetTextPosition(20);
paragraph = doc.CreateParagraph();
paragraph.SpacingBefore = 200;
paragraph.SpacingAfter = 200;
paragraph.Alignment = ParagraphAlignment.CENTER;
paragraph.VerticalAlignment = TextAlignment.CENTER;
paragraph.CreateRun().SetText("This is a sample table:");
paragraph = doc.CreateParagraph();
paragraph.Alignment = ParagraphAlignment.CENTER;
paragraph.VerticalAlignment = TextAlignment.CENTER;
paragraph.CreateRun().SetText("");
paragraph = doc.CreateParagraph();
paragraph.Alignment = ParagraphAlignment.CENTER;
paragraph.VerticalAlignment = TextAlignment.CENTER;
paragraph.CreateRun().SetText("");
paragraph = doc.CreateParagraph();
paragraph.Alignment = ParagraphAlignment.CENTER;
paragraph.VerticalAlignment = TextAlignment.CENTER;
paragraph.CreateRun().SetText("");
paragraph = doc.CreateParagraph();
paragraph.Alignment = ParagraphAlignment.CENTER;
paragraph.VerticalAlignment = TextAlignment.CENTER;
paragraph.CreateRun().SetText("");
paragraph = doc.CreateParagraph();
paragraph.Alignment = ParagraphAlignment.CENTER;
paragraph.VerticalAlignment = TextAlignment.CENTER;
paragraph.CreateRun().SetText("");
paragraph = doc.CreateParagraph();
paragraph.Alignment = ParagraphAlignment.CENTER;
paragraph.VerticalAlignment = TextAlignment.CENTER;
paragraph.CreateRun().SetText("");
paragraph = doc.CreateParagraph();
paragraph.Alignment = ParagraphAlignment.CENTER;
paragraph.VerticalAlignment = TextAlignment.CENTER;
paragraph.CreateRun().SetText("");
paragraph = doc.CreateParagraph();
paragraph.Alignment = ParagraphAlignment.CENTER;
paragraph.VerticalAlignment = TextAlignment.CENTER;
paragraph.CreateRun().SetText("");
paragraph = doc.CreateParagraph();
paragraph.Alignment = ParagraphAlignment.CENTER;
paragraph.VerticalAlignment = TextAlignment.CENTER;
paragraph.CreateRun().SetText("");
paragraph = doc.CreateParagraph();
paragraph.Alignment = ParagraphAlignment.CENTER;
paragraph.VerticalAlignment = TextAlignment.CENTER;
paragraph.CreateRun().SetText("");
paragraph = doc.CreateParagraph();
paragraph.Alignment = ParagraphAlignment.CENTER;
paragraph.VerticalAlignment = TextAlignment.CENTER;
paragraph.CreateRun().SetText("");
paragraph = doc.CreateParagraph();
paragraph.Alignment = ParagraphAlignment.CENTER;
paragraph.VerticalAlignment = TextAlignment.CENTER;
paragraph.CreateRun().SetText("");
paragraph = doc.CreateParagraph();
paragraph.Alignment = ParagraphAlignment.CENTER;
paragraph.VerticalAlignment = TextAlignment.CENTER;
paragraph.CreateRun().SetText("");
paragraph = doc.CreateParagraph();
paragraph.Alignment = ParagraphAlignment.CENTER;
paragraph.VerticalAlignment = TextAlignment.CENTER;
paragraph.CreateRun().SetText("");
paragraph = doc.CreateParagraph();
paragraph.Alignment = ParagraphAlignment.CENTER;
paragraph.VerticalAlignment = TextAlignment.CENTER;
paragraph.CreateRun().SetText("");
paragraph = doc.CreateParagraph();
paragraph.Alignment = ParagraphAlignment.CENTER;
paragraph.VerticalAlignment = TextAlignment.CENTER;
paragraph.CreateRun().SetText("");
paragraph = doc.CreateParagraph();
paragraph.Alignment = ParagraphAlignment.CENTER;
paragraph.VerticalAlignment = TextAlignment.CENTER;
paragraph.CreateRun().SetText("");
paragraph = doc.CreateParagraph();
paragraph.Alignment = ParagraphAlignment.CENTER;
paragraph.VerticalAlignment = TextAlignment.CENTER;
paragraph.CreateRun().SetText("");
paragraph = doc.CreateParagraph();
paragraph.Alignment = ParagraphAlignment.CENTER;
paragraph.VerticalAlignment = TextAlignment.CENTER;
paragraph.CreateRun().SetText("");
// 保存 Word 文档
using (FileStream stream = new FileStream("table.docx", FileMode.Create))
{
doc.Write(stream);
}
}
}
}
```
在上面的示例代码中,首先创建了一个 Word 文档对象,然后创建了一个段落对象和一个表格对象,并且设置了表格的样式和内容。接着将表格添加到段落中,并设置段落的样式。最后将文档保存到指定的文件中。
注意:上面的代码示例中,为了使表格居中,特意在表格前添加了多个空行来进行排版。实际使用时,可以根据需要调整段落的样式和排版方式。
阅读全文