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;
}
-
方法二:通过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; //长度换算成cm
private 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);
}
// 打开PPT
public 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;
else
m_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"
// wholeTable1
A.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);
// band1Vertical1
A.Band1Vertical band1Vertical1 = new A.Band1Vertical();
A.TableCellStyle tableCellStyle4 = new A.TableCellStyle();
A.TableCellBorders tableCellBorders4 = new A.TableCellBorders();
tableCellStyle4.Append(tableCellBorders4);
band1Vertical1.Append(tableCellStyle4);
// band2Vertical1
A.Band2Vertical band2Vertical1 = new A.Band2Vertical();
A.TableCellStyle tableCellStyle5 = new A.TableCellStyle();
A.TableCellBorders tableCellBorders5 = new A.TableCellBorders();
tableCellStyle5.Append(tableCellBorders5);
band2Vertical1.Append(tableCellStyle5);
// lastColumn1
A.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);
// firstColumn1
A.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);
// lastRow1
A.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);
// firstRow1
A.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 slide
SlideId 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 DEBUG
System.Diagnostics.Debug.Assert(false);
#else
MessageBox.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; }
}
}