本系列教程将为大家带来Spire.Doc for .NET在使用过程中的各类实际操作,本篇文章介绍了如何在Word文档中创建表格并插入图片。
更多资源查看:Spire.XLS系列教程 | Spire.Doc系列教程 | Spire.PDF系列教程
Spire.Doc for .NET是一个专业的Word .NET库,设计用于帮助开发人员高效地开发创建、阅读、编写、转换和打印任何来自.NET( C#, VB.NET, ASP.NET)平台的Word文档文件的功能。
本系列教程将为大家带来Spire.Doc for .NET在使用过程中的各类实际操作,本篇文章介绍了如何在Word文档中创建表格并插入图片。>>下载Spire.Doc最新试用版体验
在Word文档中,表格可以帮助我们清晰、直观的分析和整理数据。一个表格通常至少包含一行,每行至少包含一个单元格,一个单元格可以包含多种元素如文本和表格(即嵌套表格)等。
C# 创建 Word 表格
在创建表格时,可以预先指定好表格的行数和列数,也可以通过动态的方式向表格中添加行和单元格。
创建预定义行数和列数的表格
通过Table.ResetCells(int rowsNum, int columnsNum)方法来创建一个预先定义好行数和列数的表格。代码示例:
//创建Word文档 Document document = new Document();//添加sectionSection section = document.AddSection();//添加表格Table table = section.AddTable(true);//指定表格的行数和列数(2行,3列)table.ResetCells(2, 3);//获取单元格(第1行第1个单元格)并添加文本TextRange range = table[0, 0].AddParagraph().AppendText("产品"); range.CharacterFormat.FontName = "Arial";range.CharacterFormat.FontSize = 12;range.CharacterFormat.TextColor = Color.Teal;range.CharacterFormat.Bold = true;//获取单元格(第1行第2个单元格)并添加文本range = table[0, 1].AddParagraph().AppendText("单价");range.CharacterFormat.FontName = "Arial";range.CharacterFormat.FontSize = 12;range.CharacterFormat.TextColor = Color.Teal;range.CharacterFormat.Bold = true;//获取单元格(第1行第3个单元格)并添加文本range = table[0, 2].AddParagraph().AppendText("数量");range.CharacterFormat.FontName = "Arial";range.CharacterFormat.FontSize = 12;range.CharacterFormat.TextColor = Color.Teal;range.CharacterFormat.Bold = true;//获取单元格(第2行第1个单元格)并添加文本range = table[1, 0].AddParagraph().AppendText("A");range.CharacterFormat.FontName = "Arial";range.CharacterFormat.FontSize = 10;//获取单元格(第2行第2个单元格)并添加文本range = table[1, 1].AddParagraph().AppendText("¥1800");range.CharacterFormat.FontName = "Arial";range.CharacterFormat.FontSize = 10;//获取单元格(第2行第3个单元格)并添加文本range = table[1, 2].AddParagraph().AppendText("10");range.CharacterFormat.FontName = "Arial";range.CharacterFormat.FontSize = 10;//保存文档document.SaveToFile("Table.docx");

动态向表格添加行和单元格
如果需要动态地向表格添加行和单元格,需要使用Table.AddRow() 方法和 TableRow.AddCell()方法。代码示例:
//创建Word文档Document doc = new Document();//添加sectionSection section = doc.AddSection();//添加表格Table table = section.AddTable(true);//添加第1行TableRow row1 = table.AddRow();//添加第1个单元格到第1行TableCell cell1 = row1.AddCell();cell1.AddParagraph().AppendText("姓 名");//添加第2个单元格到第1行TableCell cell2 = row1.AddCell();cell2.AddParagraph().AppendText("年 龄");//添加第2行TableRow row2 = table.AddRow(true,false);//添加第1个单元格到第2行TableCell cell3 = row2.AddCell();cell3.AddParagraph().AppendText("约 翰");//添加第2个单元格到第2行TableCell cell4 = row2.AddCell();cell4.AddParagraph().AppendText("21");table.AutoFit(AutoFitBehaviorType.AutoFitToWindow);//保存文档doc.SaveToFile("Table2.docx");

创建嵌套表格
使用Body.AddTable()方法来向一个表格中的指定单元格添加一个嵌套表格。代码示例:
//创建Word文档Document doc = new Document();Section section = doc.AddSection();//添加表格Table table = section.AddTable(true);table.ResetCells(2, 3);//设置行高和列宽table.Rows[0].Height = 20;table.Rows[1].Height = 50;table.Rows[0].Cells[0].Width = table.Rows[0].Cells[2].Width = 50;table.Rows[1].Cells[0].Width = table.Rows[1].Cells[2].Width = 50;table.AutoFit(AutoFitBehaviorType.AutoFitToWindow);//添加文本到单元格table[0, 0].AddParagraph().AppendText("学 ");table[0, 1].AddParagraph().AppendText("姓 名");table[0, 2].AddParagraph().AppendText("成 绩");table[1, 0].AddParagraph().AppendText("01");table[1, 1].AddParagraph().AppendText("张 三");table[1, 2].AddParagraph().AppendText("详 情");//添加嵌套表格到第2行第3个单元格Table nestedTable = table[1, 2].AddTable(true);nestedTable.ResetCells(3, 2);nestedTable.AutoFit(AutoFitBehaviorType.AutoFitToContents);//添加文本到嵌套表格nestedTable[0, 0].AddParagraph().AppendText("科 目");nestedTable[0, 1].AddParagraph().AppendText("成 绩");nestedTable[1, 0].AddParagraph().AppendText("语 文");nestedTable[1, 1].AddParagraph().AppendText("88");nestedTable[2, 0].AddParagraph().AppendText("数 学");nestedTable[2, 1].AddParagraph().AppendText("95");//保存文档doc.SaveToFile("Nested_Table.docx", FileFormat.Docx2013);
C# 在 word 表格中插入图片
Spire.Doc 提供 TableCollection 类,我们可以获取指定的表格,然后调用 DocPicture Paragraph.AppendPicture(Image image) 方法插入图片到单元格。
//创建一个document实例并加载示例文档Document doc = new Document();doc.LoadFromFile("Sample.docx");//获取第一个tableTable table1 = (Table)doc.Sections[0].Tables[0];//插入图片到表格并设置图片宽度和高度 DocPicture picture = table1.Rows[1].Cells[2].Paragraphs[0].AppendPicture(Image.FromFile("Logo.png"));picture.Width = 110;picture.Height = 90;//保存文档 doc.SaveToFile("Result.docx", FileFormat.Docx);

*购买Spire.Doc正版授权的朋友可以点击“咨询在线客服”哦~~
标签:
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!