Front Code
<%@ page language= "C #" autoeventwireup= "true" codefile= "BTBW_rk.aspx.cs" inherits= "Lxl_hxyrk_btbw_rk"%>
<%@ Register assembly= "Microsoft.ReportViewer.WebForms, version=10.0.0.0, Culture=neutral, publickeytoken= B03F5F7F11D50A3A "
Namespace= "Microsoft.Reporting.WebForms" tagprefix= "Rsweb"%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<title> end of board head and tail record </title>
<link href= ". /css/myc.css "rel=" stylesheet "type=" Text/css "/>
<script src= ". /css/publicfun.js "type=" Text/javascript "></script>
<body style= "background: #E2E5E6;" >
<form id= "Form1" runat= "Server" >
<div style= "background:white;margin-left:5px;margin-right:5px;margin-top:5px;border:1px solid black;" >
<div style= "color: #000; font-size:14px;" >
<div>
<span id= "Zy" style= "Width:auto; margin-left:40%; height:100px; Text-align:center;
Color: #000; Font-weight:bold; font-size:20px; line-height:50px; " > Head Board tail storage record
</span>
</div>
<div style= "margin-bottom:10px;" >
<span> Welcome to <asp:label id= "Label1" runat= "Server" text= "custody" ></asp:Label> arrival!</span>
</div>
<div>
Inbound Number: <asp:textbox
Id= "TextBox4" runat= "Server" borderwidth= "1px" ></asp:TextBox>
Customer Name: <asp:textbox id= "TextBox1" runat= "Server" borderwidth= "1px" ></asp:TextBox> storage type:& Nbsp;<asp:textbox
Id= "TEXTBOX5" runat= "server" ></asp:TextBox> Product Name: <asp:textbox id= "TextBox6"
runat= "Server" ></asp:TextBox> Inbound Date: from <asp:textbox id= "TextBox2"
runat= "Server" borderwidth= "1px" onclick= "Wdatepicker ({datefmt: ' Yyyy-mm-dd '})" ></asp:TextBox>
to <asp:textbox id= "TextBox3" runat= "Server" borderwidth= "1px" onclick= "Wdatepicker ({datefmt: ' Yyyy-mm-dd '})" > </asp:TextBox>
<asp:button
Id= "Button1" runat= "Server" cssclass= "Bigbutton"
text= "Query"/>
<asp:button id= "Button2" runat= "Server" borderstyle= "Groove"
text= "Export Excel" cssclass= "Bigbutton"/>
<asp:button id= "Button3" runat= "Server" borderstyle= "Groove"
text= "Export word" cssclass= "Bigbutton"/>
</div>
<div style= "width:100%;height:30px;" ></div>
</div>
<div style= "margin-bottom:20px;width:99%;margin-left:0.5%;" >
<asp:panel id= "Panel1" runat= "Server" >
<asp:gridview id= "GridView1" runat= "Server" autogeneratecolumns= "False"
width= "100%" cssclass= "Tableblock" bordercolor= "Black" emptydatatext= "no record" >
<Columns>
<asp:boundfield datafield= "rk_id" headertext= "Inbound number" sortexpression= "rk_id"/>
<asp:boundfield datafield= "Customer name" headertext= "Customer Name" sortexpression= "Customer Name"/>
<asp:boundfield datafield= "workshop" headertext= "workshop "
sortexpression= "Workshop"/>
<asp:boundfield datafield= "Production date" headertext= "Production date" sortexpression= "Production date" dataformatstring= "{0:YYYY-MM-DD}"/ >
<asp:boundfield datafield= "Product name" headertext= "Product name "
sortexpression= "Product name"/>
<asp:boundfield datafield= "unit of measure" headertext= "Unit of Measure "
sortexpression= "Unit of measure"/>
<asp:boundfield datafield= "Quantity" headertext= "Quantity "
sortexpression= "Quantity"/>
<asp:boundfield datafield= "Inbound type" headertext= "Inbound type "
sortexpression= "Inbound type"/>
<asp:boundfield datafield= "Inbound date" headertext= "Inbound date "
sortexpression= "Inbound Date" dataformatstring= "{0:yyyy-mm-dd}"/>
<asp:boundfield datafield= "System single Date" headertext= "System single Date" sortexpression= "System single date"/>
<asp:boundfield datafield= "Remarks" headertext= "remarks" sortexpression= "Remarks"/>
</Columns>
<rowstyle font-size= "9pt" horizontalalign= "Center" verticalalign= "Middle"
bordercolor= "Black"/>
</asp:GridView>
</asp:Panel>
<div style= "width:100%;height:20px;" ></div>
</div>
</div>
</form>
</body>
Background code:
Using System;
Using System.Collections.Generic;
Using System.Web;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Data.SqlClient;
public partial class Lxl_HXYRK_BTBW_rk:System.Web.UI.Page
{
protected void Page_Load (object sender, EventArgs e)
{
if (! IsPostBack)
{
TextBox2.Text = DateTime.Now.AddDays (-1). ToString ("Yyyy-mm-dd");
TextBox3.Text = DateTime.Now.ToString ("Yyyy-mm-dd");
String sql = "SELECT * from view_btbwrk where inbound date >= '" + DateTime.Now.AddDays (-1). Date + "' and <= '" "+datetime.now.date+" ";
String sql = "SELECT * from View_btbwrk";
Gridview1.datasource = new View_btbwrkdata (). Select (SQL);
Gridview1.databind ();
}
Button2.click + = new EventHandler (button2_click);
Button3.click + = new EventHandler (button3_click);
}
//Export word
void Button3_Click (object sender, EventArgs e)
{
This . Exportcontrol (this. GridView1, "Word", "Acer Xinyuan" + System.DateTime.Now);
}
//Export Excel table
void Button2_Click (object sender, EventArgs e)
{
This . Exportcontrol (this. GridView1, "Excel", "Hongyuan" + System.DateTime.Now);
}
//Export method
Public void Exportcontrol (System.Web.UI.Control source, string DocumentType, string filename)
{
//Set HTTP header information, encoding format
if (DocumentType = = "Excel")
{
//Prevent garbled, plus this line can prevent garbled data when there is only one row of data export to Excel/word <wbr> prevent garbled characters
HttpContext.Current.Response.Write ("<meta http-equiv=content-type content=text/html;charset=utf-8>");
//excel
HttpContext.Current.Response.AppendHeader ("Content-disposition", "attachment;filename=" + Httputility.urlencode (filename + ". xls", System.Text.Encoding.UTF8));
HttpContext.Current.Response.ContentType = "Application/ms-excel";
}
else if (DocumentType = = "Word")
{
//Prevent garbled, plus this line can prevent garbled data when there is only one row of data export to Excel/word <wbr> prevent garbled characters
HttpContext.Current.Response.Write ("<meta http-equiv=content-type content=text/html;charset=utf-8>");
//word
HttpContext.Current.Response.AppendHeader ("Content-disposition", "attachment;filename=" + Httputility.urlencode (filename + ". Doc", System.Text.Encoding.UTF8));
HttpContext.Current.Response.ContentType = "Application/ms-word";
}
HttpContext.Current.Response.Charset = "UTF-8";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
Turn off view state for a control
Source. Page.enableviewstate = false;
Initialize HTMLWriter
System.IO.StringWriter writer = new System.IO.StringWriter ();
System.Web.UI.HtmlTextWriter HTMLWriter = new System.Web.UI.HtmlTextWriter (writer);
Source. RenderControl (HTMLWriter);
Output
HttpContext.Current.Response.Write (writer. ToString ());
HttpContext.Current.Response.End ();
}
Overload the Verifyrenderinginserverform method, or run with the following error message: "Type" GridView "control" GridView1 "must be placed in the form tag with Runat=server"
public override void Verifyrenderinginserverform (Control control)
{
Override Verifyrenderinginserverform.
}
}
. NET background export Excel, Word