码迷,mamicode.com
首页 > 其他好文 > 详细

ecexl操作

时间:2015-06-12 14:42:20      阅读:108      评论:0      收藏:0      [点我收藏+]

标签:

        /// <summary>
        /// 写入ecexl
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="FileName"></param>
        public static void CreateExcel(DataTable dt, string FileName)
        {

            HttpResponse resp = HttpContext.Current.Response;

            resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");

            resp.AppendHeader("Content-Disposition", "attachment;filename=" + FileName + ".xls");

            string colHeaders = string.Empty;

            string ls_item = string.Empty;

            DataRow[] myRow = dt.Select();

            int cl = dt.Columns.Count;

            for (int i = 0; i < cl; i++)
            {
                if (i < 4)
                {
                    if (i == 3)
                    {
                        colHeaders += dt.Columns[i].Caption.ToString() + "\n";
                    }

                    else
                    {
                        colHeaders += dt.Columns[i].Caption.ToString() + "\t";
                    }
                }
            }

            resp.Write(colHeaders);

            foreach (DataRow dr in myRow)
            {

                for (int j = 0; j < cl; j++)
                {
                    if (j < 4)
                    {
                        if (j == 3)
                        {
                            ls_item += Hostname2ip(dr[j - 1].ToString()) + "\n";
                        }

                        else
                        {
                            ls_item += dr[j].ToString() + "\t";
                        }
                    }
                }

            }

            resp.Write(ls_item);

            ls_item = string.Empty;

            resp.End();

        }

  

ecexl操作

标签:

原文地址:http://www.cnblogs.com/Aamir-Ye/p/4571607.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!