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
%>