Click here to Skip to main content
15,881,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
`saveFileDialog1.Filter = "Excel (*.xls)|*.xls";
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                if (!saveFileDialog1.FileName.Equals(String.Empty))
                {
                    FileInfo f = new FileInfo(saveFileDialog1.FileName);
                    if (f.Extension.Equals(".xls"))`
                    {  `Excel.Application xlApp;
                        Excel.Workbook xlWorkBook;
                        Excel.Worksheet xlWorkSheet;
                        object misValue = System.Reflection.Missing.Value;
                        
                      
                        xlApp = new Excel.ApplicationClass();
                        xlWorkBook = xlApp.Workbooks.Add(misValue);
                        xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
                        int i = 0;
                        int j = 0;

                        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))
                                {
                                    // You have to get original bitmap path here
                                    string imagString = "bitmap1.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(imagString, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue, Left, Top, ImageSize, ImageSize);
                                    oRange.RowHeight = ImageSize + 2;
                                }
                                else
                                {
                                    xlWorkSheet.Cells[i + 1, j + 1] = cell.Value;
                                }

                            }
                        }

                        xlWorkBook.SaveAs(saveFileDialog1.FileName, Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
                        xlWorkBook.Close(true, misValue, misValue);
                        xlApp.Quit();

                        releaseObject(xlWorkSheet);
                        releaseObject(xlWorkBook);
                        releaseObject(xlApp);

                        MessageBox.Show("Excel file created , you can find the file " + saveFileDialog1.FileName);
                    }
                    else
                    {
                        MessageBox.Show("Invalid file type");
                    }
                }
                else
                {
                    MessageBox.Show("You did pick a location " +
                                    "to save file to");
                }
            }
Posted
Updated 3-Jul-14 9:17am
v2

1 solution

See the solutions to this post Exception from HRESULT: 0x800A03EC Error[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900