我有一个数据网格视图,其中加载了图像。作为此数据网格源的表具有图像路径,我使用该路径加载图像。我试图将数据导出到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 全名 类型 照片//文件的路径。 指纹//文件路径。
所有都是字符串。我也尝试过使用Spire.dataExport和iTextSharp,但它不起作用。
答案 0 :(得分:3)
如果您需要将图片放入单元格,请尝试以下代码:
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;
}
答案 1 :(得分:0)
看一下这篇文章Exporting a DataGridView to an Excel/PDF/image file by using Reporting Services report generation。
同时查看Infragistics Winforms Controls Bundle。它具有可定制的UltraWinGrid,可以显示您的表数据和功能强大的Excel库,不需要安装Excell即可使用。
最后看看Stimulsoft Reports:它有很多导出格式,也不需要Excel应用程序。