NPOI 操作导出Word插入图片保存bug --HRESULT 0xc00ce020
需求:将报告数据、图片导出到docx 。
于是乎上手干:
try
{
XWPFDocument doc = new XWPFDocument();
XWPFParagraph p1 = doc.CreateParagraph();
XWPFRun run = p1.CreateRun();
string picName = "1.png";
using (FileStream fsImg = new FileStream(picName, FileMode.Open, FileAccess.Read))
{
run.AddPicture(fsImg, (int)PictureType.PNG, "1", (int)(200 * 9525), (int)(200 * 9525));
}
using (FileStream fs = new FileStream("1.docx", FileMode.Create))
{
doc.Write(fs);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
一顿操作猛如虎 wps打开没毛病 ,but Microsoft Word 打开报错

找啊找啊找,发现问题出在document.xml,直接上代码
static void CreateDoc()
{
try
{
XWPFDocument doc = new XWPFDocument();
XWPFParagraph p1 = doc.CreateParagraph();
XWPFRun run = p1.CreateRun();
string picName = "1.png";
using (FileStream fsImg = new FileStream(picName, FileMode.Open, FileAccess.Read))
{
//run.AddPicture(fsImg, (int)PictureType.PNG, "1", (int)(200 * 9525), (int)(200 * 9525));
var embedID = doc.AddPictureData(fsImg, (int)PictureType.PNG);
uint picID = 1;
CreatePicture(run, picID, picName, embedID, 200, 200);
}
using (FileStream fs = new FileStream("1.docx", FileMode.Create))
{
doc.Write(fs);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
public static void CreatePicture(XWPFRun run, uint picID, string picName, string embedID, int width, int height)
{
try
{
int EMU = 9525;
width *= EMU;
height *= EMU;
CT_Inline inline = run.GetCTR().AddNewDrawing().AddNewInline();
inline.distT = inline.distB = inline.distL = inline.distR = 0;
inline.graphic = new NPOI.OpenXmlFormats.Dml.CT_GraphicalObject();
NPOI.OpenXmlFormats.Dml.CT_GraphicalObjectData graphicData = inline.graphic.AddNewGraphicData();
graphicData.uri = "http://schemas.openxmlformats.org/drawingml/2006/picture";
string picXmlstr = "";
StringBuilder sb = new StringBuilder();
sb.Append("<pic:pic xmlns:pic=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">");
sb.Append("<pic:nvPicPr>");
sb.Append(string.Format("<pic:cNvPr id=\"{0}\" name=\"{1}\" descr=\"{2}\"/>", 1,picName,picName));
sb.Append("<pic:cNvPicPr>");
sb.Append("<a:picLocks noChangeAspect=\"1\"/>");
sb.Append("</pic:cNvPicPr>");
sb.Append("</pic:nvPicPr><pic:blipFill>");
sb.Append(string.Format("<a:blip r:embed=\"{0}\"/>", embedID));
sb.Append("<a:stretch><a:fillRect/></a:stretch></pic:blipFill><pic:spPr><a:xfrm>");
sb.Append("<a:off x=\"0\" y=\"0\"/>");
sb.Append("<a:ext cx=\"609600\" cy=\"609600\"/>");
sb.Append("</a:xfrm><a:prstGeom prst=\"rect\"><a:avLst/></a:prstGeom></pic:spPr></pic:pic>");
picXmlstr = sb.ToString();
graphicData.AddPicElement(picXmlstr);
CT_PositiveSize2D extent = inline.AddNewExtent();
extent.cx = width;
extent.cy = height;
CT_EffectExtent effectExtent = new CT_EffectExtent();
effectExtent.l = (long)19050;
effectExtent.t = effectExtent.r = effectExtent.b = 0;
inline.effectExtent = effectExtent;
CT_NonVisualDrawingProps docPr = inline.AddNewDocPr();
docPr.id = picID;
docPr.name = picName;
docPr.descr = picName;
CT_NonVisualGraphicFrameProperties cT_Non = new CT_NonVisualGraphicFrameProperties();
NPOI.OpenXmlFormats.Dml.CT_GraphicalObjectFrameLocking cT_Graphical = new NPOI.OpenXmlFormats.Dml.CT_GraphicalObjectFrameLocking();
cT_Graphical.noChangeAspect = true;
cT_Non.graphicFrameLocks = cT_Graphical;
inline.cNvGraphicFramePr = cT_Non;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
搞定收工😄

浙公网安备 33010602011771号