Export DataGridView Data with Images into Excel, HTML, or Word with Proper Table Formatting

自闭症网瘾萝莉.ら 提交于 2019-12-24 04:14:06

问题


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.


回答1:


If you need to place image into cell try this code:

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



回答2:


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.



来源:https://stackoverflow.com/questions/8312257/export-datagridview-data-with-images-into-excel-html-or-word-with-proper-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!