使用NPOI C#将图像添加到Excel(.xlsx) [英] Add image to Excel (.xlsx) using NPOI C#

查看:76
本文介绍了使用NPOI C#将图像添加到Excel(.xlsx)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用NPOI将图像插入XLSX文件(不是xls).我正在使用 XSSFWorkbook XSSFSheet

I want to insert image to an XLSX file (not xls) using NPOI. I am using XSSFWorkbook and XSSFSheet

byte[] data = File.ReadAllBytes("SomeImage.jpg");
int picInd = workbook.AddPicture(data, XSSFWorkbook.PICTURE_TYPE_JPEG);
XSSFCreationHelper helper = workbook.GetCreationHelper() as XSSFCreationHelper;
XSSFDrawing drawing = _sheet.CreateDrawingPatriarch() as XSSFDrawing;
XSSFClientAnchor anchor = helper.CreateClientAnchor() as XSSFClientAnchor;
anchor.Col1 = 1;
anchor.Row1 = 1;
XSSFPicture pict = drawing.CreatePicture(anchor, picInd) as XSSFPicture;

文件已成功保存.但是在打开它时显示以下错误,并单击是"时,它不会显示图像.

The file is saved successfully. but while opening it showing the following error and on clicking yes, it does not display the image.

推荐答案

我得到了解决方案:

byte[] data = File.ReadAllBytes("someImage.png");
int pictureIndex = workbook.AddPicture(data, PictureType.PNG);
ICreationHelper helper = workbook.GetCreationHelper();
IDrawing drawing = _sheet.CreateDrawingPatriarch();
IClientAnchor anchor = helper.CreateClientAnchor();
anchor.Col1 = 0;//0 index based column
anchor.Row1 = 0;//0 index based row
IPicture picture = drawing.CreatePicture(anchor, pictureIndex);
picture.Resize();

这篇关于使用NPOI C#将图像添加到Excel(.xlsx)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆