11月优惠进行时,消费满额即享折上豪礼,想买Spire.Doc的朋友赶快咨询在线客服吧!
推荐阅读:【想要快速完成文档格式转换吗pire系列组件格式转换完整攻略来啦!】
C# 插入表格到 Word 文本框以及获取和删除 Word 文本框中的表格
插入表格
//创建Document实例Document document = new Document();//添加节Section section = document.AddSection();//添加段落Paragraph paragraph = section.AddParagraph();//添加文本框到段落,并指定文本框的宽度和高度TextBox textbox = paragraph.AppendTextBox(300, 100);//添加文本到文本框Paragraph textboxParagraph = textbox.Body.AddParagraph();TextRange textboxRange = textboxParagraph.AppendText("Table 1");textboxRange.CharacterFormat.FontName = "Arial";//插入表格到文本框Table table = textbox.Body.AddTable(true);//指定表格的行数和列数table.ResetCells(4, 4);string[,] data = new string[,] { {"姓名","年龄","性别","工 " }, {"张三","28","男","0023" }, {"李四","30","男","0024" }, {"王五","26","女","0025" } };//将数组内容填充到表格 for (int i = 0; i < 4; i++){ for (int j = 0; j < 4; j++) { TextRange tableRange = table[i, j].AddParagraph().AppendText(data[i, j]); tableRange.CharacterFormat.FontName = "Arial"; }}//给表格应用样式table.ApplyStyle(DefaultTableStyle.LightGridAccent3);//保存文档document.SaveToFile("AddTable.docx", FileFormat.Docx2013);

获取表格
//载入Word文档Document document = new Document("AddTable.docx");//获取第一个文本框TextBox textbox = document.TextBoxes[0];//获取文本框中第一个表格Table table = textbox.Body.Tables[0] as Table;StringBuilder sb = new StringBuilder();//遍历表格中的段落并提取文本foreach (TableRow row in table.Rows){ foreach (TableCell cell in row.Cells) { foreach (Paragraph paragraph in cell.Paragraphs) { sb.AppendLine(paragraph.Text); } }}File.WriteAllText("text.txt", sb.ToString());

删除表格
//创建Document实例Document document = new Document("AddTable.docx");//获取第一个文本框TextBox textbox = document.TextBoxes[0];//删除文本框中第一个表格textbox.Body.Tables.RemoveAt(0);//保存文档document.SaveToFile("RemoveTable.docx", FileFormat.Docx2013);

标签:
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!