OpenXML读取Word中的表格(表格中含有图片),并写入另一文档中

读取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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值