OpenXml合并Table單元格代碼實(shí)例
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<List<string>> _tabData; public Program(List<List<string>> tabData) { _tabData = tabData; } static void Main(string[] args) { List<string> dataTitle = new List<string>() { "序號(hào)","姓名","性別"}; List<string> data1 = new List<string>() { "1", "張三", "男" }; List<string> data2 = new List<string>() { "2", "王五", "男" }; List<string> data3 = new List<string>() { "3", "李四", "女" }; _tabData = new List<List<string>>(); _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(); } /// <summary> /// 創(chuàng)建Word /// </summary> /// <param name="filePath"></param> 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里面,這些樣式都要自己來(lái)創(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)建頁(yè)面的大小,頁(yè)距,頁(yè)面方向一些基本的設(shè)置,如A4,B4,Letter, //GetPageSetting(PageSize,PageMargin); //在這里填充各個(gè)Paragraph,與Table,頁(yè)面上第一級(jí)元素就是段落,表格. objBody.Append(new Paragraph()); objBody.Append(new Table()); objBody.Append(new Paragraph()); //我會(huì)告訴你這里的順序很重要嗎?下面才是把上面那些設(shè)置放到Word里去.(大家可以試試把這下面的代碼放上面,會(huì)不會(huì)出現(xiàn)打開(kāi)openxml文件有誤,因?yàn)閮?nèi)容有誤) sectionProperties.Append(pageSize, pageMargin, columns, docGrid); objBody.Append(sectionProperties); //如果有頁(yè)眉,在這里添加頁(yè)眉. //if (IsAddHead) //{ //添加頁(yè)面,如果有圖片,這個(gè)圖片和上面添加在objBody方式有點(diǎn)不一樣,這里搞了好久. //ReportExport.AddHeader(objMainDocumentPart, image); //} objMainDocumentPart.Document.Save(); } } /// <summary> /// 創(chuàng)建Tab /// </summary> /// <param name="tabData"></param> /// <param name="filePath"></param> /// <param name="width"></param> public static void CreateTable(List<List<string>> tabData, string filePath,int width) { //打開(kāi)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>(BorderValues.Single),Size = 4}, new BottomBorder() { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 4 }, new LeftBorder() { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 4 }, new RightBorder() { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 4 }, new InsideHorizontalBorder() { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 4 }, new InsideVerticalBorder() { Val = new EnumValue<BorderValues>(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>(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>(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>(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>(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(); } } } }
注:他們有的說(shuō),標(biāo)記為MergedCellValues.Continue的縱向單元格一定要給值!(這個(gè)我試著給賦值null或者為“”,都能正常合并)
以上就是全部知識(shí)點(diǎn)內(nèi)容,感謝大家的閱讀和對(duì)我們的支持。
上一篇:c#中將uint值轉(zhuǎn)換成int的實(shí)例方法
欄 目:C#教程
下一篇:C#項(xiàng)目中跨文件調(diào)用公共類的實(shí)例方法
本文標(biāo)題:OpenXml合并Table單元格代碼實(shí)例
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/4690.html
您可能感興趣的文章
- 01-10C#實(shí)現(xiàn)簡(jiǎn)單合并word文檔的方法
- 01-10C#編程實(shí)現(xiàn)DataTable添加行的方法
- 01-10C#實(shí)現(xiàn)DataTable映射成Model的方法(附源碼)
- 01-10C# DataTable使用方法詳解
- 01-10詳解C#中HashTable的用法
- 01-10C#實(shí)現(xiàn)DataTable轉(zhuǎn)換成IList的方法
- 01-10C#實(shí)現(xiàn)從多列的DataTable里取需要的幾列
- 01-10C#常見(jiàn)的幾種集合 ArrayList,Hashtable,List&lt;T&gt;,
- 01-10C#實(shí)現(xiàn)Excel動(dòng)態(tài)生成PivotTable
- 01-10C#在DataTable中根據(jù)條件刪除某一行的實(shí)現(xiàn)方法


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10C#通過(guò)反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁(yè)無(wú)法打開(kāi)的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻 器左下角滾動(dòng)新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過(guò)重寫(xiě)Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-10delphi制作wav文件的方法
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 01-10C#中split用法實(shí)例總結(jié)
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 01-11Mac OSX 打開(kāi)原生自帶讀寫(xiě)NTFS功能(圖文
- 04-02jquery與jsp,用jquery
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什