ASP.net export Excel: Server Control GridView

Source: Internet
Author: User

Pre-release:

<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head id = "head1" runat = "server">
<Title> employee work logs table </title>
<Style type = "text/CSS" Media = "print">
. Noprint {display: none ;}
</Style>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div style = "margin-top: 10px; Border: 1px solid #000000;">
<Asp: gridview id = "gridview1" runat = "server" autogeneratecolumns = "true"
Width = "100%" emptydatatext = "no matching item! "Font-bold =" false "font-size =" 12px"
OnRowDataBound = "GridView1_RowDataBound"
OnRowCreated = "GridView1_RowCreated">

<AlternatingRowStyle BackColor = "WhiteSmoke"/>
<EditRowStyle BackColor = "# FFFF80"/>
<FooterStyle BackColor = "Snow"/>
<HeaderStyle BackColor = "Yellow"/>
</Asp: GridView>
<Asp: button id = "button1" runat = "server" onclick = "button#click" text = "Excel excel" cssclass = "noprint" style = "border: # b2b2b2 1px solid; line-Height: 150%; font-size: 12px; cursor: hand; Background-color: # efefef; "/>
</Div>
</Form>
</Body>
</Html>

 

Post-renewal:

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. Data. sqlclient;
Using System. Text;
Using System. IO;

Public partial class test: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{

# Region
/*
SqlCommand Cmd = new SqlCommand ("Query_AFU_FORM_YFGZ", conn );
Cmd. CommandType = CommandType. StoredProcedure;
// If (ActivityID = "")
// ActivityID = "1 ";
// SqlParameter sp = new SqlParameter ();
// Cmd. Parameters. Add (sp );
SqlDataReader odrAll = Cmd. ExecuteReader ();
GridView1.DataSource = odrAll;
GridView1.DataBind ();
OdrAll. Close ();


SqlCommand Cmd2 = new SqlCommand ("Query_AFU_FORM_YFGZ_UnChecked", conn );
Export 2.commandtype = CommandType. StoredProcedure;

SqlParameter sp = new SqlParameter ("@ Year", 2008 );
Optional 2.parameters. Add (SP );
Sqlparameter SP2 = new sqlparameter ("@ month", 10 );
Optional 2.parameters. Add (SP2 );

Sqldatareader odrall2 = pai2.executereader ();
Gridview2.datasource = odrall2;
Gridview2.databind ();
Odrall2.close ();
*/
# Endregion
String STR = "Server =.; database = Alice; uid = sa; Pwd = sa"; // define a response string;
SqlConnection sqlcon = new SqlConnection (str); // specify a SqlConnection object;
Sqlcon. Open (); // Open the data logging connector;
// Retrieve the data records in the table from the DataSet, and replicate them to the DataSet object;
SqlDataAdapter sqlda = new SqlDataAdapter ("select * from Supplies", sqlcon );
DataSet ds = new DataSet ();
Sqlda. Fill (ds );
// Upload the retrieved data from the dataset to the GridView;
GridView1.DataSource = ds;
GridView1.DataBind ();
// Related data records;
Sqlcon. Close ();
}

 

Protected void button#click (object sender, EventArgs e)
{
Export ("application/ms-excel", "FilledStatements.xls ");
}

Private void Export (string FileType, string FileName)
{
Response. Charset = "big5 ";
Response. ContentEncoding = System. Text. Encoding. UTF8;
Response. AppendHeader ("Content-Disposition", "attachment; filename =" + HttpUtility. UrlEncode (FileName, Encoding. UTF8). ToString ());
Response. ContentType = FileType;
This. EnableViewState = false;
StringWriter tw = new StringWriter ();
HtmlTextWriter hw = new HtmlTextWriter (tw );
GridView1.RenderControl (hw );
Response. Write (tw. ToString ());
Response. End ();
}

Public override void VerifyRenderingInServerForm (Control control Control)
{
}

ArrayList st = new ArrayList ();
ArrayList at = new ArrayList ();

Protected void GridView1_RowDataBound (object sender, GridViewRowEventArgs e)
{
// If (e. Row. RowType = DataControlRowType. DataRow)
//{
// For (int I = 4; I <9; I ++)
//{
// String date = e. Row. Cells [I]. Text;
// String date2 = "";
// String UniqueID = "";
// String applyID = e. Row. Cells [3]. Text;
// If (date! = "& Nbsp ;")
//{
// Date2 = date. Substring (date. IndexOf ("-") + 1 );
// UniqueID = date. Substring (0, date. IndexOf ("-"));
// E. Row. Cells [I]. Text = date2;
// E. Row. Cells [I]. Attributes. Add ("onclick", "javascript: location. href = 'alcor _ CheckDateInfo_Ajax.aspx? Date = "+ date2 +" & applyID = "+ applyID +" & UniqueID = "+ UniqueID + "'");
//}
//}
//}

// If (e. Row. RowType = DataControlRowType. Header)
//{
// String USER_ID = "";
// String a_nam = "";
// If (Request. Cookies ["USER_ID"]! = Null)
// {USER_ID = Request. Cookies ["USER_ID"]. Value ;}
// If (Request. Cookies ["_ nAutoWebCookie _" + USER_ID + "_"]! = Null)
//{
// If (Request. Cookies ["_ nAutoWebCookie _" + USER_ID + "_"] ["a_nam"]! = Null)
//{
// A_nam = Request. Cookies ["_ nAutoWebCookie _" + USER_ID + "_"] ["a_nam"]. ToString ();
//}
//}
// A_nam = HttpUtility. UrlDecode (a_nam.ToString (), System. Text. Encoding. GetEncoding ("big5 "));
// TableCellCollection tcHeader = e. Row. Cells;
// TcHeader. Clear ();

// TcHeader. Add (new TableHeaderCell ());
// TcHeader [0]. Attributes. Add ("colspan", Convert. ToString (st. Count ));
// TcHeader [0]. Text = "BU ";

// TcHeader. Add (new TableHeaderCell ());
// TcHeader [1]. Attributes. Add ("colspan", Convert. ToString (st. Count ));
// TcHeader [1]. Text = "DeptCode ";

// TcHeader. Add (new TableHeaderCell ());
// TcHeader [2]. Attributes. Add ("colspan", Convert. ToString (at. Count ));
// TcHeader [2]. Text = "EmployeeName ";

// TcHeader. Add (new TableHeaderCell ());
// TcHeader [3]. Attributes. Add ("colspan", Convert. ToString (at. Count ));
// TcHeader [3]. Text = "EmployeeNo ";

// TcHeader. Add (new TableHeaderCell ());
// TcHeader [4]. Attributes. Add ("colspan", Convert. ToString (at. Count ));
// TcHeader [4]. Text = "1 ";

// TcHeader. Add (new TableHeaderCell ());
// TcHeader [5]. Attributes. Add ("colspan", Convert. ToString (at. Count ));
// TcHeader [5]. Text = "2 ";

// TcHeader. Add (new TableHeaderCell ());
// TcHeader [6]. Attributes. Add ("colspan", Convert. ToString (at. Count ));
// TcHeader [6]. Text = "3 ";

// TcHeader. Add (new TableHeaderCell ());
// TcHeader [7]. Attributes. Add ("colspan", Convert. ToString (at. Count ));
// TcHeader [7]. Text = "4 ";

// TcHeader. Add (new TableHeaderCell ());
// TcHeader [8]. Attributes. Add ("colspan", Convert. ToString (at. Count ));
// TcHeader [8]. Text = "5 ";

// TcHeader. Add (new TableHeaderCell ());
// TcHeader [9]. Attributes. Add ("colspan", Convert. ToString (at. Count ));
// TcHeader [9]. Text = "TotalList ";
//}
}

Protected void GridView1_RowCreated (object sender, GridViewRowEventArgs e)
{

E. Row. HorizontalAlign = HorizontalAlign. Center;
/*
If (e. Row. RowType = DataControlRowType. Header)
{
St. Clear ();
At. Clear ();
For (int I = 0; I <e. Row. Cells. Count; I ++)
{

If (e. Row. Cells [I]. Text. Substring (e. Row. Cells [I]. Text. Length-1, 1) = "z ")
{
E. Row. Cells [I]. BackColor = System. Drawing. Color. GreenYellow;
St. Add (e. Row. Cells [I]. Text. Remove (e. Row. Cells [I]. Text. Length-1 ));
}
If (e. Row. Cells [I]. Text. Substring (e. Row. Cells [I]. Text. Length-1, 1) = "w ")
{
E. Row. Cells [I]. BackColor = System. Drawing. Color. GreenYellow;
At. Add (e. Row. Cells [I]. Text. Remove (e. Row. Cells [I]. Text. Length-1 ));
}
}
}
*/
}

}

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.