博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

生成excel的最简便的方法

Posted on 2008-12-11 15:45  yuanws  阅读(163)  评论(0编辑  收藏  举报

          

  string   FileName =  DateTime.Now.Month + DateTime.Now.Day + DateTime.Now.Hour + DateTime.Now.Minute;
            HttpContext.Current.Response.AddHeader("Content-Disposition", "inline;filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8) + ".xls");
            HttpContext.Current.Response.ContentType = "application/ms-excel";
            DataTable dt = webBll.getExcel();
            int i = 0, j;
            for (i = 0; i < dt.Columns.Count; i++)
            {
                HttpContext.Current.Response.Write(dt.Columns[i].ToString() + "\t");
            }
            HttpContext.Current.Response.Write("\n");
            foreach (DataRow dr in dt.Rows)
            {
                for (j = 0; j < dr.ItemArray.Length; j++)
                    HttpContext.Current.Response.Write(dr.ItemArray[j].ToString() + "\t");
                HttpContext.Current.Response.Write("\n");
            }
            HttpContext.Current.Response.End();