Skip to content

Export gridview to excel with image

New Discussion
Francesco
Francesco asked on May 26, 2010 9:33 AM

Hi,
I need help with the excel export capability about a web application.

I have an classic asp.net gridview that will be populated at runtime from a custom datatable. In this datatable there is a column that have an image from url.

I need to export all data in my gridview in excel, with image in first cell, but i tried this snippet code with unsuccessfully:

Workbook workbook = new Workbook();
Worksheet worksheet = workbook.Worksheets.Add(“Sheet1”);
string fileName = Path.Combine(Path.GetTempPath(), “myexcel.xls”);
int rowIndex = 0;

foreach (GridViewRow row in mygrid.Rows)
{
    WebClient wc = new WebClient();
string imgUrl = row.Cells[0].Text;
byte[] imageData = wc.DownloadData(imgUrl);
using (MemoryStream ms = new MemoryStream(imageData))
{
System.Drawing.
Image image = System.Drawing.Image.FromStream(ms);
WorksheetImage imageShape = new WorksheetImage(image);
WorksheetCell cellA1 = worksheet.Rows[rowIndex].Cells[0];

 

 

 

 

 

 

        imageShape.TopLeftCornerCell = cellA1;
imageShape.TopLeftCornerPosition =
new PointF(0.0F, 0.0F);
        imageShape.BottomRightCornerCell = cellA1;
imageShape.BottomRightCornerPosition = n
ew PointF(100.0F, 100.0F);
worksheet.Shapes.Add(imageShape);
}

rowIndex++;

}

workbook.Save(fileName);

When I debug this part, in workbook.Save get this error:

System.Runtime.InteropServices.ExternalException non è stata gestita dal codice utente
Message=”Errore generico in GDI+.”
Source=”System.Drawing”
ErrorCode=-2147467259
StackTrace:
in System.Drawing.Image.Save(Stream stream, ImageCodecInfo encoder, EncoderParameters encoderParams)
in System.Drawing.Image.Save(Stream stream, ImageFormat format)
in Infragistics.Excel.Serialization.BIFF8.EscherRecords.BLIP..ctor(Image image)
in Infragistics.Excel.Serialization.BIFF8.EscherRecords.BLIPStoreContainer..ctor(List`1 images)
in Infragistics.Excel.Serialization.BIFF8.EscherRecords.DrawingGroupContainer..ctor(BIFF8WorkbookSerializationManager manager)
in Infragistics.Excel.Serialization.BIFF8.BiffRecords.MSODRAWINGGROUPRecord.Save(BIFF8WorkbookSerializationManager manager)
in Infragistics.Excel.Serialization.BIFF8.BIFF8WorkbookSerializationManager.WriteRecord(BIFF8RecordType type)
in Infragistics.Excel.Serialization.BIFF8.BIFF8WorkbookSerializationManager.WriteWorkbookGlobalData(Boolean hasShapes)
in Infragistics.Excel.Serialization.WorkbookSerializationManager.WriteWorkbookGlobals(Boolean hasShapes)
in Infragistics.Excel.Serialization.BIFF8.BIFF8WorkbookSerializationManager.SaveWorkbookContents(Boolean workbookHasShapes)
in Infragistics.Excel.Serialization.WorkbookSerializationManager.Save()
in Infragistics.Excel.Workbook.SaveBIFF8File(Stream stream)
in Infragistics.Excel.Workbook.SaveHelper(Stream stream, IPackageFactory packageFactory)
in Infragistics.Excel.Workbook.Save(String fileName, IPackageFactory packageFactory)
in Infragistics.Excel.Workbook.Save(String fileName)
in Vendite_Negozi.DettaglioCategorieNegozi.GridToExcel_Click(Object sender, EventArgs e) in D:\Lavori\mygrid\Mypage.aspx.cs:riga 211
in System.Web.UI.WebControls.LinkButton.OnClick(EventArgs e)
in System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument)
in System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
in System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
in System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
in System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
InnerException:

and I don’t undestand why get this error.

Can help me?

The version of Infragistics that I use is the 9.1 for .net 3.5

Thanks

 

 

 

Sign In to post a reply

Replies

  • 0
    [Infragistics]Lyuba
    [Infragistics]Lyuba answered on May 5, 2010 12:35 PM

    Hello Francesco,

     

    The reason for this error is the behavior of the Image.FromStream method. When this method is used it is necessary the stream to be kept open for the lifetime of the image (
    http://msdn.microsoft.com/en-us/library/93z9ee4x.aspx
    ). When you call the save method the stream is closed and this error is shown. In order to avoid this I am suggesting you to replace the call

    System.Drawing.Image image = System.Drawing.Image.FromStream(ms);

    with :

    System.Drawing.Image image = Bitmap.FromFile(imgUrl);

     

    Regards,

    Lyuba Petrova

    Developer Support Engineer

    Infragistics

    http://es.infragistics.com/support

     

  • 0
    [Infragistics] Dimi Gineva
    [Infragistics] Dimi Gineva answered on May 10, 2010 12:04 PM

    Hi,

    I just wanted to know if you war able to export image to excel file or you still need help? Just let me know. Thank you.

    Sincerely,
    Dimi
    Developer Support Engineer
    Infragistics, Inc.

    • 0
      Francesco
      Francesco answered on May 19, 2010 10:22 AM

      Hi,

      sorry for my late.

      I tried the solution supplied, but I give an error with the snippet. My imgUrl member is a string like http://www.mysite.com/mypics.jpg.

      System.Drawing.Image image = Bitmap.FromFile(imgUrl); throw the System.ArgumentException with description: URI formats not supported.
      in System.IO.Path.NormalizePathFast(String path, Boolean fullCheck)
      in System.IO.Path.NormalizePath(String path, Boolean fullCheck)
      in System.IO.Path.GetFullPathInternal(String path)
      in System.IO.Path.GetFullPath(String path)
      in System.Drawing.IntSecurity.UnsafeGetFullPath(String fileName)
      in System.Drawing.IntSecurity.DemandReadFileIO(String fileName)
      in System.Drawing.Image.FromFile(String filename, Boolean useEmbeddedColorManagement)
      in System.Drawing.Image.FromFile(String filename)

      Can you give me support?

      In FromStream method my memorystream will remain open until the shape method:

       

       

       

      byte[] imageData = wc.DownloadData(imgUrl);

       

       

      using (MemoryStream ms = new MemoryStream(imageData))

      {

      System.Drawing.

       

      Image image = System.Drawing.Image.FromStream(ms);

       

       

      //System.Drawing.Image image = Bitmap.FromFile(imgUrl);

       

       

       

      WorksheetImage imageShape = new WorksheetImage(image);

       

       

      WorksheetCell cellA1 = worksheet.Rows[rowIndex].Cells[0];

      imageShape.TopLeftCornerCell = cellA1;

       

      imageShape.TopLeftCornerPosition =

       

      new PointF(0.0F, 0.0F);

      imageShape.BottomRightCornerCell = cellA1;

       

      imageShape.BottomRightCornerPosition =

       

      new PointF(100.0F, 100.0F);

      worksheet.Shapes.Add(imageShape);

      }

      Thanks

       

       

      • 0
        Francesco
        Francesco answered on May 25, 2010 4:53 PM

        Hi Dimi,
        have you a good news for my problem?

        My customer press me for a solution.

        Thank you, best regards,
        Francesco

  • 0
    [Infragistics]Lyuba
    [Infragistics]Lyuba answered on May 26, 2010 6:05 AM

    Hello Francesco,

    Then I am suggesting you to manually convert the byte array to image:

     

        public static System.Drawing.Image byteArrayToImage(byte[] byteArrayIn)

            {

                try

                {

                    if (byteArrayIn.Length > 0)

                    {                 

                         using (MemoryStream ms = new MemoryStream(byteArrayIn))

                         {

                             using (Bitmap Origninal = new Bitmap(ms))

                             {

                                 Bitmap returnBmp = new Bitmap(Origninal.Width, Origninal.Height);

                                 Graphics g = Graphics.FromImage(returnBmp);

                                 g.DrawImage(Origninal, 0, 0, Origninal.Width, Origninal.Height);

                                

                                 // notice    the two parameters     with bold style,

                                 // if you don't add the two parameters,the image drew will be

                                 // multilated or unbalanced

                                 g.Dispose();

                                 ms.Close();

                                 return (System.Drawing.Image)returnBmp;

                             }                  

                         }                     

                    }

                    return null;

                }

                catch

                {

                    return null;

                }

            }

    And in your code you should call this method when you are creating the image object:

    System.Drawing.Image image = byteArrayToImage(imageData); (instead of System.Drawing.Image image = System.Drawing.Image.FromStream(ms);)

     

    Hope this helps.

     

    Regards,

    Lyuba

    Developer Support Engineer

    Infragistics

    http://es.infragistics.com/support

     

     

    • 0
      Francesco
      Francesco answered on May 26, 2010 9:33 AM

      Hi Lyuba,
      your suggestion work fine for me.

      Thanks for help.
      Francesco

  • You must be logged in to reply to this topic.
Discussion created by
Favorites
Replies
Created On
Last Post
Discussion created by
Francesco
Favorites
0
Replies
6
Created On
May 26, 2010
Last Post
15 years, 9 months ago

Suggested Discussions

Created by

Created on

May 26, 2010 9:33 AM

Last activity on

Feb 11, 2026 2:28 PM