ASP.NET学习笔记(1)——如何导出GridView的数据到word

最近基于ASP.NET开发一个BS结构的信息系统,用到了很多高效的实现方式和工具,如报表生成,数据库操作类自动代码生成工具,网页模块化管理框架。以下谈谈用C#语言如何将网页中GridView中的数据导入Word表格中。与大家学习分享。

(1)首先,我们要为该网页中的GridView控件(3个)配置数据库表字段邦定:标记如下:
                    <asp:GridView ID="GV11" runat="server" SkinID="gridviewSkin" Width="100%" AutoGenerateColumns="False">
                        <Columns>
                            <asp:BoundField DataField="编号" HeaderText="编号" ReadOnly="True" />
                            <asp:BoundField DataField="位置" HeaderText="位置" ReadOnly="True" />
                            <asp:BoundField DataField="第几次" HeaderText="第几次" ReadOnly="True" />
                        </Columns>
                    </asp:GridView>
   //另外两个GridView控件的标记从略

(2)数据邦定到控件上
                #region 显示
                lbl1.Text = "基本信息";
                lbl2.Text = "项及其评分";
                lbl3.Text = "总评与结论";

                string sql = "select [编号],[位置],[第几次] from [表1] where [编号]='"
                    + DDBianpobianhao.Text + "' and [第几次]='" + ddEvalIndex.Text + "'";
                DataSet ds = BaseDataHelper.ExecuteQuery(sql, ConfigInfo.DataServerType, ConfigInfo.ConnectionString);
                VsTbl1 = ds.Tables[0];
                GV11.DataSource = VsTbl1;
                GV11.DataBind();

                sql = "select [项],[说明],[评分] from [表2] where [编号]='"
                   + DDBianpobianhao.Text + "' and [第几次]='" + ddEvalIndex.Text + "'";
                ds = BaseDataHelper.ExecuteQuery(sql, ConfigInfo.DataServerType, ConfigInfo.ConnectionString);
                VsTbl2 = ds.Tables[0];
                GV32.DataSource = VsTbl2;
                GV32.DataBind();

                sql = "select [概率],[评估值] from [表3] where [编号]='"
                   + DDBianpobianhao.Text + "' and [第几次]='" + ddEvalIndex.Text + "'";
                ds = BaseDataHelper.ExecuteQuery(sql, ConfigInfo.DataServerType, ConfigInfo.ConnectionString);
                VsTbl3 = ds.Tables[0];
                GV53.DataSource = VsTbl3;
                GV53.DataBind();
                #endregion
(3)导出GridView内容到WORD表格
                #region export模型3评估表
                string filename = "报告III-编号" + DDBianpobianhao.Text +
                    "(路" + lblZhongxinlicheng.Text + "侧)-第" + ddEvalIndex.Text + "次.doc";
                Response.ClearContent();
                Response.AddHeader("content-disposition", "attachment; filename=" + Server.UrlEncode(filename));
                Response.ContentType = "application/ms-word ";
                Response.ContentEncoding = System.Text.Encoding.UTF8;
                StringWriter sw = new System.IO.StringWriter();
                HtmlTextWriter htw = new HtmlTextWriter(sw);
                GridView g1 = new GridView();

                string strCaption = "基本信息";
                g1.Style.Add("width", "90%");
                g1.Caption = System.IO.Path.GetFileName(strCaption);
                g1.AutoGenerateColumns = false;
                BoundField f1 = new BoundField();
                f1.DataField = "编号";
                f1.HeaderText = "编号";
                f1.HtmlEncode = false;
                g1.Columns.Add(f1);

                BoundField f2 = new BoundField();
                f2.DataField = "位置";
                f2.HeaderText = "位置";
                g1.Columns.Add(f2);

                BoundField f3 = new BoundField();
                f3.DataField = "第几次";
                f3.HeaderText = "第几次";
                g1.Columns.Add(f3);

                g1.DataSource = VsTbl1;
                g1.DataBind();
                g1.RenderControl(htw);

                GridView g2 = new GridView();
                strCaption = "项及其评分";
                g2.Style.Add("width", "90%");
                g2.Caption = System.IO.Path.GetFileName(strCaption);
                g2.AutoGenerateColumns = false;
                f1 = new BoundField();
                f1.DataField = "项";
                f1.HeaderText = "项";
                g2.Columns.Add(f1);

                f2 = new BoundField();
                f2.DataField = "说明";
                f2.HeaderText = "说明";
                g2.Columns.Add(f2);

                f3 = new BoundField();
                f3.DataField = "评分";
                f3.HeaderText = "评分";
                f3.HtmlEncode = false;
                f3.DataFormatString = "{0:f2}";
                g2.Columns.Add(f3);

                g2.DataSource = VsTbl2;
                g2.DataBind();
                g2.RenderControl(htw);

                GridView g3 = new GridView();
                strCaption = "总评与结论";
                g3.Style.Add("width", "90%");
                g3.Caption = System.IO.Path.GetFileName(strCaption);
                g3.AutoGenerateColumns = false;
                f1 = new BoundField();
                f1.DataField = "概率";
                f1.HeaderText = "概率";
                g3.Columns.Add(f1);


                f3 = new BoundField();
                f3.DataField = "评估值";
                f3.HeaderText = "评估值";
                f3.HtmlEncode = false;
                f3.DataFormatString = "{0:f2}";
                g3.Columns.Add(f3);

                g3.DataSource = VsTbl3;
                g3.DataBind();
                g3.RenderControl(htw);
                Response.Write("<head><meta http-equiv=Content-Type content='text/html; charset=utf-8'></head>" + Environment.NewLine);
                Response.Write(sw.ToString());
                Response.End();

                #endregion

(4)输出表格的结果为

rel="File-List" href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C01%5Cclip_filelist.xml">

基本信息

编号

位置

第几次

112

1

项及其评分

说明

评分

参数1

老久的填料

2.40

参数2

风化

2.40

参数3

土壤深度: 3m

1.20

参数4

设施堵塞

5.25

参数5

降水量 33mm

4.20

参数6

腐蚀中度

2.40

参数7

人工覆盖

4.00

参数8

指数3

2.00

参数9

土壤蠕变

2.00

 

总评与结论

概率

评估值

中等风险

25.85


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值