使用正确的表格格式将带有图像的 DataGridView 数据导出到 Excel、HTML 或 Word
我有一个数据网格视图,其中加载了图像。作为该数据网格源的表具有图像路径,我使用该路径加载图像。我尝试将数据导出到 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_号 全名 类型 照片//文件路径。 指纹//文件路径。
都是字符串。我也尝试过使用 Spire.dataExport 和 iTextSharp 但它不起作用。
I have a data grid view with images loaded into it. The table which is the source for this data grid has path for images and I load images using that path. I have tried to export data to Excel and was successful, but it seems to show some problems with images. Please any help? Any help instead of Excel, HTML, or Word or anything would do, but please provide detailed help or it causes a lot of problems.
Here is the code I used to export to 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");
}
}
Table format is:
ax_no
rm_no
fullName
Types
photograph // path of file.
Fingerprint // path of file.
All are strings. I have also tried using Spire.dataExport and iTextSharp but it doesn't work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您需要将图像放入单元格中,请尝试以下代码:
If you need to place image into cell try this code:
看看这篇文章 使用以下命令将 DataGridView 导出到 Excel/PDF/图像文件Reporting Services 报告生成。
另请查看 Infragistics Winforms 控件捆绑包。它具有高度可定制的 UltraWinGrid 来可视化您的表数据和强大的 Excel 库,无需安装 Excell 即可使用。
最后看看Stimulsoft Reports:它有很多导出格式,而且也不需要Excel应用程序。
Take a look at this article Exporting a DataGridView to an Excel/PDF/image file by using Reporting Services report generation.
Also check out Infragistics Winforms Controls Bundle. It has very customizable UltraWinGrid to visualize your table data and powerful Excel libraries that does not require Excell installed to use.
And finally take a look at Stimulsoft Reports: it has a lot export formats and does not require Excel application too.