轉帖|使用教程|編輯:李顯亮|2019-08-01 11:17:52.107|閱讀 1316 次
概述:本系列教程將為大家帶來Spire.Doc for .NET在使用過程中的各類實際操作,本篇文章介紹了如何在Word文檔中創建表格并插入圖片。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
更多資源查看: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文檔中,表格可以幫助我們清晰、直觀的分析和整理數據。一個表格通常至少包含一行,每行至少包含一個單元格,一個單元格可以包含多種元素如文本和表格(即嵌套表格)等。
在創建表格時,可以預先指定好表格的行數和列數,也可以通過動態的方式向表格中添加行和單元格。
創建預定義行數和列數的表格
通過Table.ResetCells(int rowsNum, int columnsNum)方法來創建一個預先定義好行數和列數的表格。代碼示例:
//創建Word文檔 Document document = new Document(); //添加section Section 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(); //添加section Section 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);
Spire.Doc 提供 TableCollection 類,我們可以獲取指定的表格,然后調用 DocPicture Paragraph.AppendPicture(Image image) 方法插入圖片到單元格。
//創建一個document實例并加載示例文檔 Document doc = new Document(); doc.LoadFromFile("Sample.docx"); //獲取第一個table Table 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正版授權的朋友可以點擊哦~~
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn