Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

Issue in Exporting Column Headers from DatagridView to Excel

$
0
0
Hey Guys,
 
I need your assistance, am struck up with an issue.
Issue: I have retrived a SQL data into a DataGridView, It has the below column header names like (Package_ID, Package_Name etc.)
 
I used the below code to copy the data in DataGridview to Excel.
Copying is fine, but am not getting the colum headers which are visible in my DatagridView. (Package_ID, Package_Name etc.)
 
Other than the Column headers all other data are getting copied.
 
the code i used:
 
     private void copyAlltoClipboard()
            {
                  dataGridView1.SelectAll();
                  DataObject dataObj = dataGridView1.GetClipboardContent();
                  if (dataObj != null)
                        Clipboard.SetDataObject(dataObj);
            }
 
            private void btn_Export_Click(object sender, EventArgs e)
            {
                  copyAlltoClipboard();
                  Microsoft.Office.Interop.Excel.Application xlexcel;
                  Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
                  Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
                  object misValue = System.Reflection.Missing.Value;
                  xlexcel = new Excel.Application();
                  xlexcel.Visible = true;
                  xlWorkBook = xlexcel.Workbooks.Add(misValue);
                  xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
                  Excel.Range CR = (Excel.Range)xlWorkSheet.Cells[1, 1];
                  CR.Select();
                  xlWorkSheet.PasteSpecial(CR, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, true);
            }
 

i want my column headers also need to be copied to Excel.
Please assist.

Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>