Export Datagridview to excell in c#.net(WinForm)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nirmalsingh
    New Member
    • Sep 2006
    • 218

    Export Datagridview to excell in c#.net(WinForm)

    hai all,
    i need to export datagridview contents to a excell sheet including header.
    can anyone help me with sample code in c#.
    thanx in advance
    with Cheers
    Nirmal
  • nirmalsingh
    New Member
    • Sep 2006
    • 218

    #2
    Originally posted by nirmalsingh
    hai all,
    i need to export datagridview contents to a excell sheet including header.
    can anyone help me with sample code in c#.
    thanx in advance
    with Cheers
    Nirmal
    i myself found the solution, here is that.
    onclick of a button
    -----------------------------
    Code:
    dgPrefixes.SelectAll();//dgPrefixes is name of datagridview.
                    Clipboard.SetDataObject(dgPrefixes.GetClipboardContent(), true);
                    StreamWriter sw = new StreamWriter("d:\\test.xls");
                    sw.Write(Clipboard.GetText());
                    sw.Flush();
                    sw.Close();

    Comment

    • jbi
      New Member
      • Mar 2008
      • 1

      #3
      i TRIED YOUR CODE AND IT WORKED FINE..HOW CAN I ADD THE FIELD HEADINGS TO THE XLS FILE? APPRECIATE IT.
      Originally posted by nirmalsingh
      i myself found the solution, here is that.
      onclick of a button
      -----------------------------
      Code:
      dgPrefixes.SelectAll();//dgPrefixes is name of datagridview.
                      Clipboard.SetDataObject(dgPrefixes.GetClipboardContent(), true);
                      StreamWriter sw = new StreamWriter("d:\\test.xls");
                      sw.Write(Clipboard.GetText());
                      sw.Flush();
                      sw.Close();

      Comment

      Working...