最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#借助Spire.Doc實(shí)現(xiàn)Word表格自動(dòng)排版

 更新時(shí)間:2026年02月28日 14:43:29   作者:缺點(diǎn)內(nèi)向  
在日常開(kāi)發(fā)中,利用C#自動(dòng)化生成Word報(bào)告是一項(xiàng)常見(jiàn)需求,本文將介紹使用 Spire.Doc for .NET 組件,通過(guò)C#代碼精確設(shè)置 C# Word 表格列寬,希望對(duì)大家有所幫助

在日常開(kāi)發(fā)中,利用C#自動(dòng)化生成Word報(bào)告是一項(xiàng)常見(jiàn)需求。然而,許多開(kāi)發(fā)者都曾遇到過(guò)排版難題:默認(rèn)生成的表格列寬往往不盡如人意,文字擁擠或布局松散。要精準(zhǔn)控制表格外觀,關(guān)鍵在于如何高效地調(diào)整表格列寬。本文將介紹使用 Spire.Doc for .NET 組件,通過(guò)C#代碼精確設(shè)置 C# Word 表格列寬的實(shí)用方法。

準(zhǔn)備工作

在開(kāi)始編碼前,請(qǐng)確保在項(xiàng)目中引用了 Spire.Doc 的 DLL 文件。該組件無(wú)需安裝 Microsoft Word,即可在 .NET 環(huán)境下高效操作 Word 文檔。我們將通過(guò)設(shè)置單元格寬度來(lái)實(shí)現(xiàn)列寬的控制,主要使用 SetCellWidth 方法,它可以指定寬度數(shù)值和單位類(lèi)型(如磅值或百分比)。

核心代碼實(shí)現(xiàn)

以下代碼展示了如何創(chuàng)建一個(gè)表格,并將第一列的寬度固定為 200 磅,第二列設(shè)置為頁(yè)面寬度的 50%。

// 創(chuàng)建Document對(duì)象
Document doc = new Document();
Section section = doc.AddSection();

// 添加一個(gè)2行2列的表格
Table table = section.AddTable(true);
table.ResetCells(2, 2);

// 遍歷每一行,設(shè)置第一列寬度為200磅
for (int i = 0; i < table.Rows.Count; i++)
{
    // 獲取行中的第一個(gè)單元格并設(shè)置寬度
    // 參數(shù):寬度值, 寬度類(lèi)型(點(diǎn)/磅)
    table.Rows[i].Cells[0].SetCellWidth(200, CellWidthType.Point);
    
    // 設(shè)置第二列寬度為50%
    table.Rows[i].Cells[1].SetCellWidth(50, CellWidthType.Percentage);
}

// 添加一些示例內(nèi)容
table[0, 0].AddParagraph().AppendText("固定寬度列");
table[0, 1].AddParagraph().AppendText("自適應(yīng)百分比列");

// 保存文檔
doc.SaveToFile("SetColumnWidth.docx", FileFormat.Docx);

在上述代碼中,CellWidthType.Point 用于指定絕對(duì)寬度(適用于對(duì)齊要求高的場(chǎng)景),而 CellWidthType.Percentage 則用于相對(duì)寬度,更適合響應(yīng)式布局。

知識(shí)擴(kuò)展

下面小編為大家整理了C#設(shè)置 Word 表格的格式的其他方法,希望對(duì)大家有所幫助

表格樣式設(shè)置

1.內(nèi)置樣式設(shè)置

//載入文檔
Document document = new Document("Table.docx");
//獲取第一個(gè)節(jié)
Section section = document.Sections[0];

//獲取第一個(gè)表格
Table table = section.Tables[0] as Table;

//給表格應(yīng)用內(nèi)置樣式
table.ApplyStyle(DefaultTableStyle.LightGridAccent3);

//保存文檔
document.SaveToFile("BuiltinStyle.docx", FileFormat.Docx2013);

2.自定義段落樣式設(shè)置

//載入文檔
Document document = new Document("Table.docx");
//獲取第一個(gè)節(jié)
Section section = document.Sections[0];

//獲取第一個(gè)表格
Table table = section.Tables[0] as Table;

//設(shè)置自定義樣式
ParagraphStyle style = new ParagraphStyle(document);
style.Name = "TableStyle";            
style.CharacterFormat.FontSize = 14;
style.CharacterFormat.TextColor = Color.SeaGreen;
style.CharacterFormat.HighlightColor = Color.Yellow;
//將自定義樣式添加到文檔
document.Styles.Add(style);

//給表格第一行第一個(gè)單元格的第一個(gè)段落應(yīng)用自定義樣式
table[0,0].Paragraphs[0].ApplyStyle(style.Name);

//保存文檔
document.SaveToFile("CustomStyle.docx", FileFormat.Docx2013);

3.自適應(yīng)方式設(shè)置

//載入文檔
Document document = new Document("Table.docx");
//獲取第一個(gè)節(jié)
Section section = document.Sections[0];

//獲取第一個(gè)表格
Table table = section.Tables[0] as Table;

//自適應(yīng)內(nèi)容
table.AutoFit(AutoFitBehaviorType.AutoFitToContents);
//自適應(yīng)窗口
//table.AutoFit(AutoFitBehaviorType.AutoFitToWindow);
//固定列寬
//table.AutoFit(AutoFitBehaviorType.FixedColumnWidths);

//保存文檔
document.SaveToFile("AutofitMode.docx", FileFormat.Docx2013);

表格對(duì)齊方式設(shè)置

//載入文檔
Document document = new Document("Tables.docx");
//獲取第一個(gè)節(jié)
Section section = document.Sections[0];

//獲取第一、二、三個(gè)表格
Table table1 = section.Tables[0] as Table;
Table table2 = section.Tables[1] as Table;
Table table3 = section.Tables[2] as Table;

//設(shè)置第一個(gè)表格居左
table1.TableFormat.HorizontalAlignment = RowAlignment.Left;
table1.TableFormat.LeftIndent = 34;

//設(shè)置第二個(gè)表格居中
table2.TableFormat.HorizontalAlignment = RowAlignment.Center;
table2.TableFormat.LeftIndent = 34;

//設(shè)置第三個(gè)表格居右
table3.TableFormat.HorizontalAlignment = RowAlignment.Right;
table3.TableFormat.LeftIndent = 34;

//保存文檔
document.SaveToFile("TableAlignment.docx", FileFormat.Docx2013);

邊框設(shè)置

//載入文檔
Document document = new Document("Table.docx");
//獲取第一個(gè)節(jié)
Section section = document.Sections[0];

//獲取第一個(gè)表格
Table table = section.Tables[0] as Table;

//設(shè)置表格的上邊框
table.TableFormat.Borders.Top.BorderType = BorderStyle.Double;
table.TableFormat.Borders.Top.LineWidth = 1.0F;
table.TableFormat.Borders.Top.Color = Color.YellowGreen;

//設(shè)置表格的左邊框
table.TableFormat.Borders.Left.BorderType = BorderStyle.Double;
table.TableFormat.Borders.Left.LineWidth = 1.0F;
table.TableFormat.Borders.Left.Color = Color.YellowGreen;

//設(shè)置表格的右邊框
table.TableFormat.Borders.Right.BorderType = BorderStyle.Double;
table.TableFormat.Borders.Right.LineWidth = 1.0F;
table.TableFormat.Borders.Right.Color = Color.YellowGreen;

//設(shè)置表格的下邊框
table.TableFormat.Borders.Bottom.BorderType = BorderStyle.Double;
table.TableFormat.Borders.Bottom.LineWidth = 1.0F;
table.TableFormat.Borders.Bottom.Color = Color.YellowGreen;

//設(shè)置表格的水平和垂直邊框 
table.TableFormat.Borders.Horizontal.BorderType = BorderStyle.Hairline;
table.TableFormat.Borders.Horizontal.Color = Color.Orange;
table.TableFormat.Borders.Vertical.BorderType = BorderStyle.Hairline;            
table.TableFormat.Borders.Vertical.Color = Color.Orange;

//保存文檔
document.SaveToFile("TableBorder.docx", FileFormat.Docx2013);

總結(jié)

通過(guò) Spire.Doc for .NET,我們可以靈活地通過(guò)遍歷行來(lái)批量設(shè)置單元格屬性,從而間接控制整列寬度。這種方法邏輯清晰,不僅能解決排版痛點(diǎn),還能確保生成的文檔在不同環(huán)境下保持一致性。希望這段代碼能為你的文檔自動(dòng)化任務(wù)提供有力支持。

到此這篇關(guān)于C#借助Spire.Doc實(shí)現(xiàn)Word表格自動(dòng)排版的文章就介紹到這了,更多相關(guān)C# Word表格排版內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • c#使用Aspose打印文件的示例

    c#使用Aspose打印文件的示例

    這篇文章主要介紹了c#使用Aspose打印文件的示例,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下
    2021-05-05
  • C# WebAPI的幾種返回類(lèi)型方式

    C# WebAPI的幾種返回類(lèi)型方式

    本文主要介紹了C# WebAPI的幾種返回類(lèi)型方式,包括直接返回指定類(lèi)型、返回IActionResult實(shí)例和返回ActionResult,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2025-11-11
  • C#算法設(shè)計(jì)之關(guān)于1000瓶水的問(wèn)題

    C#算法設(shè)計(jì)之關(guān)于1000瓶水的問(wèn)題

    這篇文章主要介紹了C#算法設(shè)計(jì)之關(guān)于1000瓶水的問(wèn)題,是一個(gè)比較經(jīng)典的算法問(wèn)題,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-01-01
  • C#去掉字符串中所有匹配的字符String.Replace方法

    C#去掉字符串中所有匹配的字符String.Replace方法

    在C#中,如果你想要去掉字符串中所有匹配的字符,你可以使用String.Replace方法,本文主要介紹了C#去掉字符串中所有匹配的字符String.Replace方法,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-04-04
  • C#中定時(shí)任務(wù)被阻塞問(wèn)題的解決方法

    C#中定時(shí)任務(wù)被阻塞問(wèn)題的解決方法

    這篇文章主要給大家介紹了關(guān)于C#中定時(shí)任務(wù)被阻塞問(wèn)題的解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用c#具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2021-11-11
  • 詳解CLR的內(nèi)存分配和回收機(jī)制

    詳解CLR的內(nèi)存分配和回收機(jī)制

    本文詳細(xì)講解了CLR的內(nèi)存分配和回收機(jī)制,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-03-03
  • C#如何Task執(zhí)行任務(wù),等待任務(wù)完成

    C#如何Task執(zhí)行任務(wù),等待任務(wù)完成

    這篇文章主要介紹了C#如何Task執(zhí)行任務(wù),等待任務(wù)完成,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • C# 通過(guò) inline-asm 解決嵌入x86匯編

    C# 通過(guò) inline-asm 解決嵌入x86匯編

    此篇文章通過(guò)C#語(yǔ)言解決嵌入x86匯編,主要通過(guò)INline-asm方法來(lái)實(shí)現(xiàn),下面我通過(guò)圖片和代碼的形式給大家分享下,需要的朋友可以參考下
    2015-07-07
  • DevExpress實(shí)現(xiàn)根據(jù)行,列索引來(lái)獲取RepositoryItem的方法

    DevExpress實(shí)現(xiàn)根據(jù)行,列索引來(lái)獲取RepositoryItem的方法

    這篇文章主要介紹了DevExpress實(shí)現(xiàn)根據(jù)行,列索引來(lái)獲取RepositoryItem的方法,需要的朋友可以參考下
    2014-08-08
  • C#中屬性PropertyInfo使用示例小結(jié)

    C#中屬性PropertyInfo使用示例小結(jié)

    在C#中,PropertyInfo是一個(gè)用于獲取和設(shè)置屬性的類(lèi),這篇文章主要介紹了C#中屬性PropertyInfo怎么使用,需要的朋友可以參考下
    2024-06-06

最新評(píng)論

古浪县| 潢川县| 夏邑县| 和政县| 桂阳县| 沾益县| 化德县| 伊春市| 襄垣县| 阿克陶县| 庄浪县| 九寨沟县| 汉阴县| 云南省| 垫江县| 柘荣县| 刚察县| 焦作市| 济南市| 饶阳县| 通渭县| 中超| 澄迈县| 铜鼓县| 怀来县| 梓潼县| 敖汉旗| 茂名市| 盱眙县| 汨罗市| 永宁县| 临洮县| 曲水县| 微博| 青阳县| 贵港市| 伊宁市| 朔州市| 宿州市| 柘荣县| 金湖县|