这是个非常简单的例子用来说明移处/添加列的一般方法
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Drawing;
using P = DocumentFormat.OpenXml.Presentation;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private string Path { set; get; }
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog objOpenFileDialog = new OpenFileDialog();
objOpenFileDialog.Filter = "PPT (*.pptx)|*.pptx";
objOpenFileDialog.ShowDialog();
Path = objOpenFileDialog.FileName;
if (Path.Length > 0)
{
button2.Enabled = true;
button3.Enabled = true;
}
}
private Table GetTable(PresentationDocument Part)
{
PresentationPart objPresentationPart = Part.PresentationPart;
SlidePart objSlidePart = objPresentationPart.SlideParts
.First<SlidePart>();
P.Slide objSlide = objSlidePart.Slide;
P.CommonSlideData objCommonSlideData = objSlide.CommonSlideData;
P.ShapeTree objShapeTree = objCommonSlideData.ShapeTree;
P.GraphicFrame objGraphicFrame = objShapeTree
.Descendants<P.GraphicFrame>().FirstOrDefault();
Graphic objGraphic = objGraphicFrame.Graphic;
GraphicData objGraphicData = objGraphic.GraphicData;
Table table = objGraphicData.Descendants<Table>().FirstOrDefault();
return table;
}
private void button2_Click(object sender, EventArgs e)
{
if (Path.Length > 0)
{
using (PresentationDocument objPresentationDocument =
PresentationDocument.Open(Path,true))
{
Table objTable = GetTable(objPresentationDocument);
TableGrid objTableGrid = objTable.TableGrid;
GridColumn objGridColumn = objTableGrid
.Descendants<GridColumn>().FirstOrDefault();
objGridColumn.Remove();
List<TableRow> Rows = objTable.Descendants<TableRow>()
.ToList();
for (int i = 0; i < Rows.Count; i++)
{
TableRow Row = Rows[i];
TableCell objTableCell = Row.Descendants<TableCell>()
.FirstOrDefault();
objTableCell.Remove();
}
objPresentationDocument.PresentationPart.SlideParts
.First<SlidePart>().Slide.Save();
}
}
}
private void button3_Click(object sender, EventArgs e)
{
if (Path.Length > 0)
{
using (PresentationDocument objPresentationDocument =
PresentationDocument.Open(Path, true))
{
Table objTable = GetTable(objPresentationDocument);
TableGrid objTableGrid = objTable.TableGrid;
GridColumn objGridColumn = new GridColumn()
{
Width = 1234000L
};
objTableGrid.Append(objGridColumn);
List<TableRow> Rows = objTable.Descendants<TableRow>()
.ToList();
for (int i = 0; i < Rows.Count; i++)
{
TableRow Row = Rows[i];
TableCell newCell = new TableCell();
TextBody objTextBody = new TextBody();
Paragraph objParagraph = new Paragraph();
TableCellProperties objTableCellProperties =
new TableCellProperties();
BodyProperties objBodyProperties = new BodyProperties();
objTextBody.Append(objBodyProperties);
objTextBody.Append(objParagraph);
newCell.Append(objTextBody);
newCell.Append(objTableCellProperties);
Row.Append(newCell);
}
objPresentationDocument.PresentationPart.SlideParts
.First<SlidePart>().Slide.Save();
}
}
}
}
}