使用NPOI将图像插入Excel文件 [英] Insert Image to Excel File Using NPOI

查看:387
本文介绍了使用NPOI将图像插入Excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C#在Visual Studio 2010中编写程序,而我正在使用NPOI库。

I'm writing a program in Visual Studio 2010 using C#, and I'm using the NPOI library.

我正在尝试将图像插入到excel文件。我尝试了两种不同的方法,它们都不起作用。

I'm trying to insert an image to the excel file. I tried 2 different methods and neither of them works.

//Method 1

HSSFPatriarch patriarch = newSheet.CreateDrawingPatriarch() as HSSFPatriarch;
HSSFClientAnchor anchor;
var memoryStream = new MemoryStream();
System.Drawing.Image image = System.Drawing.Image.FromFile("image.jpeg");
image.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Gif);
anchor = new HSSFClientAnchor(0, 0, 255, 255, 0, 0, 0, 0);
anchor.AnchorType = 2; //types are 0, 2, and 3. 0 resizes within the cell, 2 doesn't
int index = newWorkbook.AddPicture(memoryStream.ToArray(), PictureType.JPEG);
HSSFPicture signaturePicture = patriarch.CreatePicture(anchor, index) as HSSFPicture; //ERROR

使用方法1,当我尝试编译时捕获到异常。错误消息是对象引用未设置为对象的实例,错误发生在代码的最后一行。

With method 1, and exception was caught when I try to compile. The error message wasObject reference not set to an instance of an object, and the error occurs at the last line of the code.

//Method 2

byte[] data = File.ReadAllBytes("image.jpeg");
int picInd = newWorkbook.AddPicture(data, XSSFWorkbook.PICTURE_TYPE_JPEG);
XSSFCreationHelper helper = newWorkbook.GetCreationHelper() as XSSFCreationHelper;
XSSFDrawing drawing = newSheet.CreateDrawingPatriarch() as XSSFDrawing;
XSSFClientAnchor anchor = helper.CreateClientAnchor() as XSSFClientAnchor;
anchor.Col1 = 0;
anchor.Row1 = 0;
XSSFPicture pict = drawing.CreatePicture(anchor, picInd) as XSSFPicture;

方法2编译并运行没有问题。但是当我尝试打开创建的excel文件时,我收到一条消息说 Excel在'output.xlsx'中找到了不可读的内容。你想要恢复这个工作簿的内容吗?我恢复了工作簿但仍然没有显示图像。

Method 2 compile and run without issues. But when I try to open the created excel file, I got a message saying Excel found unreadable content in 'output.xlsx'. Do you want to recover the contents of this workbook? I recovered the workbook and still no image was showing.

插入后的下一步图像是克隆同一工作簿中的工作表。使用方法2,根本没有创建克隆表,我不确定一旦图像问题得到解决,这是否会得到解决。

The next step after inserting the image is to Clone the sheet in the same workbook. With method 2, the clone sheet was not created at all, I'm not sure if this will be fix once the image issue is fixed.

有人可以帮助我有了这个?我想知道如何使这两种方法正常工作,或者是否有另一种方法将图像插入excel文件。

Can someone please help me with this? I would like to know how I can make either of the method work properly, or if there's another way to insert image to excel file.

另外,作为一个注释,我使用 XSSFWorkbook XSSFSheet 等等(不是 HSSF ),我的输出文件是 .xlsx

Also, as a note, I'm using XSSFWorkbook, XSSFSheet, and such (not HSSF), and my output file is .xlsx

感谢任何帮助/建议,谢谢!

Any help/suggestion is appreciated, Thanks!

推荐答案

你的方法-2很好。但是你需要在最后一行添加 pict.Resize();

Your Method-2 is fine. But you need to add pict.Resize(); at the last line.

byte[] data = File.ReadAllBytes("image.jpeg");
int picInd = newWorkbook.AddPicture(data, XSSFWorkbook.PICTURE_TYPE_JPEG);
XSSFCreationHelper helper = newWorkbook.GetCreationHelper() as XSSFCreationHelper;
XSSFDrawing drawing = newSheet.CreateDrawingPatriarch() as XSSFDrawing;
XSSFClientAnchor anchor = helper.CreateClientAnchor() as XSSFClientAnchor;
anchor.Col1 = 0;
anchor.Row1 = 0;
XSSFPicture pict = drawing.CreatePicture(anchor, picInd) as XSSFPicture;
pict.Resize();

这篇关于使用NPOI将图像插入Excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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