通过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表的时候始终提示:


     通过Response输出方式导出Excel_第1张图片


      

        有人说还是代码写的有问题,也有人说是Office的问题。。。。


 

          跪求路过大神指点~~~~~~··

      


     





你可能感兴趣的:(.net)