var path = HttpContext.Current.Server.MapPath(logoPath);
if (!string.IsNullOrEmpty(path) && File.Exists(path))
{
byte[] bytes = System.IO.File.ReadAllBytes(path);
int pictureIdx = workbook.AddPicture(bytes, PictureType.JPEG);
ISheet sheet = workbook.GetSheetAt(0);
if (sheet != null)
{
XSSFDrawing drawing = (XSSFDrawing) sheet.CreateDrawingPatriarch();
/* Create an anchor point /
XSSFClientAnchor anchor = new XSSFClientAnchor(0, 0, 32, 32,0, 0, 1, 1);
/ Invoke createPicture and pass the anchor point and ID */
XSSFPicture picture = (XSSFPicture) drawing.CreatePicture(anchor, pictureIdx);
}
}
插入的图片被拉伸填充在HSSFClientAnchor指定的区域。有时可能我们并不需要拉伸的效果,怎么办呢?很简单,在最后加上这样一句用来自动调节图片大小:
picture.Resize();