读取word中的表格
public static DataTable ReadWord(string path)
{
DataTable dt = new DataTable();
using(DocumentFormat.OpenXml.Packaging.WordprocessingDocument doc = Document.OpenXml.Packaging.WordprocessingDocument.Open(path,true))
{
Document.OpenXml.WordProcessing.Body body = doc.MainDocumentPart.Document.Body;
for(int i=0;i<body.FirstChild.ChildElements[2].ToArray().count();i++)
{
dt.Columns.Add(body.FirstChild.ChildElements[2].ChildElements[i].InnerText.Trim());
}
for(int i=3;i<body.FirstChild.ToArray().Count();i++)
{
List<Object> rowDatas = new List<Object>();
for(int columnIndex=0;columnIndex<body.FirstChild.ChildElements[2].ToArray().Count();columnIndex++)
{
if(body.FirstChild.ChildElements[i].ChildElements[columnIndex].InnerText.Trim()!=null)
{
rowDatas.Add(body.FirstChild.ChildElements[i].ChildElements[columnIndex].InnerText.Trim());
}
else if(body.FirstChild.ChildElements[i].ChildElements[columnIndex]..LastChild.LastChild.LastChild is DocumentFormat.OpemXml.Wordprocessing.Drawing)
{
DocumentFormat.OpemXml.Wordprocessing.Drawing drawings = body.FirstChild.ChildElements[i].ChildElements[columnIndex]..LastChild.LastChild.LastChild as DocumentFormat.OpemXml.Wordprocessing.Drawing;
car inline = drawings.Inline;
var extent = inLine.Extent;
var pic = inLine.Graphic.GraphicData.GetFirstChild<DocumentFormat.OpenXml.Drawing.Pictures.Picture>();
var embed = pic.BlipFill.Blip.Embed.Value;
var part = doc.MainDocumentPart.GetPartById(embed);
var stream = part.GetStream();
byte[] bytes = new byte[stream.Length];
stream.Read(bytes,0,bytes.Length);
string imgHtml = "<img style='width:{ImageExtent.EMU_TO_PX((decimal)extent.Cx.Value).ToString('0.')}px.height:{ImageExtent.EMU_TO_PX((decimal)extent.Cx.Value).ToString('0.')}px' src='data:{part.ContentType};base64,'+Convert.ToBase64String(bytes)+"'/>";
rowDatas.Add(Convert.ToBase64String(bytes));
}
}
dt.Rows.Add(rowDatas.ToArray());
}
return dt;
}
}
写入另一文档中
var newTableAdd = this.WordUtility.CreateTable(dt,colimns,this.WordUtility);
this.WordUtility.InsertTable(tagElement,newTableAdd);
tagElement = newTableAdd;
var nullPar = this.WordUtility.CreateParagraph("");
this.WordUtitlity.InsertParagraph(tagElement,nullPar);
根据数组数据创建表格
public Table CreateTable(List<object []> rowsData,Columns columns,OpenXmlWordUtility word2,int borderWidth = 2)
{
//边框宽
UInt32Value widthValue = new UInt32Value((uint)borderWidth);
//设置表格边框
TableProperties tblProp = new TableProperties(
new TableBorders(
new TopBorder(){Val = new EnumValue<BorderValues>(BorderValues.Single),Size=widthValue},
new BottomBorder(){Val = new EnumValue<BorderValues>(BorderValues.Single),Size=widthValue},
new LeftBorder(){Val = new EnumValue<BorderValues>(BorderValues.Single),Size=widthValue},
new RightBorder(){Val = new EnumValue<BorderValues>(BorderValues.Single),Size=widthValue},
new InsideHorizontalBorder(){Val = new EnumValue<BorderValues>(BorderValues.Single),Size=widthValue},
new InsideVerticalBorder(){Val = new EnumValue<BorderValues>(BorderValues.Single),Size=widthValue},
)
);
Table table = new Table();
table.Append(tblProp);
//列头
var columnHeaderRow = this.CreateColumnHeaderRow(new TableStyle(){RepeatHeaderRow = true},columns,500);
table.AppendChild(columnHeaderRow );
//添加数据
for(int i=0;i<rowsData.Count;i++)
{
TableRow row = new TableRow();
TableCell cell1 = new TableCell();
cell1.Append(new Paragraph(new Run(new Text(rowsData[i][0].ToString()))));
row.Append(cell1);
TableCell cell2 = new TableCell();
string base64 = rowsData[i][1].ToString();
if(base64.length>4000)
{
byte[] bytes = Convert.FromBase64String(base64);
MemoryStream menStream = new MemoryStream(bytes);
BinaryFormatter binFormatter = new BinaryFormatter();
var picture = word2.CreatePicture(FileBaseUtility.GetBinaryDataStream(base64),"Png");
cell2.Append(new Paragraph(new Run(picture)));
}
else
{
cell2.Append(new Paragraph(new Run(new Text(rowsData[i][1].ToString()))));
}
row.Append(cell2);
table.Append(row);
}
return table;
}