1.方法一 :通过自定义的CTableGrid存储表格数据进行创建表格

       // 创建表格public bool AddTable(uint slideIndex, CTableGrid tableData, double dPosX, double dPosY, string strTitleRowColor, string strOddRowColor, string strEvenRowColor){SlidePart slidePart = GetSlidePartbyIndex(slideIndex);PresentationPart presentationPart1 = m_presentationDoc.PresentationPart;
​string strTableStyleId = AutoMakeTableStyleId();SetTableStyle(strTableStyleId, strTitleRowColor, strOddRowColor, strEvenRowColor);
​// Get the shape tree that contains the shape to change.ShapeTree shapeTree = slidePart.Slide.CommonSlideData.ShapeTree;
​GraphicFrame graphicFrame = new GraphicFrame();
​NonVisualGraphicFrameProperties nonVisualGraphicFrameProperties = new NonVisualGraphicFrameProperties();GetNonVisualGraphicFrameProperties(ref nonVisualGraphicFrameProperties);
​//插入位置Transform transform = new Transform();GetTransform(ref transform, dPosX, dPosY);
​A.Graphic graphic = new A.Graphic();
​A.GraphicData graphicData = new A.GraphicData() { Uri = "http://schemas.openxmlformats.org/drawingml/2006/table" };
​A.Table table = new A.Table();
​A.TableProperties tableProperties = new A.TableProperties() { FirstRow = true, BandRow = true };A.TableStyleId tableStyleId = new A.TableStyleId();tableStyleId.Text = strTableStyleId;
​tableProperties.Append(tableStyleId);
​//生成列A.TableGrid tableGrid = new A.TableGrid();GetTableGrid(ref tableGrid, tableData);
​table.Append(tableProperties);table.Append(tableGrid);
​//生成行for (uint iRow = 0; iRow < tableData.m_nRows; ++iRow){A.TableRow tableRow = new A.TableRow() { Height = Convert.ToInt64(tableData.GetRowHeight(iRow) * m_dUnitConvertScale) }; //行高GetTableRow(ref tableRow, tableData, iRow);table.Append(tableRow);}
​graphicData.Append(table);
​graphic.Append(graphicData);
​graphicFrame.Append(nonVisualGraphicFrameProperties);graphicFrame.Append(transform);graphicFrame.Append(graphic);
​shapeTree.Append(graphicFrame);
​// Save the modified slide.slidePart.Slide.Save();
​return false;}
  1. 方法二:通过List<List<string>> tableData的形式传入表格的数据

    ​public bool AddTable(uint slideIndex, List<List<string>> tableData, double dPosX, double dPosY, double dRowHeight, double dColumnWidth, string strTitleRowColor, string strOddRowColor, string strEvenRowColor){int nRows = tableData.Count;if (nRows == 0){return false;}int nCols = tableData[0].Count;
    ​CTableGrid tData = new CTableGrid(Convert.ToUInt32(nRows), Convert.ToUInt32(nCols), dRowHeight, dColumnWidth);for (int i = 0; i < tableData.Count; i++){List<string> row = tableData[i];
    ​CTableGridRow row1 = new CTableGridRow();
    ​for (int j = 0; j < row.Count; j++){CPPTtext txt = new CPPTtext(row[j]);
    ​CPPTtableCell cell = new CPPTtableCell(txt);
    ​row1.Set(Convert.ToUInt32(j), cell);}
    ​tData.Add(Convert.ToUInt32(i), row1);}
    ​AddTable(slideIndex, tData, dPosX, dPosY, strTitleRowColor, strOddRowColor, strEvenRowColor);return true;}
    ​

为更加细致地介绍创建表格的过程,我创建立了一个创建表格的工程项目,并有相应的测试案例,下载地址:

链接:https://pan.baidu.com/s/1TRHaCGW5ACx__yk_mZxtaQ 提取码:1g7w

也可以看一下的完整项目代码:

using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Presentation;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using A = DocumentFormat.OpenXml.Drawing;
​
namespace OpenXMLCreateTable
{internal class CPPTtable{private PresentationDocument m_presentationDoc;private const double m_dUnitConvertScale = 360000.0; //长度换算成cmprivate A.RunProperties m_currentFontProperties;private Stack<A.RunProperties> m_FontPropertiesStack;private const uint m_iFontSizeScale = 100; //字体放大比例
​public CPPTtable(){m_FontPropertiesStack = new Stack<A.RunProperties>();m_currentFontProperties = new A.RunProperties() { Language = "zh-CN", AlternativeLanguage = "en-US", Dirty = false };SetDefaultFontStyle();}
​public void SetDefaultFontStyle(){m_currentFontProperties = new A.RunProperties() { Language = "zh-CN", AlternativeLanguage = "en-US", Dirty = false };m_currentFontProperties.SetAttribute(new OpenXmlAttribute("", "smtClean", "", "0"));SetFontStyle();}
​public void SetFontStyle(string strFontName = "等线", uint nSize = 18, bool bBold = false, bool bItalic = false, bool bUnderline = false, bool bShadow = false, string strColor = "000000"){SetFontName(strFontName);SetFontSize(nSize);
​SetFontBold(bBold);SetFontItalic(bItalic);SetFontUnderline(bUnderline);
​SetFontShadow(bShadow);SetFontColor(strColor);}
​// 打开PPTpublic bool Open(string strFileName, bool isEditable){m_presentationDoc = PresentationDocument.Open(strFileName, isEditable);if (m_presentationDoc == null){_throw_assert("PresentationDocument is null !");return false;}
​return m_presentationDoc != null;}
​// 创建表格public bool AddTable(uint slideIndex, CTableGrid tableData, double dPosX, double dPosY, string strTitleRowColor, string strOddRowColor, string strEvenRowColor){SlidePart slidePart = GetSlidePartbyIndex(slideIndex);PresentationPart presentationPart1 = m_presentationDoc.PresentationPart;
​string strTableStyleId = AutoMakeTableStyleId();SetTableStyle(strTableStyleId, strTitleRowColor, strOddRowColor, strEvenRowColor);
​// Get the shape tree that contains the shape to change.ShapeTree shapeTree = slidePart.Slide.CommonSlideData.ShapeTree;
​GraphicFrame graphicFrame = new GraphicFrame();
​NonVisualGraphicFrameProperties nonVisualGraphicFrameProperties = new NonVisualGraphicFrameProperties();GetNonVisualGraphicFrameProperties(ref nonVisualGraphicFrameProperties);
​//插入位置Transform transform = new Transform();GetTransform(ref transform, dPosX, dPosY);
​A.Graphic graphic = new A.Graphic();
​A.GraphicData graphicData = new A.GraphicData() { Uri = "http://schemas.openxmlformats.org/drawingml/2006/table" };
​A.Table table = new A.Table();
​A.TableProperties tableProperties = new A.TableProperties() { FirstRow = true, BandRow = true };A.TableStyleId tableStyleId = new A.TableStyleId();tableStyleId.Text = strTableStyleId;
​tableProperties.Append(tableStyleId);
​//生成列A.TableGrid tableGrid = new A.TableGrid();GetTableGrid(ref tableGrid, tableData);
​table.Append(tableProperties);table.Append(tableGrid);
​//生成行for (uint iRow = 0; iRow < tableData.m_nRows; ++iRow){A.TableRow tableRow = new A.TableRow() { Height = Convert.ToInt64(tableData.GetRowHeight(iRow) * m_dUnitConvertScale) }; //行高GetTableRow(ref tableRow, tableData, iRow);table.Append(tableRow);}
​graphicData.Append(table);
​graphic.Append(graphicData);
​graphicFrame.Append(nonVisualGraphicFrameProperties);graphicFrame.Append(transform);graphicFrame.Append(graphic);
​shapeTree.Append(graphicFrame);
​// Save the modified slide.slidePart.Slide.Save();
​return false;}
​public bool AddTable(uint slideIndex, List<List<string>> tableData, double dPosX, double dPosY, double dRowHeight, double dColumnWidth, string strTitleRowColor, string strOddRowColor, string strEvenRowColor){int nRows = tableData.Count;if (nRows == 0){return false;}int nCols = tableData[0].Count;
​CTableGrid tData = new CTableGrid(Convert.ToUInt32(nRows), Convert.ToUInt32(nCols), dRowHeight, dColumnWidth);for (int i = 0; i < tableData.Count; i++){List<string> row = tableData[i];
​CTableGridRow row1 = new CTableGridRow();
​for (int j = 0; j < row.Count; j++){CPPTtext txt = new CPPTtext(row[j]);
​CPPTtableCell cell = new CPPTtableCell(txt);
​row1.Set(Convert.ToUInt32(j), cell);}
​tData.Add(Convert.ToUInt32(i), row1);}
​AddTable(slideIndex, tData, dPosX, dPosY, strTitleRowColor, strOddRowColor, strEvenRowColor);return true;}
​private void GetTableRow(ref A.TableRow tableRow, CTableGrid grid, uint iRow){for (uint iCol = 0; iCol < grid.m_nCols; ++iCol){A.TableCell tableCell = new A.TableCell();
​//生成文本A.TextBody textBody = new A.TextBody();CPPTtableCell tableCell2 = new CPPTtableCell();tableCell2 = grid.GetCell(iRow, iCol);GetTextBody(ref textBody, tableCell2);A.TableCellProperties tableCellProperties1 = new A.TableCellProperties();
​tableCell.Append(textBody);tableCell.Append(tableCellProperties1);
​tableRow.Append(tableCell);}
​A.ExtensionList extensionList4 = new A.ExtensionList();
​A.Extension extension4 = new A.Extension() { Uri = "{0D108BD9-81ED-4DB2-BD59-A6C34878D82A}" };
​OpenXmlUnknownElement openXmlUnknownElement4 = OpenXmlUnknownElement.CreateOpenXmlUnknownElement("<a16:rowId xmlns:a16=\"http://schemas.microsoft.com/office/drawing/2014/main\" val=\"1915637097\" />");
​extension4.Append(openXmlUnknownElement4);
​extensionList4.Append(extension4);
​tableRow.Append(extensionList4);}
​private void GetTextBody(ref A.TextBody textBody, CPPTtableCell cell){pushAttribute();
​A.BodyProperties bodyProperties = new A.BodyProperties();A.ListStyle listStyle = new A.ListStyle();
​A.Paragraph paragraph = new A.Paragraph();CPPTtext cText = new CPPTtext();if (cell != null)cText = cell.m_text;
​AddText(ref paragraph, cText);
​textBody.Append(bodyProperties);textBody.Append(listStyle);textBody.Append(paragraph);
​popAttribute();}
​private void pushAttribute(){m_FontPropertiesStack.Push((A.RunProperties)m_currentFontProperties.Clone());}
​private void popAttribute(){if (m_FontPropertiesStack.Count != 0){m_currentFontProperties = m_FontPropertiesStack.Peek();m_FontPropertiesStack.Pop();}}
​private void AddText(ref A.Paragraph paragraph, CPPTtext cText){if (cText == null)return;
​pushAttribute();
​SetFontName(cText.m_font.m_strFontName);SetFontSize(cText.m_font.m_fontSize);
​SetFontBold(cText.m_font.m_bBold);SetFontItalic(cText.m_font.m_bItalic);SetFontUnderline(cText.m_font.m_bUnderline);
​SetFontColor(cText.m_font.m_strColor);SetFontShadow(cText.m_font.m_bShadow);
​AppendText(cText.m_strText, ref paragraph);
​popAttribute();}
​private void AppendText(string strText, ref A.Paragraph paragraph){A.Run run = SetText(strText);paragraph.Append((A.Run)run.Clone());}
​private A.Run SetText(string strText){A.Run run = new A.Run();
​run.Append((A.RunProperties)m_currentFontProperties.CloneNode(true));
​A.Text text = new A.Text();text.Text = strText;run.Append(text);
​return run;}
​public void SetFontBold(bool bBond = false){m_currentFontProperties.Bold = bBond;}
​public void SetFontItalic(bool bItalic = false){m_currentFontProperties.Italic = bItalic;}
​public void SetFontUnderline(bool bUnderline = false){if (bUnderline)m_currentFontProperties.Underline = A.TextUnderlineValues.Single;elsem_currentFontProperties.Underline = A.TextUnderlineValues.None;}
​public void SetFontColor(string strColor = "000000"){A.SolidFill solidFill = (A.SolidFill)m_currentFontProperties.GetFirstChild<A.SolidFill>();if (solidFill == null){solidFill = new A.SolidFill();
​m_currentFontProperties.Append(solidFill);}
​A.RgbColorModelHex rgbColorModelHex = (A.RgbColorModelHex)solidFill.GetFirstChild<A.RgbColorModelHex>();if (rgbColorModelHex == null){rgbColorModelHex = new A.RgbColorModelHex() { Val = "000000" };solidFill.Append(rgbColorModelHex);}
​rgbColorModelHex.Val.Value = strColor;}
​//文字阴影public void SetFontShadow(bool bShadow = false){if (!bShadow)return;
​A.EffectList effectList = (A.EffectList)m_currentFontProperties.GetFirstChild<A.EffectList>();if (effectList == null){effectList = new A.EffectList();m_currentFontProperties.Append(effectList);}
​A.OuterShadow outerShadow = (A.OuterShadow)effectList.GetFirstChild<A.OuterShadow>();if (outerShadow == null){outerShadow = new A.OuterShadow() { BlurRadius = 38100L, Distance = 38100L, Direction = 2700000, Alignment = A.RectangleAlignmentValues.TopLeft };effectList.Append(outerShadow);}
​A.RgbColorModelHex rgbColorModelHex = (A.RgbColorModelHex)outerShadow.GetFirstChild<A.RgbColorModelHex>();if (rgbColorModelHex == null){rgbColorModelHex = new A.RgbColorModelHex() { Val = "000000" };outerShadow.Append(rgbColorModelHex);}
​A.Alpha alpha = (A.Alpha)rgbColorModelHex.GetFirstChild<A.Alpha>();if (alpha == null){alpha = new A.Alpha() { Val = 43137 };rgbColorModelHex.Append(alpha);}}
​public void SetFontName(string strFontName = "等线"){//中文字体A.EastAsianFont eastAsianFont = (A.EastAsianFont)m_currentFontProperties.GetFirstChild<A.EastAsianFont>();if (eastAsianFont == null){eastAsianFont = new A.EastAsianFont() { Typeface = "等线" };m_currentFontProperties.Append(eastAsianFont);}eastAsianFont.Typeface.Value = strFontName;
​//英文字体A.LatinFont latinFont = (A.LatinFont)m_currentFontProperties.GetFirstChild<A.LatinFont>();if (latinFont == null){latinFont = new A.LatinFont() { Typeface = "等线" };m_currentFontProperties.Append(latinFont);}
​latinFont.Typeface.Value = strFontName;}
​public void SetFontSize(uint nSize = 18){nSize *= m_iFontSizeScale;m_currentFontProperties.FontSize = Int32Value.FromInt32((int)nSize);}
​private void GetTableGrid(ref A.TableGrid tableGrid, CTableGrid grid){for (uint idx = 0; idx < grid.m_nCols; ++idx){A.GridColumn gridColumn = new A.GridColumn() { Width = Convert.ToInt64(grid.GetColWidth(idx) * m_dUnitConvertScale) }; //列宽
​A.ExtensionList extensionList = new A.ExtensionList();
​A.Extension extension = new A.Extension() { Uri = "{9D8B030D-6E8A-4147-A177-3AD203B41FA5}" };
​OpenXmlUnknownElement openXmlUnknownElement = OpenXmlUnknownElement.CreateOpenXmlUnknownElement("<a16:colId xmlns:a16=\"http://schemas.microsoft.com/office/drawing/2014/main\" val=\"3310506284\" />");
​extension.Append(openXmlUnknownElement);
​extensionList.Append(extension);
​gridColumn.Append(extensionList);tableGrid.Append(gridColumn);}}
​private void GetTransform(ref Transform transform, double xPos, double yPos){A.Offset offset = new A.Offset() { X = Convert.ToInt64(xPos * m_dUnitConvertScale), Y = Convert.ToInt64(yPos * m_dUnitConvertScale) };//A.Extents extents = new A.Extents() { Cx = 10 * m_lengthUnitConvertScale, Cy = 10 * m_lengthUnitConvertScale };
​transform.Append(offset);//transform.Append(extents);}
​private void GetNonVisualGraphicFrameProperties(ref NonVisualGraphicFrameProperties nonVisualGraphicFrameProperties){IEnumerable<NonVisualDrawingProperties> childElements = nonVisualGraphicFrameProperties.Elements<NonVisualDrawingProperties>();int nCount = childElements.Count() + 1;
​string strTableName = "表格 " + nCount;
​NonVisualDrawingProperties nonVisualDrawingProperties = new NonVisualDrawingProperties() { Id = Convert.ToUInt32(nCount), Name = strTableName }; //(UInt32Value)4U
​NonVisualGraphicFrameDrawingProperties nonVisualGraphicFrameDrawingProperties1 = new NonVisualGraphicFrameDrawingProperties();A.GraphicFrameLocks graphicFrameLocks1 = new A.GraphicFrameLocks() { NoGrouping = true };
​nonVisualGraphicFrameDrawingProperties1.Append(graphicFrameLocks1);ApplicationNonVisualDrawingProperties applicationNonVisualDrawingProperties4 = new ApplicationNonVisualDrawingProperties();
​nonVisualGraphicFrameProperties.Append(nonVisualDrawingProperties);nonVisualGraphicFrameProperties.Append(nonVisualGraphicFrameDrawingProperties1);nonVisualGraphicFrameProperties.Append(applicationNonVisualDrawingProperties4);}
​private string AutoMakeTableStyleId(){string strStyleId = "";PresentationPart presentationPart1 = m_presentationDoc.PresentationPart;TableStylesPart tableStylesPart1 = presentationPart1.TableStylesPart;
​if (tableStylesPart1 == null){int nCount = GetPartCountInPresentationPart() + 1;string strId = "rId" + nCount;
​tableStylesPart1 = presentationPart1.AddNewPart<TableStylesPart>(strId);  // "rId6"A.TableStyleList tableStyleList1 = new A.TableStyleList() { Default = "{5C22544A-7EE6-4342-B048-85BDC9FD1C3A}" };tableStyleList1.AddNamespaceDeclaration("a", "http://schemas.openxmlformats.org/drawingml/2006/main");tableStylesPart1.TableStyleList = tableStyleList1;}
​int nCountStyle = tableStylesPart1.TableStyleList.Count() + 1;string strSub = "";if (nCountStyle < 10){strSub = "0" + nCountStyle;}
​strStyleId = "{5C22544A-7EE6-4342-B048-85BDC9FD1C" + strSub + "}";
​return strStyleId;}
​// 添加表格样式private bool SetTableStyle(string strDefaultId, string strTitleRowColor, string strOddRowColor, string strEvenRowColor, uint uiLineType = (uint)A.CompoundLineValues.Single){PresentationPart presentationPart1 = m_presentationDoc.PresentationPart;TableStylesPart tableStylesPart1 = presentationPart1.TableStylesPart;
​if (tableStylesPart1 == null){int nCount = GetPartCountInPresentationPart() + 1;string strId = "rId" + nCount;
​tableStylesPart1 = presentationPart1.AddNewPart<TableStylesPart>(strId);  // "rId6"A.TableStyleList tableStyleList1 = new A.TableStyleList() { Default = "{5C22544A-7EE6-4342-B048-85BDC9FD1C3A}" };tableStyleList1.AddNamespaceDeclaration("a", "http://schemas.openxmlformats.org/drawingml/2006/main");tableStylesPart1.TableStyleList = tableStyleList1;}
​int nCountStyle = tableStylesPart1.TableStyleList.Count() + 1;string strStyleName = "自定义" + nCountStyle;A.TableStyleEntry tableStyleEntry1 = new A.TableStyleEntry() { StyleId = strDefaultId, StyleName = strStyleName }; // StyleName = "中度样式 2 - 强调 1"
​//  wholeTable1A.WholeTable wholeTable1 = new A.WholeTable();
​A.TableCellTextStyle tableCellTextStyle1 = new A.TableCellTextStyle();
​A.FontReference fontReference1 = new A.FontReference() { Index = A.FontCollectionIndexValues.Minor };A.PresetColor presetColor1 = new A.PresetColor() { Val = A.PresetColorValues.Black };
​fontReference1.Append(presetColor1);A.SchemeColor schemeColor57 = new A.SchemeColor() { Val = A.SchemeColorValues.Dark1 }; //除标题行外字体的颜色
​tableCellTextStyle1.Append(fontReference1);tableCellTextStyle1.Append(schemeColor57);
​// 边框A.TableCellBorders tableCellBorders1 = new A.TableCellBorders();{// 左边框A.LeftBorder leftBorder1 = new A.LeftBorder();A.Outline outline4 = new A.Outline();SetOutline(ref outline4, uiLineType);leftBorder1.Append(outline4);
​// 右边框A.RightBorder rightBorder1 = new A.RightBorder();A.Outline outline5 = new A.Outline();SetOutline(ref outline5, uiLineType);rightBorder1.Append(outline5);
​// 上边框A.TopBorder topBorder1 = new A.TopBorder();A.Outline outline6 = new A.Outline();SetOutline(ref outline6, uiLineType);topBorder1.Append(outline6);
​// 下边框A.BottomBorder bottomBorder1 = new A.BottomBorder();A.Outline outline7 = new A.Outline();SetOutline(ref outline7, uiLineType);bottomBorder1.Append(outline7);
​// 内水平框A.InsideHorizontalBorder insideHorizontalBorder1 = new A.InsideHorizontalBorder();A.Outline outline8 = new A.Outline();SetOutline(ref outline8, uiLineType);insideHorizontalBorder1.Append(outline8);
​// 内竖向边框A.InsideVerticalBorder insideVerticalBorder1 = new A.InsideVerticalBorder();A.Outline outline9 = new A.Outline();SetOutline(ref outline9, uiLineType);insideVerticalBorder1.Append(outline9);
​tableCellBorders1.Append(leftBorder1);tableCellBorders1.Append(rightBorder1);tableCellBorders1.Append(topBorder1);tableCellBorders1.Append(bottomBorder1);tableCellBorders1.Append(insideHorizontalBorder1);tableCellBorders1.Append(insideVerticalBorder1);}
​A.TableCellStyle tableCellStyle1 = new A.TableCellStyle();
​A.FillProperties fillProperties1 = new A.FillProperties();
​A.SolidFill solidFill53 = new A.SolidFill();
​A.SchemeColor schemeColor64 = new A.SchemeColor() { Val = A.SchemeColorValues.Accent1 };A.Tint tint20 = new A.Tint() { Val = 20000 };
​schemeColor64.Append(tint20);
​solidFill53.Append(schemeColor64);
​fillProperties1.Append(solidFill53);
​tableCellStyle1.Append(tableCellBorders1);tableCellStyle1.Append(fillProperties1);
​wholeTable1.Append(tableCellTextStyle1);wholeTable1.Append(tableCellStyle1);
​// band1Horizontal1  奇数行A.Band1Horizontal band1Horizontal1 = new A.Band1Horizontal();A.TableCellStyle tableCellStyle2 = new A.TableCellStyle();SetTableCellStyle(ref tableCellStyle2, strOddRowColor);band1Horizontal1.Append(tableCellStyle2);
​// band2Horizontal1 偶数行A.Band2Horizontal band2Horizontal1 = new A.Band2Horizontal();A.TableCellStyle tableCellStyle3 = new A.TableCellStyle();SetTableCellStyle(ref tableCellStyle3, strEvenRowColor);band2Horizontal1.Append(tableCellStyle3);
​// band1Vertical1A.Band1Vertical band1Vertical1 = new A.Band1Vertical();A.TableCellStyle tableCellStyle4 = new A.TableCellStyle();A.TableCellBorders tableCellBorders4 = new A.TableCellBorders();tableCellStyle4.Append(tableCellBorders4);band1Vertical1.Append(tableCellStyle4);
​// band2Vertical1A.Band2Vertical band2Vertical1 = new A.Band2Vertical();A.TableCellStyle tableCellStyle5 = new A.TableCellStyle();A.TableCellBorders tableCellBorders5 = new A.TableCellBorders();tableCellStyle5.Append(tableCellBorders5);band2Vertical1.Append(tableCellStyle5);
​// lastColumn1A.LastColumn lastColumn1 = new A.LastColumn();A.TableCellTextStyle tableCellTextStyle2 = new A.TableCellTextStyle();A.TableCellStyle tableCellStyle6 = new A.TableCellStyle();SetTableCellTextStyleAndCellStyle(ref tableCellTextStyle2, ref tableCellStyle6);lastColumn1.Append(tableCellTextStyle2);lastColumn1.Append(tableCellStyle6);
​// firstColumn1A.FirstColumn firstColumn1 = new A.FirstColumn();A.TableCellTextStyle tableCellTextStyle3 = new A.TableCellTextStyle();A.TableCellStyle tableCellStyle7 = new A.TableCellStyle();SetTableCellTextStyleAndCellStyle(ref tableCellTextStyle3, ref tableCellStyle7);firstColumn1.Append(tableCellTextStyle3);firstColumn1.Append(tableCellStyle7);
​// lastRow1A.LastRow lastRow1 = new A.LastRow();A.TableCellTextStyle tableCellTextStyle4 = new A.TableCellTextStyle() { Bold = A.BooleanStyleValues.On };A.TableCellStyle tableCellStyle8 = new A.TableCellStyle();SetTableCellTextStyleAndCellStyle(ref tableCellTextStyle4, ref tableCellStyle8);lastRow1.Append(tableCellTextStyle4);lastRow1.Append(tableCellStyle8);
​// firstRow1A.FirstRow firstRow1 = new A.FirstRow();
​A.TableCellTextStyle tableCellTextStyle5 = new A.TableCellTextStyle() { Bold = A.BooleanStyleValues.On };
​A.FontReference fontReference5 = new A.FontReference() { Index = A.FontCollectionIndexValues.Minor };A.PresetColor presetColor5 = new A.PresetColor() { Val = A.PresetColorValues.Black };  //预设颜色
​fontReference5.Append(presetColor5);A.SchemeColor schemeColor74 = new A.SchemeColor() { Val = A.SchemeColorValues.Light1 }; //字体颜色
​tableCellTextStyle5.Append(fontReference5);tableCellTextStyle5.Append(schemeColor74);
​A.TableCellStyle tableCellStyle9 = new A.TableCellStyle();SetTableCellStyle(ref tableCellStyle9, strTitleRowColor);
​firstRow1.Append(tableCellTextStyle5);firstRow1.Append(tableCellStyle9);
​tableStyleEntry1.Append(wholeTable1);tableStyleEntry1.Append(band1Horizontal1);tableStyleEntry1.Append(band2Horizontal1);tableStyleEntry1.Append(band1Vertical1);tableStyleEntry1.Append(band2Vertical1);tableStyleEntry1.Append(lastColumn1);tableStyleEntry1.Append(firstColumn1);tableStyleEntry1.Append(lastRow1);tableStyleEntry1.Append(firstRow1);
​tableStylesPart1.TableStyleList.Append(tableStyleEntry1);
​return true;}
​private void SetOutline(ref A.Outline outline, uint uiLineType){outline.Width = 12700;outline.CompoundLineType = (A.CompoundLineValues)uiLineType;
​A.SolidFill solidFill47 = new A.SolidFill();A.SchemeColor schemeColor58 = new A.SchemeColor() { Val = A.SchemeColorValues.Light1 }; //边框颜色
​solidFill47.Append(schemeColor58);
​outline.Append(solidFill47);}
​private void SetTableCellStyle(ref A.TableCellStyle tableCellStyle, string strColor){A.TableCellBorders tableCellBorders = new A.TableCellBorders();
​A.FillProperties fillProperties = new A.FillProperties();
​A.SolidFill solidFill = new A.SolidFill();
​SetColorToSolidFill(ref solidFill, strColor);   // 填充颜色
​fillProperties.Append(solidFill);
​tableCellStyle.Append(tableCellBorders);tableCellStyle.Append(fillProperties);}
​private void SetColorToSolidFill(ref A.SolidFill solidFill, string strColor){A.RgbColorModelHex rgbColorModelHex = (A.RgbColorModelHex)solidFill.GetFirstChild<A.RgbColorModelHex>();if (rgbColorModelHex == null){rgbColorModelHex = new A.RgbColorModelHex() { Val = strColor };solidFill.Append(rgbColorModelHex);}else{rgbColorModelHex.Val.Value = strColor;}}
​private void SetTableCellTextStyleAndCellStyle(ref A.TableCellTextStyle tableCellTextStyle, ref A.TableCellStyle tableCellStyle){tableCellTextStyle.Bold = A.BooleanStyleValues.On;A.FontReference fontReference = new A.FontReference() { Index = A.FontCollectionIndexValues.Minor };A.PresetColor presetColor = new A.PresetColor() { Val = A.PresetColorValues.Black };
​fontReference.Append(presetColor);A.SchemeColor schemeColor1 = new A.SchemeColor() { Val = A.SchemeColorValues.Light1 };
​tableCellTextStyle.Append(fontReference);tableCellTextStyle.Append(schemeColor1);
​A.TableCellBorders tableCellBorders = new A.TableCellBorders();
​A.FillProperties fillProperties = new A.FillProperties();
​A.SolidFill solidFill = new A.SolidFill();A.SchemeColor schemeColor2 = new A.SchemeColor() { Val = A.SchemeColorValues.Accent1 };
​solidFill.Append(schemeColor2);
​fillProperties.Append(solidFill);
​tableCellStyle.Append(tableCellBorders);tableCellStyle.Append(fillProperties);}
​private int GetPartCountInPresentationPart(){PresentationPart presentationPart = m_presentationDoc.PresentationPart;
​if (presentationPart == null){return 0;}
​int iCount = Enumerable.Count<SlidePart>(presentationPart.SlideParts);iCount += Enumerable.Count<CustomXmlPart>(presentationPart.CustomXmlParts);iCount += Enumerable.Count<FontPart>(presentationPart.FontParts);iCount += Enumerable.Count<SlideMasterPart>(presentationPart.SlideMasterParts);
​if (presentationPart.CommentAuthorsPart != null){iCount++;}if (presentationPart.HandoutMasterPart != null){iCount++;}if (presentationPart.LegacyDiagramTextInfoPart != null){iCount++;}if (presentationPart.LegacyDiagramTextInfoPart != null){iCount++;}if (presentationPart.PresentationPropertiesPart != null){iCount++;}if (presentationPart.TableStylesPart != null){iCount++;}if (presentationPart.ThemePart != null){iCount++;}if (presentationPart.UserDefinedTagsPart != null){iCount++;}if (presentationPart.VbaProjectPart != null){iCount++;}if (presentationPart.ViewPropertiesPart != null){iCount++;}if (presentationPart.NotesMasterPart != null){iCount++;}
​return iCount;}
​private int GetSlideCount(){// Check for a null document object.if (m_presentationDoc == null){_throw_assert("The presentation document is empty.");return 0;}
​int slidesCount = 0;
​// Get the presentation part of document.PresentationPart presentationPart = m_presentationDoc.PresentationPart;
​// Get the slide count from the SlideParts.if (presentationPart != null){slidesCount = presentationPart.SlideParts.Count();}
​// Return the slide count to the previous method.return slidesCount;}
​private SlidePart GetSlidePartbyIndex(uint slideIndex){if (m_presentationDoc == null){_throw_assert("The presentation document is empty.");return null;}
​// Use the CountSlides sample to get the number of slides in the presentation.int slidesCount = GetSlideCount();
​if (slideIndex < 0 || slideIndex >= slidesCount){_throw_assert("Slide index is out of range.");return null;}
​// Get the presentation part from the presentation document.PresentationPart presentationPart = m_presentationDoc.PresentationPart;
​// Get the presentation from the presentation part.Presentation presentation = presentationPart.Presentation;
​// Get the list of slide IDs in the presentation.SlideIdList slideIdList = presentation.SlideIdList;
​// Get the slide ID of the specified slideSlideId slideId = slideIdList.ChildElements[Convert.ToInt32(slideIndex)] as SlideId;
​// Get the relationship ID of the slide.string slideRelId = slideId.RelationshipId;
​// Save the modified presentation.presentation.Save();
​// Get the slide part for the specified slide.SlidePart slidePart = presentationPart.GetPartById(slideRelId) as SlidePart;return slidePart;}
​public static void _throw_assert(string error){
#if DEBUGSystem.Diagnostics.Debug.Assert(false);
#elseMessageBox.Show(error,"Error");
#endif}}
​public class CPPTFont{public CPPTFont(){Init();}
​public void Init(){m_strFontName = "等线";m_fontSize = 18;m_bBold = false;m_bItalic = false;m_bUnderline = false;m_bShadow = false;m_strColor = "000000";}
​public string m_strFontName { get; set; }public uint m_fontSize { get; set; }public bool m_bBold { get; set; }public bool m_bItalic { get; set; }public bool m_bUnderline { get; set; }public bool m_bShadow { get; set; }public string m_strColor { get; set; }}
​public class CPPTtext{public CPPTtext(){m_strText = "";m_font = new CPPTFont();}
​public CPPTtext(string strText){m_strText = strText;m_font = new CPPTFont();}
​public CPPTtext(string strText, CPPTFont font){m_strText = strText;m_font = new CPPTFont();m_font = font;}
​public string m_strText { get; set; }public CPPTFont m_font { get; set; }}
​// 表格public class CTableGrid{public CTableGrid(){m_dictTableRow = new Dictionary<uint, CTableGridRow>();}
​public CTableGrid(uint rows, uint cols, double dRowHeight, double dColWidth){m_dictTableRow = new Dictionary<uint, CTableGridRow>();m_listRowHeight = new List<double>();m_listColWidth = new List<double>();
​m_nRows = rows;m_nCols = cols;
​SetRowHeight(dRowHeight);SetColWidth(dColWidth);}
​public void Add(uint iRow, CTableGridRow gridRow){m_dictTableRow.Add(iRow, gridRow);}
​public CTableGridRow Get(uint iRow){if (!m_dictTableRow.ContainsKey(iRow))return null;
​return m_dictTableRow[iRow];}
​public CPPTtableCell GetCell(uint iRow, uint iCol){if (!m_dictTableRow.ContainsKey(iRow))return null;
​return m_dictTableRow[iRow].Get(iCol);}
​public void SetRowHeight(uint iRow, double dRowHeight){if (m_listRowHeight.Count == 0)return;
​if (iRow > m_listColWidth.Count - 1){CPPTtable._throw_assert("Argument Out of Range.");return;}m_listRowHeight[Convert.ToInt32(iRow)] = dRowHeight;}
​public double GetRowHeight(uint iRow){if (iRow > m_listRowHeight.Count - 1){CPPTtable._throw_assert("Argument Out of Range.");return 0.0;}return m_listRowHeight[Convert.ToInt32(iRow)];}
​public void SetColWidth(uint iCol, double dColWidth){if (m_listColWidth.Count == 0)return;
​if (iCol > m_listColWidth.Count - 1){CPPTtable._throw_assert("Argument Out of Range.");return;}m_listColWidth[Convert.ToInt32(iCol)] = dColWidth;}
​public double GetColWidth(uint iCol){if (iCol > m_listColWidth.Count - 1){CPPTtable._throw_assert("Argument Out of Range.");return 0.0;}return m_listColWidth[Convert.ToInt32(iCol)];}
​private void SetRowHeight(double dRowHeight){m_listRowHeight.Clear();for (int iRow = 0; iRow < m_nRows; iRow++){m_listRowHeight.Add(dRowHeight);}}
​private void SetColWidth(double dColWidth){m_listColWidth.Clear();for (int iCol = 0; iCol < m_nCols; iCol++){m_listColWidth.Add(dColWidth);}}
​public void SetCell(uint iRow, uint iCol, CPPTtableCell tableCell){m_dictTableRow[iRow].Set(iCol, tableCell);}
​public uint m_nRows { get; set; }public uint m_nCols { get; set; }public List<double> m_listRowHeight { get; set; }public List<double> m_listColWidth { get; set; }public Dictionary<uint, CTableGridRow> m_dictTableRow { get; set; }//private string style;};
​// 表格行public class CTableGridRow{public CTableGridRow(){m_dictionaryCell = new Dictionary<uint, CPPTtableCell>();}
​public void Set(uint iCol, CPPTtableCell cell){m_dictionaryCell[iCol] = cell;}
​public CPPTtableCell Get(uint iCol){if (!m_dictionaryCell.ContainsKey(iCol))return null;
​return m_dictionaryCell[iCol];}
​public Dictionary<uint, CPPTtableCell> m_dictionaryCell { get; set; }}
​// 表格单元格public class CPPTtableCell{public CPPTtableCell(){m_text = new CPPTtext();}
​public CPPTtableCell(CPPTtext text){m_text = new CPPTtext();m_text = text;}
​public CPPTtext m_text { get; set; }}
}

C# 使用OpenXML创建PPT表格相关推荐

  1. 创建带有表格的PPT

    1.程序说明 1.1编程语言:Java 1.2 第三方库:Apache POI Apache POI 官网: http://poi.apache.org/ 下载页面: http://poi.apach ...

  2. python给ppt表格加边框_带你用Python玩转PPT

    作者 | 陈熹 来源 | 早起Python(ID:zaoqi-python) 头图 | CSDN 下载自东方IC 导读 大家好,今 天依旧是Python办公自动化基础系列,在之前我们分别详细讲解了 今 ...

  3. POI 创建PPT小记

    通用ppt创建步骤 //创建一份pptXMLSlideShow ppt=new XMLSlideShow();//创建第一张幻灯片XSLFSlide slide1 = ppt.createSlide( ...

  4. dotnet OpenXML 读取 PPT 形状边框定义在 Style 的颜色画刷

    本文来和大家聊聊在 PPT 形状使用了 Style 样式的颜色画刷读取方法 在开始之前,期望大家已了解如何在 dotnet 应用里面读取 PPT 文件,如果还不了解读取方法,请参阅 C# dotnet ...

  5. phpexcel_cell 获取表格样式_Java 操作Word表格——创建嵌套表格、添加/复制表格行或列、设置表格是否禁止跨页断行...

    精品推荐 国内稀缺优秀Java全栈课程-Vue+SpringBoot通讯录系统全新发布! Docker快速手上视频教程(无废话版)[免费] 作者:E-iceblue https://www.cnblo ...

  6. 点击按钮创建一个表格 点击按钮创建一个表格 权限选择 元素的value属性操作

    点击按钮创建一个表格 <!DOCTYPE html> <html lang="en"> <head><meta charset=" ...

  7. 如何使用SAP CRM增强工具AET创建Table表格类型的增强

    Jerry创建于2019年1月11日周五. 第一步,创建一个起包裹作用的SAP CRM UI component,用于容纳这个表格类型的UI增强: 点击按钮Create UI component,指定 ...

  8. html的表格使用函数,从另一个HTML表格创建HTML表格的jQuery函数

    我在页面上有一个HTML表格,我想用jQuery从中获取信息,清理/清理它,并创建一个新的"干净的"HTML表格信息.从另一个HTML表格创建HTML表格的jQuery函数 我有表 ...

  9. java预览表格预览文档_java 如何创建一个表格.docx

    java 如何创建一个表格 import java.awt.Component; import java.awt.event.ActionEvent; import java.awt.event.Ac ...

  10. c# 获取word表格中的内容_Java 在Word中创建嵌套表格

    嵌套表格,即在一个大的表格单元格中再嵌进去一个或几个小的表格,使表格内容布局合理.本文将通过java程序来演示如何在Word中创建嵌套表格. 使用工具:Free Spire.Doc for Java ...

最新文章

  1. NeHe教程Qt实现——lesson09
  2. mysql 优化表 3000万_mysql优化:专题三、关于单表查询,可以这么优化
  3. 关于Maven中pom自动补全
  4. linux里面i386 i686 i486 i586代表什么?是什么意思
  5. des加密的c语言程序,C++中四种加密算法之DES源代码
  6. 19. Django进阶:分页
  7. java根据ip获取经纬度城市地址
  8. php如何自动阅卷,智能评卷系统 自动阅卷软件
  9. 学习spf记录引发的问题(一)
  10. 视频异常事件检测Object-centric Auto-encoders and Dummy Anomalies for Abnormal Event Detection in Video
  11. python爬京东联盟_Python爬取京东商品数据
  12. 基于到达时间差(TDOA)的室内定位(/无线传感器网络定位)——极大似然估计ML
  13. 互联网巨头的人工智能野心,你看懂了吗?
  14. java.util.zip 类 ZipEntry
  15. 电脑卡住了怎么办,鼠标无法操作,那就试试快捷键吧
  16. 电脑上上传安装apk到手机或者模拟器上
  17. 网易免费企业邮箱的SMTP、POP服务地址和端口
  18. 【云驻共创】华为云IoTDA服务下的设备管理流程实操
  19. 低成本创业必知的五个“基本点”
  20. 关于如何更改cmd命令行的路径的方法

热门文章

  1. 如何清除计算机搜索框内的搜索历史记录,如何清除搜索框关键字记录
  2. Docker--一门值得你学习的手艺
  3. matlab求组合数不想求组合数矩阵,【潘德的预言】用关系模型与组合数计算NPC最大相容人数和所有组合...
  4. 如何给Word中的图片添加题注
  5. openGL中用Assimp库加载骨骼动画
  6. 英语·句子的五大结构
  7. 经过路由无法找到计算机,共享打印机找不到对方电脑解决方法
  8. git项目文件上不显示图标的问题(绿色,红色)
  9. 如何写好一篇科技论文?
  10. 固态硬盘 格式化 linux,ssd固态硬盘格式化图文详细教程