我正在从URL下载图像并保存到C:\ Temp目录中的本地驱动器。
我可以使用以下代码将图片添加到工作表中:
System.Drawing.Image img = System.Drawing.Image.FromFile(strFileLocation);
Excel.Shape shapeAdd = wsNew.Shapes.AddPicture(strFileLocation, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue, 400, 100 * jPageItemNumber, img.Width, img.Height);
shapeAdd.Name = "Item" + jPageItemNumber.ToString() + "_Pic2";
然而,我真正想做的是改变现有形状的图片而不是添加新形状。
我有这个代码,它不会抛出错误,但也绝对没有:
Excel.Shape shpPicture = getShape(wsNew, "Item" + jPageItemNumber.ToString() + "_Pic");
shpPicture.Fill.UserPicture(strFileLocation);
public static Excel.Shape getShape(Excel.Worksheet ws, string strShapeName)
{
foreach (Excel.Shape s in ws.Shapes)
{
if (s.Name == strShapeName)
{
return s;
}
}
return null;
}
我在这里做错了什么?我可以找到无数添加图片的例子,但没有在Excel中更改图片的例子。
此外,如果重要的话,我想“修复”比例,以便在此过程中不会更改它们并扭曲我正在添加的图片。
所以为了让这更有趣,我在同一个项目中有以下代码。此代码创建一个新形状,然后用UserPicture填充它。几乎相同的代码,只有我看到的区别是,在这种情况下,我使用的是新创建的形状而不是现有的形状。而这一个工作。
Excel.Shape shapeStaticMap = wsNew2.Shapes.AddShape(Office.MsoAutoShapeType.msoShapeRectangle, 60, 75, 700, 500);
shapeStaticMap.Fill.UserPicture(strFileLocation);