翻譯|使用教程|編輯:胡濤|2023-02-06 11:22:11.600|閱讀 175 次
概述:本文講的是如何通過Spire.D?oc設(shè)置表格的垂直對(duì)齊方式,歡迎查閱
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
Spire.Doc for .NET是一款專門對(duì) Word 文檔進(jìn)行操作的 .NET 類庫。在于幫助開發(fā)人員無需安裝 Microsoft Word情況下,輕松快捷高效地創(chuàng)建、編輯、轉(zhuǎn)換和打印 Microsoft Word 文檔。擁有近10年專業(yè)開發(fā)經(jīng)驗(yàn)Spire系列辦公文檔開發(fā)工具,專注于創(chuàng)建、編輯、轉(zhuǎn)換和打印Word/PDF/Excel等格式文件處理,小巧便捷。
E-iceblue 功能類庫Spire 系列文檔處理組件均由中國本土團(tuán)隊(duì)研發(fā),不依賴第三方軟件,不受其他國家的技術(shù)或法律法規(guī)限制,同時(shí)適配國產(chǎn)操作系統(tǒng)如中科方德、中標(biāo)麒麟等,兼容國產(chǎn)文檔處理軟件 WPS(如 .wps/.et/.dps 等格式
為表格設(shè)置垂直對(duì)齊可以在不同的位置顯示內(nèi)容。共有三個(gè)選項(xiàng),包括頂部、底部、中間。默認(rèn)為中間。本文講的是如何通過Spire.Doc設(shè)置表格的垂直對(duì)齊方式,具體步驟如下:
第 1 步:創(chuàng)建一個(gè)新的 Word 文檔并添加一個(gè)新節(jié)。
Document document = new Document(); Section section = document.AddSection();
第 2 步:添加一個(gè) 3 列 3 行的表格。您可以在創(chuàng)建表格時(shí)將 showBoder 屬性設(shè)置為 true。將第一列合并為一個(gè)單元格。
Table table = section.AddTable(true); table.ResetCells(3, 3); table.ApplyVerticalMerge(0, 0, 2);
第 3 步:設(shè)置每個(gè)單元格的垂直對(duì)齊方式,默認(rèn)為頂部。這里我們?cè)O(shè)置第一行為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 步:將數(shù)據(jù)追加到表中。
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");
結(jié)果截圖:
完整代碼:
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 中設(shè)置表格的垂直對(duì)齊方式,如果您有其他問題也可以繼續(xù)瀏覽本系列文章,獲取相關(guān)教程,你還可以給我留言或者加入我們的官方技術(shù)交流群。
歡迎下載|體驗(yàn)更多E-iceblue產(chǎn)品
獲取更多信息請(qǐng)咨詢 ;技術(shù)交流Q群(767755948)
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@ke049m.cn