Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Problem

Export datagridview to excel data exported without Header text of column .

Detais

when export datagridview to excel it export success but all data exported without header

so How to solve this problem .

C#
private void releaseObject(object obj)
        {
            try
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
                obj = null;
            }
            catch (Exception ex)
            {
                obj = null;
                MessageBox.Show("Exception Occured while releasing object " + ex.ToString());
            }
            finally
            {
                GC.Collect();
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {

            Excel.Application xlApp;
            Excel.Workbook xlWorkBook;
            Excel.Worksheet xlWorkSheet;
            object misValue = System.Reflection.Missing.Value;

            xlApp = new Excel.Application();
            xlWorkBook = xlApp.Workbooks.Add(misValue);
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
            int i = 0;
            int j = 0;
            for ( i = 1; i < ScfGrid.Columns.Count + 1; i++)
            {
                if (ScfGrid.Columns[i - 1].Visible)
                {
                    xlWorkSheet.Cells[1, i] = ScfGrid.Columns[i - 1].HeaderText;
                }
            }
            for (i = 0; i <= ScfGrid.RowCount - 1; i++)
            {
                for (j = 0; j <= ScfGrid.ColumnCount - 1; j++)
                {
                    DataGridViewCell cell = ScfGrid[j, i];
                    xlWorkSheet.Cells[i + 1, j + 1] = cell.Value;
                }
            }

            xlWorkBook.SaveAs("D:\\ahmed.xls", 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 c:\\csharp.net-informations.xls");
        

        }


What I have tried:

when export datagridview to excel data exported without Header text of column
Posted
Updated 29-Jan-18 17:40pm

you are over writing the first row in the second loop, thats why the header is not available
try this
for (i = 0; i <= ScfGrid.RowCount - 1; i++)
          {
              for (j = 0; j <= ScfGrid.ColumnCount - 1; j++)
              {
                  DataGridViewCell cell = ScfGrid[j, i];
                  xlWorkSheet.Cells[i + 2, j + 1] = cell.Value;
              }
          }
 
Share this answer
 
Have you tried this?

protected void ExportToExcel(DataTable dataTable, bool exportMode, string fileName)
        {
            StringWriter stringWrite = new StringWriter();
            HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
            GridView gv = new GridView();
            gv.ShowHeader = false; //Removes Header
            gv.DataSource = dataTable;
            gv.DataBind();

            for (int I = 0; I < dataTable.Rows.Count; I++)
            {
                for (int iCol = 0; iCol < dataTable.Columns.Count; iCol++)
                {
                    if (dataTable.Rows[I][iCol].ToString().Length < 256)
                    {
                        gv.Rows[I].Cells[Convert.ToInt32(iCol)].Attributes.Add("class", "text");
                    }
                }
            }

            dataTable = null;

            if (gv.Rows.Count == 0)
            {
                gv.ShowHeaderWhenEmpty = true;
                gv.EmptyDataText = "No Data !"
            }
            else
            {
                Response.Clear();
                Response.Charset = "utf-8";
                Response.ContentType = "application/vnd.ms-excel";

                if (exportMode)
                {
                    Response.AddHeader("Content-Disposition", "attachment;filename=\"" + fileName.Replace(" ", "_") + "\"");
                }

                gv.RenderControl(htmlWrite);

                Response.Write(stringWrite.ToString());
                Response.End();
            }

            htmlWrite.Dispose();
            stringWrite.Dispose();
            gv.Dispose();
        }


Hope that helps
 
Share this answer
 
Comments
ahmed_sa 29-Jan-18 18:26pm    
sorry i use windows form not web form
can you please modify function above for windows form if possible

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900