首页 > 编程语言 >C#下载csv代码总结(解决中文乱码问题)

C#下载csv代码总结(解决中文乱码问题)

时间:2022-08-24 15:00:55浏览次数:62  
标签:cell string C# 乱码 whereQuery strLine new csv Response

  • /// <summary>
  • /// 下载
  • /// </summary>
  • /// <param name="startTime"></param>
  • /// <param name="endTime"></param>
  • public void Download(DateTime? startTime, DateTime? endTime)
  • {
  • Response<VSysLog> _rsp = new Response<VSysLog>();
  • try
  • {
  • using (NetEntities et = new NetEntities())
  • {
  • startTime = startTime == null ? DateTime.Now.AddMonths(-) : startTime.Value;
  • endTime = endTime == null ? DateTime.Now : endTime.Value;
  • int deviceType = (int)EnumDeviceType.网关设备;
  •  
  • //搜索条件
  • var whereQuery = PredicateExtensions.True<net_warninglog>();
  • //搜索条件---开始时间和结束时间
  • whereQuery = whereQuery.And(n => n.WarningTime >= startTime && n.WarningTime <= endTime);
  • //搜索条件---设备类型
  • whereQuery = whereQuery.And(n => n.DeviecType == deviceType);
  • //搜索条件---模糊查询
  • if (!string.IsNullOrEmpty(Request["condition"]))
  • {
  • string condition = Request["condition"];
  • whereQuery = whereQuery.And(n => n.WarningSource.Contains(condition));
  • }
  • List<VWarningLog> logList = et.net_warninglog.Where(whereQuery.Compile()).AsEnumerable().Select(n =>
  • new VWarningLog
  • {
  • id = n.ID,
  • warningName = n.WarningName,
  • warningReason = n.WarningReason,
  • deviceType = Enum.GetName(typeof(EnumDeviceType), n.DeviecType),
  • warningSource = n.WarningSource,
  • descr = n.Descr,
  • warningTime = n.WarningTime.ToString("yyyy-MM-dd HH:mm:ss")
  • }).OrderByDescending(x => x.warningTime).ToList();
  •  
  • System.IO.StringWriter sw = new StringWriter();
  • StringBuilder sbTitle = new StringBuilder();
  • System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
  • //定义模版(标题、内容字段、格式化参数)
  • string[,] template = new string[,] {{ "终端编号,", "warningSource", "{0}"}, { "设备类型,", "deviceType", "{0}" }, { "报警原因,", "warningReason", "{0}"},
  • { "报警信息,", "warningName", "{0}" }, { "描述", "descr", "{0}" }, { "报警时间", "warningTime", "{0}" } };
  • string strLine = "";
  • sw = new StringWriter();
  • //获取模板的行数
  • int colCount = template.GetLength();
  • //表头
  • for (int i = ; i < colCount; i++)
  • {
  • //在模板里面已经添加了逗号
  • strLine += template[i, ];
  • }
  • strLine.Remove(strLine.Length - );
  • sw.WriteLine(strLine);
  • strLine = "";
  •  
  • //表的内容
  • for (int j = ; j < logList.Count; j++)
  • {
  • strLine = "";
  • for (int k = ; k < colCount; k++)
  • {
  • if (k > && k < colCount)
  • {
  • strLine += ",";
  • }
  • string cell = "";
  • string data = string.Format(template[k, ], logList[j].GetType().GetProperty(template[k, ]).GetValue(logList[j], null));
  • if (string.IsNullOrEmpty(data))
  • {
  • strLine += "";
  • }
  • else
  • {
  • //前面加的单引号则是防止数字被转换成其它格式
  • cell = "'" + data.Trim();
  • }
  • //防止里面含有特殊符号
  • if (!string.IsNullOrEmpty(cell))
  • {
  • cell = cell.Replace("\"", "\"\"");
  • cell = "\"" + cell + "\"";
  • strLine += cell;
  • }
  • }
  • sw.WriteLine(strLine);
  • }
  • string attachment = "attachment; filename=" + DateTime.Now.ToString("yyyy年MM月dd日HH点") + "网关报警日志.csv";
  • Response.Clear();
  • Response.ClearHeaders();
  • Response.ClearContent();
  • Response.AddHeader("content-disposition", attachment);
  • Response.ContentType = "text/csv";
  • Response.AddHeader("Pragma", "public");
  • Response.Charset = "UTF-8";
  • Response.ContentEncoding = System.Text.Encoding.UTF8;
  • Response.HeaderEncoding = System.Text.Encoding.UTF8;
  • //防止中文乱码(解决中文乱码问题)
  • Response.BinaryWrite(new byte[] { 0xEF, 0xBB, 0xBF });
  • response.Write(sw.ToString());
  • Response.End();
  • sw.Close();
  • }
  • }
  • catch (Exception ex)
  • {
  • _rsp.code = (int)EnumCode.程序异常;
  • _rsp.msg = ex.Message;
  • LogHelper.WriteLog(className, "Download", ex);
  • }
  • }

标签:cell,string,C#,乱码,whereQuery,strLine,new,csv,Response
From: https://www.cnblogs.com/cuihongyu3503319/p/16619935.html

相关文章

  • C# 读取MotherBoard的信息
    通过C#来读取PC的MotherBoard上的信息,如产品名称,制造商,版本等,方法如下:Reference中添加System.Management,并在头文件中引入该Assemble添加对应的类,并进行使用,如......
  • SpringMVC、MVC、JavaBean、表述层、三层架构
    来源:BV1Ry4y1574R?p=3、BV1Ry4y1574R?p=4SpringMVC是Spring的MVC模块,也就是来实现Web功能的模块。Spring里面包含有很多的模块,比如说SpringFramework是它的......
  • ASP.NET Core Docker容器部署
    最简单的部署操作方式1.安装辅助工具1.1安装Gitsudoyum-yinstallgit安装完成后输入git命令查看1.2安装Nginx(这种部署方式暂时没用到)参考文档:https://......
  • 历时2月,动态线程池 DynamicTp 发布里程碑版本 V1.0.8
    关于DynamicTpDynamicTp是一个基于配置中心实现的轻量级动态线程池管理工具,主要功能可以总结为动态调参、通知报警、运行监控、三方包线程池管理等几大类。经过多个版......
  • kafka的producer配置参数
    主要介绍下kafka的producer配置参数,只取了其中的一部分常用的,后续的有时间,也会补充一些,更多的详细参数,可以参考《kafka官网》,参数的内容,主要是选取《apachekafka实战》书......
  • CentOS安装部署Weblogic12.1.3
    开始以为和旧版安装一样,使用控制台的方式,下载bin文件,然后一步步在console执行下来就行了。万万没想到,从12C版本后,bin文件不提供了,改成全系统通用的jar文件(generic.jar)。......
  • 万物皆可集成系列:低代码对接泛微e-cology
    近些年来,当谈论到企业信息化的时候,OA作为集团化全员应用,在企业经营管理中使用非常广泛。众所周知,泛微e-cology作为面向大中型企业的平台型协同办公自动化系统,它的标准化功......
  • JS doc 接口文档生成器
    前言项目中使用到需要把js方法生成接口文档,使用到了JSdoc这个工具,使用该工具生成文档,需要在方法里加入注释,根据注释说明生成文档,这里顺便记录一下使用过程,模拟了一些j......
  • 用 Wireshark 分析 TCP 吞吐瓶颈
    https://www.kawabangga.com/posts/4794Debug网络质量的时候,我们一般会关注两个因素:延迟和吞吐量(带宽)。延迟比较好验证,Ping一下或者 mtr 一下就能看出来。这篇文章分......
  • oracle 怎么查看用户对应的表空间
    oracle怎么查看用户对应的表空间?查询用户:查看数据库里面所有用户,前提是你是有dba权限的帐号,如sys,system:select*fromdba_users;查看你能管理的所有用户:select......