.net 後臺導出excel ,word
前臺代碼
<%@ 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">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>板頭板尾入庫記錄</title>
<link href="../css/myc.css" rel="stylesheet" type="text/css" />
<script src="../css/Publicfun.js" type="text/javascript"></script>
</head>
<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>
<img src="../images/top.jpg" />
<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;">板頭板尾入庫記錄
</span>
</div>
<div style="margin-bottom:10px;">
<span>歡迎<asp:Label ID="Label1" runat="server" Text="保管"></asp:Label>的到來!</span>
</div>
<div>
入庫單號:<asp:TextBox
ID="TextBox4" runat="server" BorderWidth="1px"></asp:TextBox>
客戶名稱:<asp:TextBox ID="TextBox1" runat="server" BorderWidth="1px" ></asp:TextBox> 入庫類型: <asp:TextBox
ID="TextBox5" runat="server"></asp:TextBox> 產品名稱:<asp:TextBox ID="TextBox6"
runat="server"></asp:TextBox>入庫日期:從 <asp:TextBox ID="TextBox2"
runat="server" BorderWidth="1px" onClick="WdatePicker({dateFmt:‘yyyy-MM-dd‘})"></asp:TextBox>
到 <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="查詢" />
<asp:Button ID="Button2" runat="server" BorderStyle="Groove"
Text="導出excel" CssClass="BigButton" />
<asp:Button ID="Button3" runat="server" BorderStyle="Groove"
Text="導出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="暫無記錄">
<Columns>
<asp:BoundField DataField="rk_id" HeaderText="入庫編號" SortExpression="rk_id" />
<asp:BoundField DataField="客戶名稱" HeaderText="客戶名稱" SortExpression="客戶名稱" />
<asp:BoundField DataField="車間" HeaderText="車間"
SortExpression="車間" />
<asp:BoundField DataField="生產日期" HeaderText="生產日期" SortExpression="生產日期" DataFormatString="{0:yyyy-MM-dd}" />
<asp:BoundField DataField="產品名稱" HeaderText="產品名稱"
SortExpression="產品名稱" />
<asp:BoundField DataField="計量單位" HeaderText="計量單位"
SortExpression="計量單位" />
<asp:BoundField DataField="數量" HeaderText="數量"
SortExpression="數量" />
<asp:BoundField DataField="入庫類型" HeaderText="入庫類型"
SortExpression="入庫類型" />
<asp:BoundField DataField="入庫日期" HeaderText="入庫日期"
SortExpression="入庫日期" DataFormatString="{0:yyyy-MM-dd}" />
<asp:BoundField DataField="制單日期" HeaderText="制單日期" SortExpression="制單日期" />
<asp:BoundField DataField="備註" HeaderText="備註" SortExpression="備註" />
</Columns>
<HeaderStyle Font-Size="9pt" CssClass="TableHeader"/>
<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>
</html>
後臺代碼:
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 入庫日期>= ‘" + 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);
}
//導出word
void Button3_Click(object sender, EventArgs e)
{
this.ExportControl(this.GridView1, "Word", "宏鑫源" + System.DateTime.Now);
}
//導出excel表
void Button2_Click(object sender, EventArgs e)
{
this.ExportControl(this.GridView1, "Excel", "宏鑫源" + System.DateTime.Now);
}
//導出方法
public void ExportControl(System.Web.UI.Control source, string DocumentType, string filename)
{
//設置Http的頭信息,編碼格式
if (DocumentType == "Excel")
{
//防止出現亂碼,加上這行可以防止在只有一行數據時出現亂碼Gridview數據導出到Excel/Word <wbr>防止出現亂碼
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")
{
//防止出現亂碼,加上這行可以防止在只有一行數據時出現亂碼Gridview數據導出到Excel/Word <wbr>防止出現亂碼
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;
//關閉控件的視圖狀態
source.Page.EnableViewState = false;
//初始化HtmlWriter
System.IO.StringWriter writer = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWriter = new System.Web.UI.HtmlTextWriter(writer);
source.RenderControl(htmlWriter);
//輸出
HttpContext.Current.Response.Write(writer.ToString());
HttpContext.Current.Response.End();
}
//重載VerifyRenderingInServerForm方法,否則運行的時候會出現如下錯誤提示:“類型“GridView”的控件“GridView1”必須放在具有 runat=server 的窗體標記內”
public override void VerifyRenderingInServerForm(Control control)
{
//override VerifyRenderingInServerForm.
}
}
.net 後臺導出excel ,word