2
Answers

How to Export gridview which contain images column to excel

Ask a question
Umair Kasmani

Umair Kasmani

11y
2.9k
1
i want to export gridview to excel file but the problem is that i have images in gridview which is stored in database table..by the way i storing the whole images not the path of the image and i want to export the images as well..here is the code but when i run this it export the other data but not the images .. please help.. Thank You


here is my code

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 (int h = 1; h < grid.Columns.Count + 1; h++)
                    {
                        xlWorkSheet.Cells[1, h] = grid.Columns[h - 1].HeaderText;
                    }
                        for (i = 0; i <= grid.RowCount - 1; i++)
                        {
                            for (j = 0; j <= grid.ColumnCount - 1; j++)
                            {

                                DataGridViewCell cell = grid[j, i];
                                xlWorkSheet.Cells.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
                                xlWorkSheet.Columns.AutoFit();
                                xlWorkSheet.Cells[i + 2, j + 1] = cell.FormatedValue;


                            }
                        }

                    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");
            }
        }




Answers (2)