Asp.net datagrid two ways to export excel

Source: Internet
Author: User

Asp.net datagrid two ways to export excel article provides you with two datagrid export excel Code one is to use vb.net export excel one is to use c # datagrid export excel instance code.

Asp tutorial. net datagrid export excel two methods
This article provides you with two pieces of excel Export Code: one is to use vb.net to export excel, and the other is to use c # datagrid to export excel instance code.
*/
// C #:

 

Private void exporttoexcel ()
{
Savefiledialog = new savefiledialog ();
Savefiledialog. filter = "execl files (*. xls) | *. xls ";
Savefiledialog. filterindex = 0;
Savefiledialog. restoredirectory = true;
Savefiledialog. createprompt = true;
Savefiledialog. title = "Save As an excel file ";
Savefiledialog. showdialog ();

If (savefiledialog. filename. indexof (":") <0) return; // It is clicked "cancel"

Stream mystream;
Mystream = savefiledialog. openfile ();
Streamwriter sw = new streamwriter (mystream, system. text. encoding. getencoding (-0 ));
String columntitle = "";
Try
{
// Write the column title
For (int I = 0; I <dgvgf_in_info.columncount; I ++)
{
If (I> 0)
{
Columntitle + = "t ";
}
Columntitle + = dgvgf_in_info.columns [I]. headertext;
}
Sw. writeline (columntitle );

// Write column content
For (int j = 0; j <dgvgf_in_info.rows.count; j ++)
{
String columnvalue = "";
For (int k = 0; k <dgvgf_in_info.columns.count; k ++)
{
If (k> 0)
{
Columnvalue + = "t ";
}
If (dgvgf_in_info.rows [j]. cells [k]. value = null)
Columnvalue + = "";
Else
Columnvalue + = dgvgf_in_info.rows [j]. cells [k]. value. tostring (). trim ();
}
Sw. writeline (columnvalue );
}
Sw. close ();
Mystream. close ();
}
Catch (exception e)
{
Messagebox. show (e. tostring ());
}
Finally
{
Sw. close ();
Mystream. close ();
}
}

// Vb.net

Private sub exporttoexcel ()
Dim savefiledialog as new savefiledialog ()
Savefiledialog. filter = "execl files (*. xls) | *. xls"
Savefiledialog. filterindex = 0
Savefiledialog. restoredirectory = true
Savefiledialog. createprompt = true
Savefiledialog. title = "Save As an excel file"
Savefiledialog. showdialog ()

If savefiledialog. filename. indexof (":") <0 then
Return
End if
'Canceled"
Dim mystream as stream
Mystream = savefiledialog. openfile ()
Dim sw as new streamwriter (mystream, system. text. encoding. getencoding (-0 ))
Dim columntitle as string = ""
Try
'Write column headers
For I as integer = 0 to dgvgf_in_info.columncount-1
If I> 0 then
Columntitle + = vbtab
End if
Columntitle + = dgvgf_in_info.columns (I). headertext
Next
Sw. writeline (columntitle)

'Write column content
For j as integer = 0 to dgvgf_in_info.rows.count-1
Dim columnvalue as string = ""
For k as integer = 0 to dgvgf_in_info.columns.count-1
If k> 0 then
Columnvalue + = vbtab
End if
If dgvgf_in_info.rows (j). cells (k). value is nothing then
Columnvalue + = ""
Else
Columnvalue + = dgvgf_in_info.rows (j). cells (k). value. tostring (). trim ()
End if
Next
Sw. writeline (columnvalue)
Next
Sw. close ()
Mystream. close ()
Catch e as exception
Messagebox. show (e. tostring ())
Finally
Sw. close ()
Mystream. close ()
End try
End sub

 

%>

Related Article

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.