Export gridview to excel with image
New DiscussionHi,
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 = new 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