导出的方法

方法一:

/// <summary>
        /// 导出方法
        /// </summary>
        /// <param name="fileType"></param>
        /// <param name="fileName"></param>
        public void Export(string fileType, string fileName)
        {
            Response.Charset = "GB2312";
            Response.ContentEncoding = System.Text.Encoding.UTF8;
            Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName, Encoding.UTF8).ToString());
            Response.ContentType = fileType;
            this.EnableViewState = false;
            StringWriter tw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(tw);
            this.grdList.AllowPaging = false;//设置不分页
            BindHistoryScoresNotPaging();//不分页绑定
            this.grdList.RenderControl(hw);
            Response.Write(tw.ToString());
            Response.End();
            this.grdList.AllowPaging = true;
            BindHistoryScores();//分页绑定
        } 



        /// <summary>
        /// 导出考试成绩
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDaoChu_Click(object sender, EventArgs e)
        {
            Export("Application/ms-Excel", "用户信息.xls");
        }

        //override掉这个方法
        public override void VerifyRenderingInServerForm(Control control)
        {
            //注释掉下面的代码,否则在asp.net2.0下会报错(注:GridView是asp.net2.0下的控件,1.1下一些控件也可以导出成Excel或者Word)
            //base.VerifyRenderingInServerForm(control);
        }

方法二:

 /// <summary>
    /// 导出实验室产出
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Button2_Click(object sender, EventArgs e)//daochu
    {
        if (ds != null)
        {
            CreateExcel(ds, "1", "outExcel");
        }
        else
        {
            mtd.Alert("没有可导出的数据!", this);
        }
    }
    public void CreateExcel(DataSet ds, string typeid, string FileName)
    {
        HttpResponse resp;
        resp = Page.Response;
        resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
        resp.AppendHeader("Content-Disposition", "attachment;filename=" + FileName + ".xls");
        resp.ContentType = "application/ms-excel";
        string colHeaders = "", ls_item = "";

        //定义表对象与行对像,同时用DataSet对其值进行初始化
        System.Data.DataTable dt = ds.Tables[0];
        DataRow[] myRow = dt.Select("");
        // typeid=="1"时导出为EXCEL格式文件;typeid=="2"时导出为XML格式文件
        if (typeid == "1")
        {
            //取得数据表各列标题,各标题之间以\t分割,最后一个列标题后加回车符
            //for (i = 0; colHeaders += dt.Columns[i].Caption.ToString() + "\t";
            //colHeaders += dt.Columns[i].Caption.ToString() + "\n")
            //    //向HTTP输出流中写入取得的数据信息
            //    resp.Write(colHeaders);
            //逐行处理数据
            int mn = conn.GetRowCount(str_Sql);
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                if (i != 0 && i != 13 && i != 14 && i != 15)
                {
                    ls_item += dt.Columns[i].ToString() + "\t";
                    //ls_item += row[i].ToString() + "\n";
                    //当前行数据写入HTTP输出流,并且置空ls_item以便下行数据    
                }
            }
            ls_item = ls_item.TrimEnd(new char[] { '\t' });
            ls_item += "\n";
            resp.Write(ls_item);
            ls_item = "";
            foreach (DataRow row in myRow)
            {
                //在当前行中,逐列获得数据,数据之间以\t分割,结束时加回车符\n
                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    if (i != 0 && i != 13 && i != 14 && i != 15)
                    {
                        ls_item += row[dt.Columns[i]].ToString() + "\t";
                    }

                }
                ls_item = ls_item.TrimEnd(new char[] { '\t' });
                ls_item += "\n";
                resp.Write(ls_item);
                ls_item = "";
            }
        }
        else
        {
            if (typeid == "2")
            {
                //从DataSet中直接导出XML数据并且写到HTTP输出流中
                resp.Write(ds.GetXml());
            }
        }
        //写缓冲区中的数据到HTTP头文件中
        resp.End();
    }

posted @ 2011-04-26 17:10  乄蛇~  阅读(162)  评论(0编辑  收藏  举报