转载 https://www.cnblogs.com/zhangliming/archive/2013/04/02/2995655.html
我需要重复操作的word 根据对应人物名称插入的不同位置的 单元格,一次只能插入一张图片
/// <summary>
/// 导出word
/// </summary>
/// <param name="fileName">原文件名</param>
/// <param name="outPath">输出路径</param>
/// <param name="record">要输入数据信息</param>
/// <returns></returns>
public static bool ExportMeetingRecord(string fileName, string outPath, NpoiTransmitData record)
{
XWPFDocument doc = null;
using (FileStream file = new FileStream(fileName, FileMode.Open, FileAccess.Read))
{
doc = new XWPFDocument(file);
}
try
{
//遍历表格
var tables = doc.Tables;
if (tables == null || tables.Count == 0)
{
}
else
{
var table = doc.Tables[doc.Tables.Count - 1];
foreach (var row in table.Rows)
{
List<XWPFTableCell> cells = row.GetTableCells();
for (int i = 0; i < cells.Count; i = i + 2)
{
XWPFTableCell cell = cells[i];
string text = cell.GetText();
if (!text.Trim().Equals(record.name)) continue;
string imagePath = record.value;
if (string.IsNullOrEmpty(imagePath)) continue;
string picId = string.Empty;
using (FileStream imageFileStream = new FileStream(imagePath, FileMode.Open))
{
picId = doc.AddPictureData(imageFileStream, (int)NPOI.XWPF.UserModel.PictureType.JPEG);
}
if (string.IsNullOrEmpty(picId)) continue;
//cells[i + 1].SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
string imageName = Path.GetFileNameWithoutExtension(imagePath);
CreatePicture(cells[i + 1], picId, (uint)i, 64, 64);
//CreatePicture2(cells[i + 1], picId, 64, 64);
//setCellImage(cells[i + 1], 3 * 256, 6 * 256, imagePath, NPOI.XWPF.UserModel.PictureType.JPEG);
}
}
}
FileStream file = new FileStream(outPath, FileMode.Create, FileAccess.Write);
doc.Write(file);
file.Close();
file.Dispose();
return true;
}
catch (Exception ex)
{
throw ex;
}
}
注意
$"<pic:cNvPr id="{id + 1}" name=“图片 {id + 1}”/>"
与
docPr.id = id+1;
docPr.name = "图片 " + (id + 1);
尽量保持一致,以免出现 插入一次图片后 word 文件无法读取的问题。
/// <summary>
/// 创建图片
/// </summary>
/// <param name="cell">插入的单元格</param>
/// <param name="imageId">插入word时生成的image的ID</param>
/// <param name="id">id索引(如1,2等)区别 不同图片</param>
/// <param name="width">要插入的图片宽度</param>
/// <param name="height">要插入的图片高度</param>
public static void CreatePicture(XWPFTableCell cell, string imageId, uint id, int width, int height)
{
int EMU = 9525;
width *= EMU;
height *= EMU;
string picXml = "<pic:pic xmlns:pic=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">"
+ "<pic:nvPicPr>"
+ $"<pic:cNvPr id=\"{id + 1}\" name=\"图片 {id + 1}\"/>"
+ "<pic:cNvPicPr/>"
+ "</pic:nvPicPr>"
+ "<pic:blipFill>"
+ $"<a:blip r:embed=\"{imageId}\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\">"//cstate=\"print\"
// + "<a:extLst>"
// + "<a:ext uri=\"\">"//{ 28A0092B-C50C-407E-A947-70E740481C1C}
//+ "<a14:useLocalDpi xmlns:a14=\"http://schemas.microsoft.com/office/drawing/2006/main\" val=\"0\"/>"
// + "</a:ext>"
// + "</a:extLst>"
+ "</a:blip>"
+ "<a:stretch>"
+ "<a:fillRect/>"
+ "</a:stretch>"
+ "</pic:blipFill>"
+ "<pic:spPr>"
+ "<a:xfrm>"
+ "<a:off x=\"0\" y=\"0\"/>"
+ $"<a:ext cx=\"{width}\" cy=\"{height}\"/>"
+ "</a:xfrm>"
+ "<a:prstGeom prst=\"rect\">"
+ "<a:avLst/>"
+ "</a:prstGeom>"
+ "</pic:spPr>"
+ "</pic:pic>";
//var run = doc.CreateParagraph().CreateRun();
XWPFParagraph newPara = cell.AddParagraph();
newPara.Alignment = ParagraphAlignment.CENTER;
XWPFRun imageCellRunn = newPara.CreateRun();
CT_Inline inline = imageCellRunn.GetCTR().AddNewDrawing().AddNewInline();
inline.graphic = new CT_GraphicalObject();
inline.graphic.graphicData = new CT_GraphicalObjectData();
inline.graphic.graphicData.uri = "http://schemas.openxmlformats.org/drawingml/2006/picture";
try
{
inline.graphic.graphicData.AddPicElement(picXml);
}
catch (XmlException xe)
{
}
NPOI.OpenXmlFormats.Dml.WordProcessing.CT_PositiveSize2D extent = inline.AddNewExtent();
extent.cx = width;
extent.cy = height;
NPOI.OpenXmlFormats.Dml.WordProcessing.CT_NonVisualDrawingProps docPr = inline.AddNewDocPr();
docPr.id = id+1;
docPr.name = "图片 " + (id + 1);
}