asp.net数据导出到word

时间:2021-07-24 06:21:37

我觉得这个文章不能算我的原创,因为代码也是我从别的地方找到的,而且我在网上查了,基本上都是这套代码。

 

通用的把数据倒进word或者excel。这里的导入excel和我之前的那篇文章中的方法是不一样的。

 

代码如下:

 

aspx页面中:

 

<form id="form1" runat="server">
         <div>
            <table align="center" cellpadding="0" cellspacing="0" border="0" runat="server" id="table1">
                <tr>
                    <td align="center">

                       <!-- 其实这俩个Button是一样的 ->
                        <asp:Button ID="cmdOpen" runat="server" Text="在线打开" CommandName="open" OnCommand="Button_Click"  />&nbsp;
                        <asp:Button ID="cmdSave" runat="server" Text="本地保存" CommandName="save" OnCommand="Button_Click" />
                        <asp:DropDownList ID="listType" runat="server" >
                            <asp:ListItem Value="excel">Excel</asp:ListItem>
                            <asp:ListItem Value="word">Word</asp:ListItem>
                        </asp:DropDownList><br />
                        <br />
                        数据源:<br />
                        <br />
                        <asp:GridView ID="myGW" runat="server" >
                        </asp:GridView>
                    </td>
                </tr>
            </table>
        </div>
    </form>

 

aspx.cs中:

 

protected void Page_Load(object sender, EventArgs e)
    {
        FillGridView();
    }
    private void OutPut(string fileType, string strType)
    {
        Response.Clear();
        Response.Buffer = true;
        Response.Charset = "GB2312";
        Response.AppendHeader("Content-Disposition", fileType);
        Response.ContentType = strType;
        this.EnableViewState = false;
        System.IO.StringWriter swOut = new System.IO.StringWriter();
        HtmlTextWriter hTw = new HtmlTextWriter(swOut);
        myGW.RenderControl(hTw); 

//这里很关键,这是将GridView中的所有东西导入到Word或者Excel中。这里也可以写成table1.RenderControl(hTw); 

这样table中的所以值都会导入到word中。
        Response.Write(swOut.ToString());
        Response.End();
    }
    public override void VerifyRenderingInServerForm(Control control)//我也不知道这个方法是干嘛的,但是这个从写的方法必须有否则会报错的。
    {
    }
    protected void Button_Click(object sender, CommandEventArgs e)
    {
        switch (e.CommandName)
        {
            case "save":
                switch (listType.SelectedValue)
                {
                    case "excel":
                        OutPut("attachment;filename=out.xls", "application/ms-excel");
                        break;
                    case "word":
                        OutPut("attachment;filename=out.doc", "application/ms-word");
                        break;
                }
                break;
            case "open":
                switch (listType.SelectedValue)
                {
                       case "excel":
                        OutPut("online;filename=out.xls", "application/ms-excel");
                        break;
                        case "word":
                        OutPut("online;filename=out.doc", "application/ms-word");
                        break;
                }
                break;
        }
    }
    private void FillGridView()//这里是你的数据,这个方法也可以没有的,这里是将GridView中的数据导出,也可以将整个页面导出。
    {
        Common.Data.IDataProvider myProvider = Common.Frame.DataOper.GetSystemDataProvider();
        DataTable dt = myProvider.GetData("select * from gongjiao_peixun_time");
        this.myGW.DataSource = dt.DefaultView;
        this.myGW.DataBind();
    }