Exporting Datagridview to excel using c#.net

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • leelaprasad9999
    New Member
    • Dec 2012
    • 5

    Exporting Datagridview to excel using c#.net

    hi i'm trying to export datagridview data with image to excel , i'm using the below code, but image cannot be displayed.


    Code:
    Microsoft.Office.Interop.Excel.Application ExcelApp = new Microsoft.Office.Interop.Excel.Application();
                Microsoft.Office.Interop.Excel._Workbook ExcelBook;
                Microsoft.Office.Interop.Excel._Worksheet ExcelSheet;
                int i = 0;
                int j = 0;
                //create object of excel
                ExcelBook = (Microsoft.Office.Interop.Excel._Workbook)ExcelApp.Workbooks.Add(1);
                ExcelSheet = (Microsoft.Office.Interop.Excel._Worksheet)ExcelBook.ActiveSheet;
                //export header
                for (i = 1; i <= this.dataGridView1.Columns.Count; i++)
                {
                    ExcelSheet.Cells[1, i] = this.dataGridView1.Columns[i - 1].HeaderText;
                }
                //export data
                for (i = 1; i <= this.dataGridView1.RowCount; i++)
                {
                    for (j = 1; j <= dataGridView1.Columns.Count; j++)
                    {
                        ExcelSheet.Cells[i + 1, j] = dataGridView1.Rows[i - 1].Cells[j - 1].Value;
                    }
                }
                ExcelApp.Visible = true;
                //set font Khmer OS System to data range
                Microsoft.Office.Interop.Excel.Range myRange = ExcelSheet.get_Range(ExcelSheet.Cells[1, 1], ExcelSheet.Cells[this.dataGridView1.RowCount + 1, this.dataGridView1.Columns.Count]);
                Microsoft.Office.Interop.Excel.Font x = myRange.Font;
                x.Name = "Arial";
                x.Size = 10;
                //set bold font to column header
                myRange = ExcelSheet.get_Range(ExcelSheet.Cells[1, 1], ExcelSheet.Cells[1, this.dataGridView1.Columns.Count]);
                x = myRange.Font;
                x.Bold = true;
                //autofit all columns
    
                myRange.EntireColumn.AutoFit();
                //
                ExcelSheet = null;
                ExcelBook = null;
                ExcelApp = null;
    pls help me .............
    Last edited by Rabbit; Dec 3 '12, 05:56 AM. Reason: Please use code tags when posting code.
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    I don't see where in your code you try to insert your image.

    Comment

    • leelaprasad9999
      New Member
      • Dec 2012
      • 5

      #3
      i have 5 columns in datagridview and 5th column is imagecolumn. already i have inserted image to datagridview. when i export datagridview to excel, data's are printing but image not displaying. this code is for export button_click..

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        I still don't see where you account for image data anywhere in your code. Image objects are different from text data, you have to treat it differently.

        Comment

        • leelaprasad9999
          New Member
          • Dec 2012
          • 5

          #5
          Code:
          private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
          {
           double shapes1== Convert.ToDouble(dataGridView1.CurrentRow.Cells[1].Value);
          
          if(shapes1==030)
          {
            Bitmap img2 = Properties.Resources.IT_030;
           dataGridView1.CurrentRow.Cells[5].Value = img2;
          }
          }
          i have combobox in column1 if combobox text is ie.,(030) respective image will display in imagecolumn from project resources. this is how i'm inserting imagecolumn value of datagridview.

          Comment

          • Rabbit
            Recognized Expert MVP
            • Jan 2007
            • 12517

            #6
            That's half the puzzle. You have to treat images differently to get images to show up in the datagrid. You also have to treat images differently to get them to show up in excel.

            Comment

            Working...