Asp.net no component export Excel

Source: Internet
Author: User

Recently, I want to create a data export function. I found it online, but I didn't find it. Sometimes I want to export PDF files like excel. dll. It seems that I need third-party DLL support.

Below is the source code I exported for excel. Which of the following is a good solution? I want to introduce the data in the source code to my simulation.

 

 Using System;
Using System. collections;
Using System. configuration;
Using System. Data;
Using System. LINQ;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. htmlcontrols;
Using System. Web. UI. webcontrols;
Using System. Web. UI. webcontrols. webparts;
Using System. xml. LINQ;
Using System. IO;

Public Partial Class Reportexel: system. Web. UI. Page
{
Protected Void Page_load ( Object Sender, eventargs E)
{

}
Protected Void Button#click ( Object Sender, eventargs E)
{
Dataset DS = New Dataset ();
Datatable dt = New Datatable ();
Datacolumn coll = DT. Columns. Add ( " Title " , Typeof ( String ));
Coll. allowdbnull = False ;
Coll. Unique = True ; Datarow Dr;

For ( Int I = 0 ; I < 10 ; I ++)
{
Dr = DT. newrow ();// New Line
Dr [ " Title " ] = " The " + I + " Column data " ;
DT. Rows. Add (DR );
}

DS. Tables. Add (DT );


Exportdatatoexcel (DT, " Continued personnel agent " );
// Createexcel (DS, "aa.xls ");
}





/// <Summary>
///
/// </Summary>
/// <Param name = "DT"> </param>
/// <Param name = "strfilename"> Include .xls </Param>
Public Static Void Exportdatatoexcel (datatable DT, String Filename)
{
Try
{
Stringwriter Sw = New Stringwriter ();
String Colstr = "" ;
Foreach (Datacolumn col In DT. columns)
{
Colstr + = col. columnname + " \ T " ;
}
Sw. writeline (colstr );

Foreach (Datarow row In DT. Rows)
{
Colstr = "" ;
Foreach (Datacolumn col In DT. columns)
{
Colstr + = row [col. columnname]. tostring () + " \ T " ;
}
Sw. writeline (colstr );
}
Sw. Close ();
Httpcontext. Current. response. appendheader ( " Content-Disposition " , " Attachment; filename = " + Httputility. urlencode (filename + " . Xls " , System. Text. encoding. utf8 ));
Httpcontext. Current. response. contenttype = " Application/MS-Excel " ;
System. Web. httpcontext. Current. response. contentencoding = system. Text. encoding. default;
System. Web. httpcontext. Current. response. Write (SW );
System. Web. httpcontext. Current. response. End ();
}
Catch (Exception ex)
{
Throw Ex;
}
}




Public Static Void Exportdatatoexcelbyweb (datatable DT, system. Web. UI. webcontrols. DataGrid dgoutput, String Filename)
{
Try
{
Dgoutput. Visible = True ;
Dgoutput. datasource = DT;
Dgoutput. databind ();
System. Web. httpcontext. Current. response. Clear ();
System. Web. httpcontext. Current. response. Buffer = True ;
System. Web. httpcontext. Current. response. charset = " Gb2312 " ;
System. Web. httpcontext. Current. response. appendheader ( " Content-Disposition " , " Attachment; filename = " + Filename + " . Xls " );
System. Web. httpcontext. Current. response. contentencoding = system. Text. encoding. getencoding ( " Gb2312 " ); // Set the output stream to simplified Chinese
System. Web. httpcontext. Current. response. contenttype = " Application/MS-Excel " ; // Set the output file type to an Excel file.
Dgoutput. enableviewstate =False ;
System. Globalization. cultureinfo mycitrad = New System. Globalization. cultureinfo ( " ZH-CN " , True );
System. Io. stringwriter ostringwriter = New System. Io. stringwriter (mycitrad );
System. Web. UI. htmltextwriter ohtmltextwriter = New System. Web. UI. htmltextwriter (ostringwriter );
Dgoutput. rendercontrol (ohtmltextwriter );
System. Web. httpcontext. Current. response. Write (ostringwriter. tostring ());
System. Web. httpcontext. Current. response. End ();
Dgoutput. Visible = False ;
}
Catch (Exception ex)
{
Throw Ex;
}
}





}

 

 

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.