表格是組織整理數(shù)據(jù)的一種重要手段,應(yīng)在生活中的方方面面。在Word文檔中將繁雜的文字表述內(nèi)容表格化,能快速、直接地獲取關(guān)鍵內(nèi)容信息。那么,通過C#,我們也可以在Word文檔中添加表格,這里將介紹兩種不同的表格添加方法。
創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),大安市企業(yè)網(wǎng)站建設(shè),大安市品牌網(wǎng)站建設(shè),網(wǎng)站定制,大安市網(wǎng)站建設(shè)報價,網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,大安市網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實用型網(wǎng)站。使用工具:Spire.Doc for .NET
使用方法:安裝后,添加引用dll文件到項目中即可
表格添加方法一:動態(tài)地向Word添加表格行和單元格內(nèi)容,需調(diào)用方法section. AddTable()、table. AddRow和row. AddCell()
using System; using Spire.Doc; using Spire.Doc.Documents; using Spire.Doc.Fields; using System.Drawing; namespace CreateTable_Doc { class Program { static void Main(string[] args) { //創(chuàng)建一個Document類實例,并添加section Document doc = new Document(); 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("設(shè)備名稱"); //添加第3個單元格到第1行 TableCell cell3 = row1.AddCell(); cell3.AddParagraph().AppendText("設(shè)備型號"); //添加第4個單元格到第1行 TableCell cell4 = row1.AddCell(); cell4.AddParagraph().AppendText("設(shè)備數(shù)量"); //添加第5個單元格到第1行 TableCell cell5 = row1.AddCell(); cell5.AddParagraph().AppendText("設(shè)備價格"); //添加表格第2行 TableRow row2 = table.AddRow(true, false); //添加第6個單元格到第2行 TableCell cell6 = row2.AddCell(); cell6.AddParagraph().AppendText("1"); //添加第7個單元格到第2行 TableCell cell7 = row2.AddCell(); cell7.AddParagraph().AppendText("機床"); //添加第8個單元格到第2行 TableCell cell8 = row2.AddCell(); cell8.AddParagraph().AppendText("M170010"); //添加第9個單元格到第2行 TableCell cell9 = row2.AddCell(); cell9.AddParagraph().AppendText("12"); //添加第10個單元格到第2行 TableCell cell10 = row2.AddCell(); cell10.AddParagraph().AppendText("8W"); table.AutoFitBehavior(AutoFitBehaviorType.wdAutoFitWindow); //保存文檔 doc.SaveToFile("Table.docx"); } } }
效果示例:
表格添加方法二:預(yù)定義表格行和列
using System; using Spire.Doc; using Spire.Doc.Fields; using System.Drawing; namespace CreateTable2_Word { class Program { static void Main(string[] args) { //創(chuàng)建一個Document類實例,并添加section Document document = new Document(); Section section = document.AddSection(); //添加表格指定表格的行數(shù)和列數(shù)(2行,5列) Table table = section.AddTable(true); table.ResetCells(2, 5); //獲取單元格(第1行第1個單元格)并添加文本內(nèi)容,設(shè)置字體字號顏色等(單元格中內(nèi)容及個性化設(shè)置可以根據(jù)需要來進行調(diào)整) TextRange range = table[0, 0].AddParagraph().AppendText("序列號"); range.CharacterFormat.FontName = "Arial"; range.CharacterFormat.FontSize = 12; range.CharacterFormat.TextColor = Color.Brown; range.CharacterFormat.Bold = true; //獲取單元格(第1行第2個單元格)并添加文本 range = table[0, 1].AddParagraph().AppendText("設(shè)備名稱"); range.CharacterFormat.FontName = "Arial"; range.CharacterFormat.FontSize = 12; range.CharacterFormat.TextColor = Color.Brown; 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.Brown; range.CharacterFormat.Bold = true; //獲取單元格(第1行第4個單元格)并添加文本 range = table[0, 3].AddParagraph().AppendText("設(shè)備數(shù)量"); range.CharacterFormat.FontName = "Arial"; range.CharacterFormat.FontSize = 12; range.CharacterFormat.TextColor = Color.Brown; range.CharacterFormat.Bold = true; //獲取單元格(第1行第5個單元格)并添加文本 range = table[0, 4].AddParagraph().AppendText("設(shè)備價格"); range.CharacterFormat.FontName = "Arial"; range.CharacterFormat.FontSize = 12; range.CharacterFormat.TextColor = Color.Brown; range.CharacterFormat.Bold = true; //獲取單元格(第2行第1個單元格)并添加文本 range = table[1, 0].AddParagraph().AppendText("1"); range.CharacterFormat.FontName = "Arial"; range.CharacterFormat.FontSize = 12; //獲取單元格(第2行第2個單元格)并添加文本 range = table[1, 1].AddParagraph().AppendText("機床"); range.CharacterFormat.FontName = "Arial"; range.CharacterFormat.FontSize = 12; //獲取單元格(第2行第3個單元格)并添加文本 range = table[1, 2].AddParagraph().AppendText("M170010"); range.CharacterFormat.FontName = "Arial"; range.CharacterFormat.FontSize = 12; //獲取單元格(第2行第4個單元格)并添加文本 range = table[1, 3].AddParagraph().AppendText("12"); range.CharacterFormat.FontName = "Arial"; range.CharacterFormat.FontSize = 12; //獲取單元格(第2行第5個單元格)并添加文本 range = table[1, 4].AddParagraph().AppendText("8W"); range.CharacterFormat.FontName = "Arial"; range.CharacterFormat.FontSize = 12; //保存文檔 document.SaveToFile("Table2.docx"); } } }
以上介紹的兩種方法中,你可以根據(jù)自己的需要添加內(nèi)容或者設(shè)置內(nèi)容格式等。如果覺得對你有用的話,歡迎轉(zhuǎn)載!感謝閱讀。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
網(wǎng)頁名稱:C#如何添加表格到Word文檔-創(chuàng)新互聯(lián)
當(dāng)前地址:http://www.rwnh.cn/article38/cepcpp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動態(tài)網(wǎng)站、網(wǎng)站設(shè)計、網(wǎng)站排名、品牌網(wǎng)站設(shè)計、云服務(wù)器、面包屑導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容