473,396 Members | 1,879 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,396 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 2023

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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
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.