StreamWriter (DataGridView) Export Excel

Source: Internet
Author: User

  Public void writeexcel (Datagridview ds, string path)          {            try             {                 SaveFileDialog saveFileDialog = new  SaveFileDialog ();                 saveFileDialog.Filter =  "execl files  (*.xls) |*.xls";                  savefiledialog.filterindex =  0;                 savefiledialog.restoredirectory = true;                 savefiledialog.createprompt = false;                 saveFileDialog.Title =  "Export Excel file to";                 saveFileDialog.FileName =  Path;                 if   (Savefiledialog.showdialog ()  == dialogresult.ok)                  {                      Stream myStream;                      Mystream = savefiledialog.openfile ();                &nbsP;     streamwriter sw = new streamwriter (myStream,  Encoding.GetEncoding ("gb2312"));                     stringbuilder sb = new stringbuilder ();                      messagebox.show (ds. Rows.count+ "," +ds. ColumnCount);                     for  (Int i = 0; i < ds. columncount; i++)                      {             &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SB. Append (ds. Columns[i]. Headertext +&nBSP; " \ t ");                      }               &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SB. Append (Environment.NewLine);//Line wrapping after each line of data                      //Write Content                      for  (int j = 0;  j < ds. rows.count; j++)                      {                         for  (int k = 0;  k < ds. columncount; k++) &NBSP;&NBSP;&NBSP;&NBSP;&Nbsp;                    {                 &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SB. Append (ds. ROWS[J]. CELLS[K]. Value.tostring ()  +  "\ T");                         }                         sb . Append (Environment.NewLine);//Line wrapping after each line of data                       }         &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SW. Write (sb.) ToString ());       &nbsP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SW. Flush ();                  &NBSP;&NBSP;&NBSP;SW. Close ();//Release Resources                      killspecialexcel ();                     messagebox.show ("The specified Excel file has been generated!");                 }             }             catch  (Exception ex)              {                 messagebox.show (ex. Message);             }         }

StreamWriter (DataGridView) Export Excel

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.