Saturday, May 2, 2015

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

Interop.Excel version : 12.0

Excel Version using on my system : 2007

code which I've written for exporting Images to Excel..

for (i = 0; i <= dataGridView1.RowCount - 1; i++)
                             {
                                 for (j = 0; j <= dataGridView1.ColumnCount - 1; j++)
                                 {
                                     DataGridViewCell cell = dataGridView1[j, i];
                                     xlWorkSheet.Cells.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
                                     xlWorkSheet.Columns.AutoFit();
              
                                     if (cell.Value.GetType() == typeof(Bitmap))
                                     {
                                           
                                            Bitmap image =(Bitmap)cell.Value;
                                            image.Save(@"C:\btnvehpic.bmp");
                                             Excel.Range oRange = (Excel.Range)xlWorkSheet.Cells[i + 1, j + 1];
                                             float Left = (float)((double)oRange.Left);
                                             float Top = (float)((double)oRange.Top);
                                             const float ImageSize = 32;
                                             xlWorkSheet.Shapes.AddPicture(@"C:\btnvehpic.bmp", Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue, Left, Top, ImageSize, ImageSize);
                                             oRange.RowHeight = ImageSize + 2;
                                             File.Delete(@"C:\btnvehpic.bmp");
                                     }
                                     else
                                     {
                                         xlWorkSheet.Cells[i + 1, j + 1] = cell.Value;
                                     }
                                     
                                 }
                             }

Actual Datagridview snap:


Thanks & Regards RAJENDRAN M

No comments:

Post a Comment