导出excel


1.用System.Web.HttpUtility.UrlEncode或者Server.UrlEncode方法,不过要2个参数都写上:
System.Web.HttpUtility.UrlEncode("报表",System.Text.Encoding.UTF8)+".xls");

2.用HttpUtility.UrlPathEncode方法,只要写一个参数就能够了:
HttpUtility.UrlPathEncode("报表.xls")html

 

====app

实际中使用方法:ide

        #region
        string style = @"<style> .text { mso-number-format:\@; } </script> ";
        string s = FileName;
        Response.Clear();
        //获取或设置一个值,该值指示是否缓冲输出,并在完成处理整个响应以后将其发送
        Response.Buffer = true;
        //获取或设置输出流的HTTP字符集
        Response.Charset = "GB2312";
        // filename = System.Web.HttpUtility.UrlEncode(filename, System.Text.Encoding.GetEncoding("GB2312 "));
        //将HTTP头添加到输出流
        Response.AppendHeader("Content-Disposition", "p_w_upload;filename=" + HttpUtility.UrlEncode(s + ".xls", Encoding.UTF8).ToString());
        //获取或设置输出流的HTTP字符集
        Response.ContentEncoding = System.Text.Encoding.UTF7;
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
        //获取或设置输出流的HTTP MIME类型
        // Response.ContentType = "application/ms-excel";this

        Response.ContentType = "application/vnd.xls";excel

        System.IO.StringWriter onstringwriter = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter onhtmltextwriter = new System.Web.UI.HtmlTextWriter(onstringwriter);
        StringWriter sw = new StringWriter();
        Response.Write(style);
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        this.GridView1.RenderControl(htw);
        string html = sw.ToString().Trim();
        Response.Output.Write(html);
        Response.Flush();
        Response.End();code

上边是从gridview总导出数据orm

    public void CreateExcelNew(DataSet ds, string typeid, string FileName)
        {
            HttpResponse resp;
            resp = Page.Response;
            resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            resp.AppendHeader("Content-Disposition", "p_w_upload;filename=" + HttpUtility.UrlPathEncode("住户表即时数据("+DateTime.Now.ToString("MMddhhmmss")+").xls")+ "");
            resp.ContentType = "application/ms-excel";htm


            string colHeaders = "", ls_item = "";
            int i = 0;对象

            //定义表对象与行对像,同时用DataSet对其值进行初始化
            System.Data.DataTable dt = ds.Tables[0];
            DataRow[] myRow = dt.Select("");
            // typeid=="1"时导出为EXCEL格式文件;typeid=="2"时导出为XML格式文件
            if (typeid == "1")
            {ip

                colHeaders += "" + "\t" + "住户表即时数据\n";
                colHeaders += "时间:" + DateTime.Now.ToLongDateString() + "\n";
                //                 
                    String[] title = { "dfg", " 这里是不少的列的集合" };


                    for (i = 0; i < title.Length; i++)
                    {

                        colHeaders += title[i].ToString() + "\t";
                    }

                    colHeaders += "\n";
                    //向HTTP输出流中写入取得的数据信息
                    resp.Write(colHeaders);
                    //逐行处理数据 这部分处理的是列数据。
                    foreach (DataRow row in myRow)
                    {
                        //在当前行中,逐列得到数据,数据之间以\t分割,结束时加回车符\n
                        ls_item += row["id"].ToString() + "\t";
                        ls_item += "'" + row["zhcode"].ToString() + "\t";
                        ls_item += row["xqname"].ToString() + "\t";
                        ls_item += "'" + row["jzqnum"].ToString() + "\t";
                        ls_item += row["jzqname"].ToString() + "\t";

 

                        ls_item += "'" + row["lhnum"].ToString() + "\t";
                        ls_item += "'" + row["dynum"].ToString() + "\t";


                        ls_item += "'" + row["fjnum"].ToString() + "\t";
                        ls_item +=   row["cnmj"].ToString() + "\t";
                        ls_item += row["cjsj"].ToString() + "\t";
                        ls_item +=  string.Format("{0:#0.00}", row["rl"].ToString()) + "\t";
                       
                        ls_item +=   row["jtsj"].ToString() + "\t";


                        ls_item +=  string.Format("{0:#0.0}", row["gswd"].ToString()) + "\t";
                        ls_item +=  string.Format("{0:#0.0}",  row["hswd"].ToString()) + "\t";

                        ls_item +=  string.Format("{0:#0.0}",row["sdwd"].ToString()) + "\t";
                        ls_item += string.Format("{0:#0.0}", row["sw"].ToString()) + "\t";
                       
                        ls_item +=string.Format("{0:#0.0}", row["zqsw"].ToString()) + "\t";
                        ls_item += string.Format("{0:#0.0}", row["ljsw"].ToString()) + "\t";
                        ls_item += string.Format("{0:#0.0}", row["wc"].ToString()) + "\t";


                        ls_item +=   row["bsb2"].ToString() + "\t";
                        ls_item +=   row["fsb2"].ToString() + "\t";
                        ls_item +="'"+ row["zhbnum"].ToString() + "\t";
                        

                        ls_item += "\n";
                        //for (i = 2; i < ds.Tables[0].Columns.Count - 1; i++)
                        //    ls_item += row[i].ToString() + "\t";
                        //ls_item += row[i].ToString() + "\n";
                        //当前行数据写入HTTP输出流,而且置空ls_item以便下行数据
                        resp.Write(ls_item);
                        ls_item = "";
                    }
               
            }
            else
            {
                if (typeid == "2")
                {
                    //从DataSet中直接导出XML数据而且写到HTTP输出流中
                    resp.Write(ds.GetXml());
                }
            }
            //写缓冲区中的数据到HTTP头文件中
            resp.End();

        }

这个是从dataset导出excel。其中关于导出文件名的问题,解决用的是最上边的方法  

相关文章
相关标签/搜索