C# 导出数据成CSV文件

 public bool ExportPatientStatisticsDetails(ExportPatientDetailDto patientDetails)
        {
            try
            {
                var patientList = patientDetails.PatientTableList;

                if (File.Exists(@"D:\患者统计表.csv"))
                {
                    File.Delete(@"D:\患者统计表.csv");
                }
                StringBuilder strColu = new StringBuilder();
                StringBuilder strValue = new StringBuilder();
                StreamWriter sw = new StreamWriter(new FileStream(@"D:\患者统计表.csv",FileMode.CreateNew),Encoding.GetEncoding("GB2312"));
                strColu.Append("部位,患者数,比例");
                sw.WriteLine(strColu);
                foreach (var dr in patientList)
                {
                    strValue.Remove(0, strValue.Length);//移出
                    strValue.Append(dr.NameOfDistributionType + ",");
                    strValue.Append(dr.NumberOfPatients + ",");
                    strValue.Append(dr.Proportion);
                    sw.WriteLine(strValue);
                }
                sw.Close();
                return  true;

            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return false;
            }

        }

备注:

传入的参数类,可根据需求定制

 public class ExportPatientDetailDto
    {
        public List<PatientStatisticsOutputDto> PatientTableList { get; set; }
    }
public class PatientStatisticsOutputDto
    {
        public string NameOfDistributionType { get; set; }

        public int NumberOfPatients { get; set; }

        public double Proportion { get; set; }

    }
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容