This project is read-only.

2 pictures in a worksheet

Jul 21, 2009 at 10:16 AM
Edited Jul 21, 2009 at 12:45 PM

Hi,

 

I'm trying to create an Excel Workbook which contains 2 WorkSheet.

The first one contains data, and the 2nd one charts.

As i can't atm create charts with NPOI, i use an other library that give me a System.Drawing.Image item.

I have the following code :

 

                HSSFSheet chartSheet = hssfworkbook.CreateSheet("ChartSheet");
                HSSFPatriarch patriarch = chartSheet.CreateDrawingPatriarch();
                HSSFClientAnchor anchor = new HSSFClientAnchor();

                HSSFPicture picture = patriarch.CreatePicture(anchor,LoadImage(chart,hssfworkbook));
                picture.Resize();

                HSSFPatriarch patriarch2 = chartSheet.CreateDrawingPatriarch();
                HSSFClientAnchor anchor2 = new HSSFClientAnchor(0,0,0,0,0,20,10,30);
                HSSFPicture picture2 = patriarch2.CreatePicture(anchor2, LoadImage(chart1, hssfworkbook));
                picture2.Resize();

And as result, i got only one picture.

 

If I create a 3th sheet, with the 2nd picture, everything is ok.

Have i make a mistake or is it an issue ?

 

Edit : As often, you search several  hour, make a post for heal, and find out the solution without any help ^^

Problem solve :x

Coordinator
Jul 22, 2009 at 12:34 AM

You should use ONLY one HSSFPatriarch for each sheet. I changed your code a bit.

HSSFSheet chartSheet = hssfworkbook.CreateSheet("ChartSheet");
                HSSFPatriarch patriarch = chartSheet.CreateDrawingPatriarch();
                HSSFClientAnchor anchor = new HSSFClientAnchor();
//insert the first picture in the chart sheet
                HSSFPicture picture = patriarch.CreatePicture(anchor,LoadImage(chart,hssfworkbook));
                picture.Resize();

//insert the second picture in the chart sheet

                HSSFClientAnchor anchor2 = new HSSFClientAnchor(0,0,0,0,0,20,10,30);
                HSSFPicture picture2 = patriarch.CreatePicture(anchor2, LoadImage(chart1, hssfworkbook));
                picture2.Resize();


If the code still doesn't work, please feel free to let me know.

Sep 17, 2009 at 4:21 PM
Edited Sep 17, 2009 at 5:36 PM

hi,

   Im a newbiee whith NPOI and having problems while inserting image of top left corner of sheet, i was looking for many examples and worte this:

                //----------------------------------
                HSSFPatriarch patriarch = sheet1.CreateDrawingPatriarch();
                HSSFClientAnchor anchor = new HSSFClientAnchor();
                int pictureIdx = 0;
                using (FileStream fs = new FileStream(@"c:\imgTest.jpg", FileMode.Open))
                {
                    byte[] bytes = new byte[fs.Length];
                    fs.Write(bytes, 0, (int)fs.Length);
                    pictureIdx = wrkBk.AddPicture(bytes, HSSFWorkbook.PICTURE_TYPE_JPEG);
                }
                HSSFPicture picture = patriarch.CreatePicture(anchor, pictureIdx);
                //picture.Resize(); // throw invalid parameter if uncomment
                //----------------------------------
                MemoryStream stream = new MemoryStream();
                wrkBk.Write(stream);
                return stream.GetBuffer();
                //----------------------------------

       above this code i create the workbook and populate whith data, when i open the generated excel file all data is there but i receive a error whith the mesage(in portuguese, my translation may no not exacly the same) "File Error: Data May Have been Lost" and whithout the image i put in the top left corner near first cell. I probably doing some stupid in this code but what ?

      Another doubt, how can i set filters in columns ?

 

Thanks a lot.

 

 

May 10, 2011 at 3:16 PM
Edited May 10, 2011 at 6:16 PM

Hello.

 

I have the same problem.  Finally, did you solve it??, or really, is no way to make it??.

 

Thanks!