使用OpenXml操作Excel,实现在固定位置插入图片<二>

今天感觉状态还不错,就接着写,来个一气呵成。

下面是在Excel中插入图片,中间实现时经历了一个小水坑,遇到的问题是:能插入图片,但图片的位置和大小都不受控制,不知是否也有人遇见过,到最后才发现,原来是换算单位的原因,英寸和像素的换算单位不一样。希望看到这篇文章的亲们,对你能有帮助。

/// <summary>
/// 添加图片
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <param name="sImagePath"></param>
private static void InsertImage(long x, long y, string sImagePath)
{

InsertImage(x, y, null, null, sImagePath);
}
private static void InsertImage(long x, long y, long? width, long? height, string sImagePath)
{
try
{
//工作表中的绘制区域,若不存在则在工作表中增加绘制区域,存在则使用已存在的绘制区域
WorksheetPart wsp = CurrentWorksheetPart;
DrawingsPart dp;
ImagePart imgp;
WorksheetDrawing wsd;
ImagePartType ipt;
//判断要插入图片的图片类型
switch (sImagePath.Substring(sImagePath.LastIndexOf('.') + 1).ToLower())
{
case "png":
ipt = ImagePartType.Png;
break;
case "jpg":
case "jpeg":
ipt = ImagePartType.Jpeg;
break;
case "gif":
ipt = ImagePartType.Gif;
break;
case "tiff":
ipt = ImagePartType.Tiff;
break;
default:
return;
}

if (wsp.DrawingsPart == null)
{
//----- no drawing part exists, add a new one

dp = wsp.AddNewPart<DrawingsPart>();
imgp = dp.AddImagePart(ipt, wsp.GetIdOfPart(dp));
wsd = new WorksheetDrawing();
}
else
{
//----- use existing drawing part

dp = wsp.DrawingsPart;
imgp = dp.AddImagePart(ipt);
dp.CreateRelationshipToPart(imgp);
wsd = dp.WorksheetDrawing;
}

using (FileStream fs = new FileStream(sImagePath, FileMode.Open))
{
imgp.FeedData(fs);
}

int imageNumber = dp.ImageParts.Count<ImagePart>();
if (imageNumber == 1)
{
Drawing drawing = new Drawing();
drawing.Id = dp.GetIdOfPart(imgp);
wsp.Worksheet.Append(drawing);
}
//插入图片的相关属性设置
NonVisualDrawingProperties nvdp = new NonVisualDrawingProperties();
nvdp.Id = new UInt32Value((uint)(1024 + imageNumber));
nvdp.Name = "Picture " + imageNumber.ToString();
nvdp.Description = "";
DocumentFormat.OpenXml.Drawing.PictureLocks picLocks = new DocumentFormat.OpenXml.Drawing.PictureLocks();
picLocks.NoChangeAspect = true;
picLocks.NoChangeArrowheads = true;
NonVisualPictureDrawingProperties nvpdp = new NonVisualPictureDrawingProperties();
nvpdp.PictureLocks = picLocks;
NonVisualPictureProperties nvpp = new NonVisualPictureProperties();
nvpp.NonVisualDrawingProperties = nvdp;
nvpp.NonVisualPictureDrawingProperties = nvpdp;

DocumentFormat.OpenXml.Drawing.Stretch stretch = new DocumentFormat.OpenXml.Drawing.Stretch();
stretch.FillRectangle = new DocumentFormat.OpenXml.Drawing.FillRectangle();

BlipFill blipFill = new BlipFill();
DocumentFormat.OpenXml.Drawing.Blip blip = new DocumentFormat.OpenXml.Drawing.Blip();
blip.Embed = dp.GetIdOfPart(imgp);
blip.CompressionState = DocumentFormat.OpenXml.Drawing.BlipCompressionValues.Print;
blipFill.Blip = blip;
blipFill.SourceRectangle = new DocumentFormat.OpenXml.Drawing.SourceRectangle();
blipFill.Append(stretch);
//在Excel中插入的图片的起始点
DocumentFormat.OpenXml.Drawing.Transform2D t2d = new DocumentFormat.OpenXml.Drawing.Transform2D();
DocumentFormat.OpenXml.Drawing.Offset offset = new DocumentFormat.OpenXml.Drawing.Offset();
offset.X = 0;
offset.Y = 0;
t2d.Offset = offset;
Bitmap bm = new Bitmap(sImagePath);

DocumentFormat.OpenXml.Drawing.Extents extents = new DocumentFormat.OpenXml.Drawing.Extents();

if (width == null)
extents.Cx = (long)bm.Width * (long)((float)9525);
else
extents.Cx = width * (long)((float)9525);

if (height == null)
extents.Cy = (long)bm.Height * (long)((float)9525);
else
extents.Cy = height * (long)((float)9525);

bm.Dispose();
t2d.Extents = extents;
ShapeProperties sp = new ShapeProperties();
sp.BlackWhiteMode = DocumentFormat.OpenXml.Drawing.BlackWhiteModeValues.Auto;
sp.Transform2D = t2d;
DocumentFormat.OpenXml.Drawing.PresetGeometry prstGeom = new DocumentFormat.OpenXml.Drawing.PresetGeometry();
prstGeom.Preset = DocumentFormat.OpenXml.Drawing.ShapeTypeValues.Rectangle;
prstGeom.AdjustValueList = new DocumentFormat.OpenXml.Drawing.AdjustValueList();
sp.Append(prstGeom);
sp.Append(new DocumentFormat.OpenXml.Drawing.NoFill());

DocumentFormat.OpenXml.Drawing.Spreadsheet.Picture picture = new DocumentFormat.OpenXml.Drawing.Spreadsheet.Picture();
picture.NonVisualPictureProperties = nvpp;
picture.BlipFill = blipFill;
picture.ShapeProperties = sp;

Position pos = new Position();
//在Excel中设置插图片位置
pos.X = x * 9525;
pos.Y = y * 9525;
//pos.X = 72 * 9525;/*x * 914400 / 72;*/
//pos.Y = 180 * 9525;/* y * 914400 / 72;*/
Extent ext = new Extent();
//ext.Cx = extents.Cx;
//ext.Cy = extents.Cy;
//在Excel中设置插图片的宽度和高度
ext.Cx = 216 * 9525; /*extents.Cx;*/
ext.Cy = 252 * 9525;/*extents.Cy;*/
AbsoluteAnchor anchor = new AbsoluteAnchor();

anchor.Position = pos;
anchor.Extent = ext;
anchor.Append(picture);
anchor.Append(new ClientData());
wsd.Append(anchor);
wsd.Save(dp);
}
catch (Exception ex)
{
throw ex; // or do something more interesting if you want
}
}

 

posted on 2018-01-28 11:55  单子  阅读(1188)  评论(1编辑  收藏  举报