473,407 Members | 2,326 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Exception while Exporting Datagridview data with Images to Excel...

Hi Everyone,

I have datagridview in that 17 columns are text columns and 5 columns are Image columns,when I'm trying to export whole gridview data's along with images to EXCEL,getting "HRESULT: 0x800A03EC" Error..While debugging the code I'm getting the error on image column not able to export..
Can anybody help us to resolve this issue..


Here is my code which i used:

Expand|Select|Wrap|Line Numbers
  1. private void btnexportexcel_Click(object sender, EventArgs e)
  2.          {
  3.              try
  4.              {
  5.                  SaveFileDialog savefile = new SaveFileDialog();
  6.                  savefile.Filter = "Excel (*.xls)|*.xls";
  7.                  if (savefile.ShowDialog() == DialogResult.OK)
  8.                  {
  9.                      if (!savefile.FileName.Equals(string.Empty))
  10.                      {
  11.                          FileInfo finfo = new FileInfo(savefile.FileName);
  12.                          if (finfo.Extension.Equals(".xls"))
  13.                          {
  14.                              Excel.Application xlApp;
  15.                              Excel.Workbook xlWorkBook;
  16.                              Excel.Worksheet xlWorkSheet;
  17.                              object misValue = System.Reflection.Missing.Value;
  18.  
  19.                              xlApp = new Excel.Application();
  20.                              xlWorkBook = xlApp.Workbooks.Add(misValue);
  21.                              xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(2);
  22.                              int i = 0;
  23.                              int j = 0;
  24.  
  25.                              for (i = 0; i <= dataGridView1.RowCount - 1; i++)
  26.                              {
  27.                                  for (j = 0; j <= dataGridView1.ColumnCount - 1; j++)
  28.                                  {
  29.                                      DataGridViewCell cell = dataGridView1[j, i];
  30.                                      xlWorkSheet.Cells.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
  31.                                      xlWorkSheet.Columns.AutoFit();
  32.  
  33.                                      if (cell.Value.GetType() == typeof(Bitmap))
  34.                                      {
  35.                                              string image =Convert.ToString(dataGridView1.CurrentRow.Cells[i].Value); 
  36.                                              Excel.Range oRange = (Excel.Range)xlWorkSheet.Cells[i + 1, j + 1];
  37.                                              float Left = (float)((double)oRange.Left);
  38.                                              float Top = (float)((double)oRange.Top);
  39.                                              const float ImageSize = 32;
  40.                                              xlWorkSheet.Shapes.AddPicture(image, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue, Left, Top, ImageSize, ImageSize);
  41.                                              oRange.RowHeight = ImageSize + 2;         
  42.                                      }
  43.                                      else
  44.                                      {
  45.                                          xlWorkSheet.Cells[i + 1, j + 1] = cell.Value;
  46.                                      }
  47.  
  48.                                  }
  49.                              }
  50.  
  51.                              xlWorkBook.SaveAs(savefile.FileName, Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue,
  52.                                                 Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
  53.                              xlWorkBook.Close(true, misValue, misValue);
  54.                              xlApp.Quit();
  55.  
  56.                              releaseObject(xlWorkSheet);
  57.                              releaseObject(xlWorkBook);
  58.                              releaseObject(xlApp);
  59.  
  60.                              MessageBox.Show("Excel file created , you can find the file " + savefile.FileName);
  61.  
  62.                          }
  63.                          else
  64.                          {
  65.                              MessageBox.Show("Invalid file type");
  66.                          }
  67.                      }
  68.                      else
  69.                      {
  70.                          MessageBox.Show("You did pick a location " + "to save file to");
  71.                      }
  72.                  }
  73.              }
  74.  
  75.              catch (Exception ex)
  76.              {
  77.                  MessageBox.Show("Exception Occured", ex.Message);
  78.              }      
  79.          }
Apr 22 '15 #1
0 2025

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: chemdawg | last post by:
I"m Having a problem Sharing data between access Microsoft® Excel. I need to populate the databases in a flash by linking and updating the excel sheets.
1
by: michb | last post by:
I need to export data from access to an excel database to calculate times and wages for the week but the code that I entered doesn't seem to like either me or the database! I need it to go to one...
2
by: bienwell | last post by:
Hi, I have a question about exporting data from datagrid control into Excel file in ASP.NET. On my Web page, I have a linkbutton "Export data". This link will call a Sub Function to perform...
0
by: Sridhar | last post by:
Hi, I am having trouble renaming the excel sheet while exporting to excel. we have a datagrid that contains some analytical data. I have the name to the excel file as "temp.xls" inside the code....
5
by: jonas | last post by:
I have a series of arrays that I would like to send to excel for display and processing After spending most of the evening google I cant find anything out there that gives an example to what I...
1
by: deedi | last post by:
Hi, I've a problem with exporting my query results into excel. When the rows returned are less, export to excel is happening without any issues. When there is large number of rows, IE is...
0
by: lenniekuah | last post by:
Hi Friends, I need your help. Please help me. I am trying to retrieve data from Excel Spreadsheet to fill the DataGridView for display prior to updating SQL SERVER with individual DataGridView...
1
by: balaji04 | last post by:
Hi, I've a problem with exporting my query results into excel. When the rows returned are less, export to excel is happening without any issues. When there is large number of rows, IE is showing...
5
by: leelaprasad9999 | last post by:
hi i'm trying to export datagridview data with image to excel , i'm using the below code, but image cannot be displayed. Microsoft.Office.Interop.Excel.Application ExcelApp = new...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.