将带有图像的DataGridView数据导出到带有正确表格格式的Excel,HTML或Word中

我有一个数据网格视图与图像加载到它。 作为这个数据网格源的表具有图像的path,并使用该path加载图像。 我试图导出数据到Excel,并成功,但它似乎显示图像的一些问题。 请帮忙吗? 任何帮助,而不是Excel,HTML或Word或任何东西都可以,但请提供详细的帮助,否则会导致很多问题。

这是我用来导出到Excel的代码:

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)) { xlWorkSheet.Cells[i + 1, j + 1] = ReadFile((Bitmap)cell.Value); } 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"); } } 

表格格式是:

ax_no rm_no fullNametypes照片文件的path。 指纹//文件的path。

都是string。 我也尝试使用Spire.dataExport和iTextSharp,但它不起作用。

如果您需要将图像放入单元格,请尝试以下代码:

 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; xlWorkSheet1.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; } 

看看这篇文章通过使用Reporting Services报表生成将DataGridView导出到Excel / PDF /图像文件 。

另外检查出Infragistics Winforms控件包 。 它具有非常可定制的UltraWinGrid来可视化您的表格数据和强大的Excel库,不需要Excell安装使用。

最后看看Stimulsoft Reports:它有很多导出格式,不需要Excel应用程序。