今天就跟大家聊聊有關(guān)使用OpenXml怎么合并Table單元格,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
在和平等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都網(wǎng)站設(shè)計(jì)、網(wǎng)站制作 網(wǎng)站設(shè)計(jì)制作定制網(wǎng)站制作,公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),品牌網(wǎng)站建設(shè),營銷型網(wǎng)站建設(shè),外貿(mào)網(wǎng)站制作,和平網(wǎng)站建設(shè)費(fèi)用合理。using DocumentFormat.OpenXml; using DocumentFormat.OpenXml.Packaging; using DocumentFormat.OpenXml.Wordprocessing; using OpenXML.Model; using System; using System.Collections.Generic; namespace OpenXML { class Program { //表格數(shù)據(jù) public static List> _tabData; public Program(List
> tabData) { _tabData = tabData; } static void Main(string[] args) { List
dataTitle = new List () { "序號","姓名","性別"}; List data1 = new List () { "1", "張三", "男" }; List data2 = new List () { "2", "王五", "男" }; List data3 = new List () { "3", "李四", "女" }; _tabData = new List >(); _tabData.Add(dataTitle); _tabData.Add(data1); _tabData.Add(data2); _tabData.Add(data3); CreateTable(_tabData, @"C:\Users\dzw159\Desktop\WT\VS\OpenXMLFile\openXMLTest.docx",300); //CreateOpenXMLFile(@"C:\Users\dzw159\Desktop\WT\VS\OpenXMLFile\openXMLTest.docx"); Console.WriteLine("Hello World!"); Console.Read(); } ///
/// 創(chuàng)建Word /// /// public static void CreateOpenXMLFile(string filePath) { using (WordprocessingDocument objWordDocument = WordprocessingDocument.Create(filePath, WordprocessingDocumentType.Document)) { MainDocumentPart objMainDocumentPart = objWordDocument.AddMainDocumentPart(); objMainDocumentPart.Document = new Document(new Body()); Body objBody = objMainDocumentPart.Document.Body; //創(chuàng)建一些需要用到的樣式,如標(biāo)題3,標(biāo)題4,在OpenXml里面,這些樣式都要自己來創(chuàng)建的 //ReportExport.CreateParagraphStyle(objWordDocument); SectionProperties sectionProperties = new SectionProperties(); PageSize pageSize = new PageSize(); PageMargin pageMargin = new PageMargin(); Columns columns = new Columns() { Space = "220" };//720 DocGrid docGrid = new DocGrid() { LinePitch = 100 };//360 //創(chuàng)建頁面的大小,頁距,頁面方向一些基本的設(shè)置,如A4,B4,Letter, //GetPageSetting(PageSize,PageMargin); //在這里填充各個Paragraph,與Table,頁面上第一級元素就是段落,表格. objBody.Append(new Paragraph()); objBody.Append(new Table()); objBody.Append(new Paragraph()); //我會告訴你這里的順序很重要嗎?下面才是把上面那些設(shè)置放到Word里去.(大家可以試試把這下面的代碼放上面,會不會出現(xiàn)打開openxml文件有誤,因?yàn)閮?nèi)容有誤) sectionProperties.Append(pageSize, pageMargin, columns, docGrid); objBody.Append(sectionProperties); //如果有頁眉,在這里添加頁眉. //if (IsAddHead) //{ //添加頁面,如果有圖片,這個圖片和上面添加在objBody方式有點(diǎn)不一樣,這里搞了好久. //ReportExport.AddHeader(objMainDocumentPart, image); //} objMainDocumentPart.Document.Save(); } } ////// 創(chuàng)建Tab /// /// /// /// public static void CreateTable(List> tabData, string filePath,int width) { //打開Word文件 using (WordprocessingDocument d = WordprocessingDocument.Open(filePath,true)) { //聲明表格 Table tab = new Table(); //表格樣式 TableProperties tblProp = new TableProperties(new TableBorders( new TableBorders( new TopBorder() { Val = new EnumValue
(BorderValues.Single),Size = 4}, new BottomBorder() { Val = new EnumValue (BorderValues.Single), Size = 4 }, new LeftBorder() { Val = new EnumValue (BorderValues.Single), Size = 4 }, new RightBorder() { Val = new EnumValue (BorderValues.Single), Size = 4 }, new InsideHorizontalBorder() { Val = new EnumValue (BorderValues.Single), Size = 4 }, new InsideVerticalBorder() { Val = new EnumValue (BorderValues.Single), Size = 4 } ) )); //設(shè)置表格寬度 tblProp.TableWidth = new TableWidth() { Width = width.ToString(), Type = TableWidthUnitValues.Dxa }; //樣式添加 tab.Append(tblProp); int j = 0; //循環(huán)生成單元格 foreach (var item in tabData) { //聲明Tab行 TableRow row = new TableRow(); for (var i = 0; i < item.Count; i++) { //申明單元格 TableCell cell = new TableCell(); TableCellProperties tableCellProperties = new TableCellProperties(); //單元格樣式設(shè)置 TableCellMargin margin = new TableCellMargin(); margin.LeftMargin = new LeftMargin() { Width="100", Type = TableWidthUnitValues.Dxa }; margin.RightMargin = new RightMargin() { Width = "100", Type = TableWidthUnitValues.Dxa }; tableCellProperties.Append(margin); Paragraph par = new Paragraph(); Run run = new Run(); //如果同一列的參數(shù)相同(合并單元格) if (j < (tabData.Count - 1) && item[i] == tabData[j + 1][i]) { VerticalMerge verticalMerge = new VerticalMerge() { Val = MergedCellValues.Restart }; //RunProperties rpr = new RunProperties(); //rpr.Append(new Bold()); //run.Append(rpr); //verticalMerge.Val = MergedCellValues.Restart; //Text t = new Text(item[i]); //t.Space = new EnumValue (SpaceProcessingModeValues.Preserve); //run.Append(t); TableCellProperties tableCellProperties2 = new TableCellProperties(); tableCellProperties2.Append(verticalMerge); cell.Append(tableCellProperties2); Text t = new Text(item[i]); t.Space = new EnumValue (SpaceProcessingModeValues.Preserve); run.Append(t); } //和上一行比較(合并單元格) else if (j>0 && j < (tabData.Count) && item[i] == tabData[j -1][i]) { VerticalMerge verticalMerge = new VerticalMerge() { Val = MergedCellValues.Continue }; TableCellProperties tableCellProperties2 = new TableCellProperties(); tableCellProperties2.Append(verticalMerge); cell.Append(tableCellProperties2); Text t = new Text(item[i]); t.Space = new EnumValue (SpaceProcessingModeValues.Preserve); run.Append(t); } else { //RunProperties rPr = new RunProperties(); //rPr.Append(new Bold()); //run.Append(rPr); //單元格內(nèi)容添加(由內(nèi)向外順序) Text t = new Text(item[i]); t.Space = new EnumValue (SpaceProcessingModeValues.Preserve); run.Append(t); } par.Append(run); cell.Append(tableCellProperties); cell.Append(par); row.Append(cell); } j++; //表格添加行 tab.Append(row); } //objBody.Append(new Paragraph()); //objBody.Append(new Table()); d.MainDocumentPart.Document.Body.Append(new Paragraph(new Run(tab))); d.MainDocumentPart.Document.Save(); } } } }
看完上述內(nèi)容,你們對使用OpenXml怎么合并Table單元格有進(jìn)一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注創(chuàng)新互聯(lián)成都網(wǎng)站設(shè)計(jì)公司行業(yè)資訊頻道,感謝大家的支持。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。