CVS导出&&自定义Attribute的使用
1.cvs导出:List转为byte[]
/// <summary> /// CvsExport帮助类 /// </summary> public static class CvsExportHelper { /// <summary> /// Creates the CSV from a generic list. /// </summary>; /// <typeparam name="T"></typeparam>; /// <param name="list">The list.</param>; public static byte[] CreateCSVFromGenericList<T>(List<T> list) { if (list == null || list.Count == 0) return null; //get type from 0th member Type t = list[0].GetType(); string newLine = Environment.NewLine; MemoryStream output = new MemoryStream(); using (var sw = new StreamWriter(output, new UTF8Encoding(true)))// 用true来指定包含bom { //make a new instance of the class name we figured out to get its props object o = Activator.CreateInstance(t); //gets all properties PropertyInfo[] props = o.GetType().GetProperties(); //foreach of the properties in class above, write out properties //this is the header row foreach (PropertyInfo pi in props) { var attributes = pi.GetCustomAttributes(false); var columnMapping = attributes .FirstOrDefault(a => a.GetType() == typeof(CSVColumnAttribute)); if (columnMapping != null) { var mapsto = columnMapping as CSVColumnAttribute; sw.Write(mapsto.Name + ","); } else { sw.Write(pi.Name + ","); } } sw.Write(newLine); //this acts as datarow foreach (T item in list) { //this acts as datacolumn foreach (PropertyInfo pi in props) { //this is the row+col intersection (the value) string whatToWrite = Convert.ToString(item.GetType() .GetProperty(pi.Name) .GetValue(item, null)) .Replace(',', ' ') + ','; sw.Write(whatToWrite); } sw.Write(newLine); } sw.Flush(); } byte[] bytesInStream = output.ToArray(); // simpler way of converting to array output.Close(); return bytesInStream; } }
2.前端调用(网页:内存导出)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | /// <summary> /// CSV Export /// </summary> /// <param name="r"></param> /// <returns></returns> [HttpPost] [ActionName( "CSVExport" )] public ActionResult CSVExport(ExportDSVRequest r) { string fileName = string .Format( "CRMInfo-{0}.csv" , Guid.NewGuid().ToString()); byte [] result = null ; if ( string .IsNullOrWhiteSpace(r.JsonData)) { return Redirect( "~/report/list" ); //返回上一页 } var billsResult = JsonHelper.DeserializeJsonToList<AccountDailyMarginCSV>(r.JsonData); result = CvsExportHelper.CreateCSVFromGenericList(billsResult); return File(result, "text/csv" , fileName); } |
导出至特定路径
1 2 3 4 5 6 7 8 9 10 11 12 13 | string fileName = string .Format( "CRMInfo-{0}.csv" , Guid.NewGuid().ToString()); string content = log.ToString(); string dir = Directory.GetCurrentDirectory(); string fullName = Path.Combine(dir, fileName); if (File.Exists(fullName)) File.Delete(fullName); using (FileStream fs = new FileStream(fullName, FileMode.CreateNew, FileAccess.Write)) { StreamWriter sw = new StreamWriter(fs, Encoding.Default); sw.Flush(); sw.Write(content); sw.Flush(); sw.Close(); } |
自定义attribute
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | /// <summary> /// CSV列名Attribute /// </summary> public class CSVColumnAttribute : Attribute { /// <summary> /// Name /// </summary> public string Name { get ; } /// <summary> /// set name /// </summary> /// <param name="name"></param> public CSVColumnAttribute( string name) { this .Name = name; } } |
attribute使用:对List<T>中的T设置列名
分类:
实战
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· 没有源码,如何修改代码逻辑?
· NetPad:一个.NET开源、跨平台的C#编辑器
· 面试官:你是如何进行SQL调优的?