zoukankan      html  css  js  c++  java
  • .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=" 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>
    &nbsp;
    客户名称:<asp:TextBox ID="TextBox1" runat="server" BorderWidth="1px" ></asp:TextBox>&nbsp;入库类型:&nbsp;<asp:TextBox
    ID="TextBox5" runat="server"></asp:TextBox>&nbsp;产品名称:<asp:TextBox ID="TextBox6"
    runat="server"></asp:TextBox>入库日期:从&nbsp; <asp:TextBox ID="TextBox2"
    runat="server" BorderWidth="1px" onClick="WdatePicker({dateFmt:'yyyy-MM-dd'})"></asp:TextBox>
    &nbsp;到&nbsp; <asp:TextBox ID="TextBox3" runat="server" BorderWidth="1px" onClick="WdatePicker({dateFmt:'yyyy-MM-dd'})"></asp:TextBox>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <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="100%;height:30px;"></div>

    </div>
    <div style="margin-bottom:20px;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="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.
    }
    }

  • 相关阅读:
    基于角色的权限控制
    C#中实现拖动无边框窗体Form
    C#加密方法汇总
    监控 SQL Server (2005/2008) 的运行状况来自微软TetchNet
    经典SQL语句集锦【转】
    Asp.net+Xml+js实现无线级下拉菜单
    CuteEditor5.0的安装及它与Ajax.net配合无刷新操作数据库!
    sql 提取数字、字母、汉字
    c# 判断远程文件是否存在
    c#采集网页用得几个函数
  • 原文地址:https://www.cnblogs.com/weiyu11/p/7568743.html
Copyright © 2011-2022 走看看