轉(zhuǎn)帖|使用教程|編輯:李顯亮|2019-08-01 11:17:52.107|閱讀 1316 次
概述:本系列教程將為大家帶來Spire.Doc for .NET在使用過程中的各類實(shí)際操作,本篇文章介紹了如何在Word文檔中創(chuàng)建表格并插入圖片。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
更多資源查看:Spire.XLS系列教程 | Spire.Doc系列教程 | Spire.PDF系列教程
Spire.Doc for .NET是一個專業(yè)的Word .NET庫,設(shè)計用于幫助開發(fā)人員高效地開發(fā)創(chuàng)建、閱讀、編寫、轉(zhuǎn)換和打印任何來自.NET( C#, VB.NET, ASP.NET)平臺的Word文檔文件的功能。
本系列教程將為大家?guī)?strong>Spire.Doc for .NET在使用過程中的各類實(shí)際操作,本篇文章介紹了如何在Word文檔中創(chuàng)建表格并插入圖片。>>下載Spire.Doc最新試用版體驗(yàn)
在Word文檔中,表格可以幫助我們清晰、直觀的分析和整理數(shù)據(jù)。一個表格通常至少包含一行,每行至少包含一個單元格,一個單元格可以包含多種元素如文本和表格(即嵌套表格)等。
在創(chuàng)建表格時,可以預(yù)先指定好表格的行數(shù)和列數(shù),也可以通過動態(tài)的方式向表格中添加行和單元格。
創(chuàng)建預(yù)定義行數(shù)和列數(shù)的表格
通過Table.ResetCells(int rowsNum, int columnsNum)方法來創(chuàng)建一個預(yù)先定義好行數(shù)和列數(shù)的表格。代碼示例:
//創(chuàng)建Word文檔 Document document = new Document(); //添加section Section section = document.AddSection(); //添加表格 Table table = section.AddTable(true); //指定表格的行數(shù)和列數(shù)(2行,3列) table.ResetCells(2, 3); //獲取單元格(第1行第1個單元格)并添加文本 TextRange range = table[0, 0].AddParagraph().AppendText("產(chǎn)品"); 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("數(shù)量"); 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");
動態(tài)向表格添加行和單元格
如果需要動態(tài)地向表格添加行和單元格,需要使用Table.AddRow() 方法和 TableRow.AddCell()方法。代碼示例:
//創(chuàng)建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");
創(chuàng)建嵌套表格
使用Body.AddTable()方法來向一個表格中的指定單元格添加一個嵌套表格。代碼示例:
//創(chuàng)建Word文檔 Document doc = new Document(); Section section = doc.AddSection(); //添加表格 Table table = section.AddTable(true); table.ResetCells(2, 3); //設(shè)置行高和列寬 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("學(xué) 號"); 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("數(shù) 學(xué)"); nestedTable[2, 1].AddParagraph().AppendText("95"); //保存文檔 doc.SaveToFile("Nested_Table.docx", FileFormat.Docx2013);
Spire.Doc 提供 TableCollection 類,我們可以獲取指定的表格,然后調(diào)用 DocPicture Paragraph.AppendPicture(Image image) 方法插入圖片到單元格。
//創(chuàng)建一個document實(shí)例并加載示例文檔 Document doc = new Document(); doc.LoadFromFile("Sample.docx"); //獲取第一個table Table table1 = (Table)doc.Sections[0].Tables[0]; //插入圖片到表格并設(shè)置圖片寬度和高度 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正版授權(quán)的朋友可以點(diǎn)擊哦~~
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn