• C#导出excel


     //导出excel
        public void CreateExcel(DataTable dt, string FileName, string InvType)
        {
            Response.Clear();
            Response.Charset = "UTF-8";
            Response.Buffer = true;
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            Response.AppendHeader("Content-Disposition", "attachment;filename="" + System.Web.HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8) + ".xls"");
            string colHeaders = string.Empty;
            string ls_item = string.Empty;
            DataRow[] myRow = dt.Select();
            int i = 0;
            int cl = dt.Columns.Count;
            //写表头
            DataTable dtTitle = Datehelper.Question(InvType);
            for (int Q = 0; Q < dtTitle.Rows.Count; Q++)
            {
                string Value = dtTitle.Rows[Q]["QuestionId"].ToString() + "." + dtTitle.Rows[Q]["QuestionTitle"].ToString();
                if (Q == dtTitle.Rows.Count - 1)
                {
                    ls_item += Value + "
    ";
                }
                else
                {
                    ls_item += Value + "	";
                }
    
            }
            Response.Output.Write(ls_item);
            ls_item = string.Empty;
    
    
            //写数据
            foreach (DataRow row in myRow)
            {
                for (i = 0; i < cl; i++)
                {
                    if (i == (cl - 1))
                    {
                        ls_item += row[i].ToString() + "
    ";
                    }
                    else
                    {
                        ls_item += row[i].ToString() + "	";
                    }
                }
                Response.Output.Write(ls_item);
                ls_item = string.Empty;
            }
            Response.Output.Flush();
            Response.End();
            //HttpContext.Current.ApplicationInstance.CompleteRequest();
        }
  • 相关阅读:
    无熟人难办事?—迪米特法则
    考题抄错会做也白搭—模板方法模式
    简历复印—原型模式
    Android Studio 安装及常见问题
    雷锋依然在人间——工厂方法模式
    欢迎测试
    客户端程序设计V1
    Linux服务器端程序设计V1
    【Alpha】最后一篇
    【Alpha】开发日志Day10-0721
  • 原文地址:https://www.cnblogs.com/lq0418/p/9952589.html
Copyright © 2020-2023  润新知