0

我想将gridview导出到excel文件,但问题是我在gridview中有图像,它存储在数据库表中..顺便说一句,我存储整个图像而不是图像的路径,我也想导出图像。 .这是代码,但是当我运行它时它会给出异常..请帮助..谢谢

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

这是excel表格的图片,但图片栏中的图片没有显示,但文本“System.drawing.bitmap” 电子表格

4

0 回答 0