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