asp.net datagrid export Excel two Ways

Source: Internet
Author: User

ASP tutorial. NET DataGrid export Excel two Ways
Article for you to provide two of DataGrid export Excel code is to use vb.net export Excel one is to use C # DataGrid to export the instance Code of Excel.
*/
C#:

private void exportToExcel ()
        {
             SaveFileDialog SaveFileDialog = new SaveFileDialog ();
            savefiledialog.filter = "execl files (*.xls) |*.xls ";
            savefiledialog.filterindex = 0;
            savefiledialog.restoredirectory = true;
            savefiledialog.createprompt = true;
            savefiledialog.title = "Save as Excel file";
            Savefiledialog.showdialog ();

if (Savefiledialog.filename.indexof (":") < 0) return; It was called "Cancel."

Stream MyStream;
MyStream = Savefiledialog.openfile ();
StreamWriter sw = new StreamWriter (MyStream, System.text.encoding.getencoding (-0));
String columntitle = "";
Try
{
Write column headers
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);

Writing column Contents
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 Excel File"
Savefiledialog.showdialog ()

If Savefiledialog.filename.indexof (":") < 0 Then
Return
End If
' was ordered to cancel. '
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 contents
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

%>

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.