導出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。其中關於導出文件名的問題,解決用的是最上邊的方法  

相關文章
相關標籤/搜索