当前位置:编程学习 > C#/ASP.NET >>

asp.net导出excel

运行时可以导出,但是发布后就不行了,各位大侠有没有什么好的解决方法啊?
我的代码如下:
HttpResponse resp;
            resp = Page.Response;
            resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            resp.AppendHeader("Content-Disposition", "attachment;filename=ll.xls");
            resp.ContentType = "application/ms-excel";
            this.EnableViewState = false;  
            string colHeaders = "", Is_item = "";
            //int i = 0;
            if (ds.Tables.Count != 0 && ds.Tables[0].Rows.Count != 0)
            {
                int count = ds.Tables[0].Rows.Count;
                colHeaders = "学号";
                resp.Write(colHeaders + "\t"););
                colHeaders="姓名";
                resp.Write(colHeaders + "\t");
                colHeaders = "性别";
                resp.Write(colHeaders + "\t");
                colHeaders = "体育班信息";
                resp.Write(colHeaders + "\t");
                colHeaders = "行政班";
                resp.Write(colHeaders + "\t");
                colHeaders  = "年级";
                resp.Write(colHeaders + "\t");
                colHeaders = "考试成绩";
                resp.Write(colHeaders + "\n");
                if (type == 0)
                {
                    for (int i = 0; i < count; i++)
                    {
                        //for (int j = 0; j < 7; j++)
                        //{
                        //    excel.Cells[i + 2, j + 1] = ds.Tables[0].Rows[i][j].ToString();
                        //}
                        Is_item  = ds.Tables[0].Rows[i][0].ToString();
                        resp.Write(Is_item + "\t");
                        Is_item  = ds.Tables[0].Rows[i][1].ToString();
                        resp.Write(Is_item + "\t");
                        Is_item = ds.Tables[0].Rows[i][2].ToString();
                        resp.Write(Is_item + "\t");
                        Is_item = ds.Tables[0].Rows[i][7].ToString();
                        resp.Write(Is_item + "\t");
                        Is_item= ds.Tables[0].Rows[i][3].ToString();
                        resp.Write(Is_item + "\t");
                        Is_item = ds.Tables[0].Rows[i][4].ToString();
                        resp.Write(Is_item + "\t");
                        Is_item = ds.Tables[0].Rows[i][5].ToString();
                        resp.Write(Is_item + "\n");
                    }
                }
                else
                {
                    for (int i = 0; i < count; i++)
                    {
                        for (int j = 0; j < 7; j++)
                        {
                            Is_item = ds.Tables[0].Rows[i][j].ToString();
                            if (j != 6)
                            {
                                resp.Write(Is_item + "\t");
                            }
                            else
                            {
                                resp.Write(Is_item + "\n");
                            }
                        }
                    }
                }
                cook.Expires = DateTime.Now;
                Response.Cookies.Add(cook);
            }
            //Is_item = "kk";
            //resp.Write(Is_item);
            resp.End(); --------------------编程问答--------------------

protected void Button1_Click(object sender, EventArgs e)
        {
            string filename = "理赔查看";
            Export("application/ms-excel", filename);
        }
        private void Export(string fileType, string filename)
        {
            Response.Clear();
            Response.Buffer = true;
            Response.Charset = "gb2312";
            Response.ContentEncoding = System.Text.Encoding.UTF7;
            Response.AppendHeader("content-disposition", "attachment;filename=\"" + System.Web.HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8) + ".xls\"");
            //Response.AppendHeader("Content-Disposition","attachment;filename="+System.Web.HttpUtility.UrlEncode(filename,System.Text.Encoding.UTF8)+".xls");
            Response.ContentType = fileType;
            this.EnableViewState = false;
            StringWriter tw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(tw);
            GridView1.RenderControl(hw);
            Response.Write(tw.ToString());
            Response.End();
        }
        public override void VerifyRenderingInServerForm(System.Web.UI.Control control)
        {

            //base.VerifyRenderingInServerForm(control);


        }
这是一段可以正常使用的代码
--------------------编程问答-------------------- ?
?????????????????????? --------------------编程问答-------------------- 学习了。。。。。。。。。。
补充:.NET技术 ,  其他语言
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,