通過Response輸出方式匯出Excel,response匯出excel

來源:互聯網
上載者:User

通過Response輸出方式匯出Excel,response匯出excel



        目前見過這麼幾種類型的匯出Excel表:


        1,NPIO方式,高仿Java.功能比較強大,但是比較重。


        2, 前輩封裝的。。。。


           3,本文要說的。。



           這周五又要導個Excel表,,上面估計是覺得平台封裝的匯出比較重吧,要我用response方式匯出excel表,一個字,沒用過。還好上過百度大學~



          

 /// <summary>        /// 擷取表的標題        /// </summary>        /// <param name="caption">The caption.</param>        /// <returns>System.String.</returns>        /// <remarks>Editor:v-liuhch CreateTime:2015/5/30 19:44:44</remarks>        private string GetHeaderName(string caption)        {            string headName = string.Empty;            switch (caption)            {                case "SerialNo":                    headName = "SN碼";                    break;                case "PrizeName":                    headName = "獎品名稱";                    break;                case "TelPhone":                    headName = "領取電話 ";                    break;                case "NickName":                    headName = "姓名";                    break;                case "AwardingTime":                    headName = "領取時間";                    break;                case "OkPrizeTime":                    headName = "抽中時間";                    break;                default:                    headName = " ";                    break;            }            return headName;        }

         上面一段是擷取欄位標題的。



         接著是匯出的過程:


      

  private void ExportSN(HttpContext context)        {            string accode = context.Request.QueryString["accode"];            HttpResponse response = context.Response;            string fileName = "SN碼匯出表-" + DateTime.Now.ToShortDateString();            excelHeader = string.Empty;            excelContent = string.Empty;            DataTable dt = PrizeAdapter.Instance.SnManagerData(accode);//擷取資料來源            DataRow[] dr = dt.Select();            int colCount = dt.Columns.Count;            string headName = string.Empty;            //列名            for (int i = 0; i < colCount; i++)            {                headName = GetHeaderName(dt.Columns[i].Caption);                if (i == colCount - 1)                {                    excelHeader += headName + "\n";                }                else                {                    excelHeader += headName + "\t";                }            }            //行資料            foreach (DataRow row in dt.Rows)            {                for (int i = 0; i < colCount; i++)                {                    if (i == colCount - 1)                    {                        excelContent += row[i].ToString() + "\n";                    }                    else                    {                        excelContent += row[i].ToString() + "\t";                    }                }                           }           // ExportGotPrizeSn(context);            response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");            response.ContentType = "application/vnd.ms-excel";            response.AppendHeader("Content-Disposition", "attachment; filename=test.xls");            response.Write(excelHeader);            response.Write(excelContent);            response.Flush();            response.End();                 }


       這裡要注意指定的contenttype.



       但是始終有一個曆史遺留問題搞不定:



       開啟匯出的Excel表的時候始終提示:


    


      

        有人說還是代碼寫的有問題,也有人說是Office的問題。。。。


 

          跪求路過大神指點~~~~~~··

      


     





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.