NPOI 导出Excel图片 (网络)
导出网络图片,需要将网络图片下载到本地或者内存流中,建议下载的时候使用缩略图;
高清图片效率慢;
Uri uri = new Uri(imgPath); //imgPath :网络图片地址
WebRequest webRequest = WebRequest.Create(uri);
using (WebResponse webResponse = webRequest.GetResponse())
{
Bitmap bitmap = new Bitmap(webResponse.GetResponseStream());
using (MemoryStream ms = new MemoryStream())
{
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] bytes = ms.ToArray();
int pictureIdx = workbook.AddPicture(bytes, NPOI.SS.UserModel.PictureType.PNG); //添加图片
HSSFPatriarch patriarch = (HSSFPatriarch)sheet.CreateDrawingPatriarch();
HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 240, col, row, col + 1, row + 1);
//图片位置,图片左上角为(col, row)
HSSFPicture pict = (HSSFPicture)patriarch.CreatePicture(anchor, pictureIdx);
pict.Resize(); //用图片原始大小来显示
}
}