翻譯|使用教程|編輯:胡濤|2023-02-06 11:22:11.600|閱讀 160 次
概述:本文講的是如何通過Spire.D?oc設置表格的垂直對齊方式,歡迎查閱
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
Spire.Doc for .NET是一款專門對 Word 文檔進行操作的 .NET 類庫。在于幫助開發人員無需安裝 Microsoft Word情況下,輕松快捷高效地創建、編輯、轉換和打印 Microsoft Word 文檔。擁有近10年專業開發經驗Spire系列辦公文檔開發工具,專注于創建、編輯、轉換和打印Word/PDF/Excel等格式文件處理,小巧便捷。
E-iceblue 功能類庫Spire 系列文檔處理組件均由中國本土團隊研發,不依賴第三方軟件,不受其他國家的技術或法律法規限制,同時適配國產操作系統如中科方德、中標麒麟等,兼容國產文檔處理軟件 WPS(如 .wps/.et/.dps 等格式
為表格設置垂直對齊可以在不同的位置顯示內容。共有三個選項,包括頂部、底部、中間。默認為中間。本文講的是如何通過Spire.Doc設置表格的垂直對齊方式,具體步驟如下:
第 1 步:創建一個新的 Word 文檔并添加一個新節。
Document document = new Document(); Section section = document.AddSection();
第 2 步:添加一個 3 列 3 行的表格。您可以在創建表格時將 showBoder 屬性設置為 true。將第一列合并為一個單元格。
Table table = section.AddTable(true); table.ResetCells(3, 3); table.ApplyVerticalMerge(0, 0, 2);
第 3 步:設置每個單元格的垂直對齊方式,默認為頂部。這里我們設置第一行為Top,第二行為Middle,第三行為Bottom。
table.Rows[0].Cells[0].CellFormat.VerticalAlignment = VerticalAlignment.Middle; table.Rows[0].Cells[1].CellFormat.VerticalAlignment = VerticalAlignment.Top; table.Rows[0].Cells[2].CellFormat.VerticalAlignment = VerticalAlignment.Top; table.Rows[1].Cells[1].CellFormat.VerticalAlignment = VerticalAlignment.Middle; table.Rows[1].Cells[2].CellFormat.VerticalAlignment = VerticalAlignment.Middle; table.Rows[2].Cells[1].CellFormat.VerticalAlignment = VerticalAlignment.Bottom; table.Rows[2].Cells[2].CellFormat.VerticalAlignment = VerticalAlignment.Bottom;
第 4 步:將數據追加到表中。
Paragraph paraPic = table.Rows[0].Cells[0].AddParagraph(); DocPicture pic = paraPic.AppendPicture(Image.FromFile("1.png")); String[][] data = { new string[] {"","Spire.Office","Spire.DataExport"}, new string[] {"","Spire.Doc","Spire.DocViewer"}, new string[] {"","Spire.XLS","Spire.PDF"} }; for (int r = 0; r < 3; r++) { TableRow dataRow = table.Rows[r]; dataRow.Height = 50; for (int c = 0; c < 3; c++) { if (c == 1) { Paragraph par = dataRow.Cells[c].AddParagraph(); par.AppendText(data[r][c]); dataRow.Cells[c].Width = (section.PageSetup.ClientWidth) / 2; } if (c == 2) { Paragraph par = dataRow.Cells[c].AddParagraph(); par.AppendText(data[r][c]); dataRow.Cells[c].Width = (section.PageSetup.ClientWidth) / 2; } } }
第 5 步:保存并查看。
document.SaveToFile(@"result.docx", FileFormat.Docx2013); System.Diagnostics.Process.Start(@"result.docx");
結果截圖:
完整代碼:
using Spire.Doc; using Spire.Doc.Documents; using Spire.Doc.Fields; using System; using System.Drawing; namespace SetVerticalAlignment { class Program { static void Main(string[] args) { Document document = new Document(); Section section = document.AddSection(); Table table = section.AddTable(true); table.ResetCells(3, 3); table.ApplyVerticalMerge(0, 0, 2); table.Rows[0].Cells[0].CellFormat.VerticalAlignment = VerticalAlignment.Middle; table.Rows[0].Cells[1].CellFormat.VerticalAlignment = VerticalAlignment.Top; table.Rows[0].Cells[2].CellFormat.VerticalAlignment = VerticalAlignment.Top; table.Rows[1].Cells[1].CellFormat.VerticalAlignment = VerticalAlignment.Middle; table.Rows[1].Cells[2].CellFormat.VerticalAlignment = VerticalAlignment.Middle; table.Rows[2].Cells[1].CellFormat.VerticalAlignment = VerticalAlignment.Bottom; table.Rows[2].Cells[2].CellFormat.VerticalAlignment = VerticalAlignment.Bottom; Paragraph paraPic = table.Rows[0].Cells[0].AddParagraph(); DocPicture pic = paraPic.AppendPicture(Image.FromFile("1.png")); String[][] data = { new string[] {"","Spire.Office","Spire.DataExport"}, new string[] {"","Spire.Doc","Spire.DocViewer"}, new string[] {"","Spire.XLS","Spire.PDF"} }; for (int r = 0; r < 3; r++) { TableRow dataRow = table.Rows[r]; dataRow.Height = 50; for (int c = 0; c < 3; c++) { if (c == 1) { Paragraph par = dataRow.Cells[c].AddParagraph(); par.AppendText(data[r][c]); dataRow.Cells[c].Width = (section.PageSetup.ClientWidth) / 2; } if (c == 2) { Paragraph par = dataRow.Cells[c].AddParagraph(); par.AppendText(data[r][c]); dataRow.Cells[c].Width = (section.PageSetup.ClientWidth) / 2; } } } document.SaveToFile(@"result.docx", FileFormat.Docx2013); System.Diagnostics.Process.Start(@"result.docx"); } }
以上便是如何通過 Spire.Doc 在 Word 中設置表格的垂直對齊方式,如果您有其他問題也可以繼續瀏覽本系列文章,獲取相關教程,你還可以給我留言或者加入我們的官方技術交流群。
歡迎下載|體驗更多E-iceblue產品
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn