C# 需要将列表传递到此csv导出类的帮助吗
我在网上找到了这个CSV导出类,想从另一个类传递我自己的列表 我的列表已经准备好,包含3列C# 需要将列表传递到此csv导出类的帮助吗,c#,export,export-to-csv,C#,Export,Export To Csv,我在网上找到了这个CSV导出类,想从另一个类传递我自己的列表 我的列表已经准备好,包含3列 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.SqlTypes; using System.IO; using System.Reflection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlTypes;
using System.IO;
using System.Reflection;
public class CsvExport<T> where T : class
{
public List<T> Objects;
public CsvExport(List<T> objects)
{
Objects = objects;
}
public string Export()
{
return Export(true);
}
public string Export(bool includeHeaderLine)
{
StringBuilder sb = new StringBuilder();
//Get properties using reflection.
IList<PropertyInfo> propertyInfos = typeof(T).GetProperties();
if (includeHeaderLine)
{
//add header line.
foreach (PropertyInfo propertyInfo in propertyInfos)
{
sb.Append(propertyInfo.Name).Append(",");
}
sb.Remove(sb.Length - 1, 1).AppendLine();
}
//add value for each property.
foreach (T obj in Objects)
{
foreach (PropertyInfo propertyInfo in propertyInfos)
{
sb.Append(MakeValueCsvFriendly(propertyInfo.GetValue(obj, null))).Append(",");
}
sb.Remove(sb.Length - 1, 1).AppendLine();
}
return sb.ToString();
}
//export to a file.
public void ExportToFile(string path)
{
File.WriteAllText(path, Export());
}
//export as binary data.
public byte[] ExportToBytes()
{
return Encoding.UTF8.GetBytes(Export());
}
//get the csv value for field.
private string MakeValueCsvFriendly(object value)
{
if (value == null) return "";
if (value is Nullable && ((INullable)value).IsNull) return "";
if (value is DateTime)
{
if (((DateTime)value).TimeOfDay.TotalSeconds == 0)
return ((DateTime)value).ToString("yyyy-MM-dd");
return ((DateTime)value).ToString("yyyy-MM-dd HH:mm:ss");
}
string output = value.ToString();
if (output.Contains(",") || output.Contains("\""))
output = '"' + output.Replace("\"", "\"\"") + '"';
return output;
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Data.SqlTypes;
使用System.IO;
运用系统反思;
公共类CsvExport,其中T:class
{
公开列出对象;
公共CsvExport(列出对象)
{
对象=对象;
}
公共字符串导出()
{
退货出口(真实);
}
公共字符串导出(bool includeHeaderLine)
{
StringBuilder sb=新的StringBuilder();
//使用反射获取属性。
IList propertyInfos=typeof(T).GetProperties();
如果(包括领头线)
{
//添加标题行。
foreach(PropertyInfo PropertyInfo in propertyInfos中的PropertyInfo)
{
sb.Append(propertyInfo.Name);
}
删除(sb.Length-1,1.AppendLine();
}
//为每个属性添加值。
foreach(对象中的对象)
{
foreach(PropertyInfo PropertyInfo in propertyInfos中的PropertyInfo)
{
sb.Append(MakeValueCsvFriendly(propertyInfo.GetValue(obj,null)).Append(“,”);
}
删除(sb.Length-1,1.AppendLine();
}
使某人返回字符串();
}
//导出到文件。
公共void导出文件(字符串路径)
{
writealText(路径,Export());
}
//导出为二进制数据。
公共字节[]导出字节()
{
返回Encoding.UTF8.GetBytes(Export());
}
//获取字段的csv值。
私有字符串MakeValueCsvFriendly(对象值)
{
如果(value==null)返回“”;
if(value可为null&((INullable)value).IsNull)返回“”;
if(值为DateTime)
{
如果(((DateTime)值).TimeOfDay.TotalSeconds==0)
返回((日期时间)值).ToString(“yyyy-MM-dd”);
返回((日期时间)值).ToString(“yyyy-MM-dd-HH:MM:ss”);
}
字符串输出=value.ToString();
if(output.Contains(“,”)| output.Contains(“\”)
输出='“'+输出。替换(“\”,“\”)+';
返回输出;
}
}
以下是使用Exporter和示例类的示例
class Sample
{
public string Field1 {get;set;}
public int Field2 {get;set;}
}
List<Sample> source = new List<Sample>()
// fill list
CsvExport<Sample> export = new CsvExport<Sample>(source);
export.ExportToFile("yourFile.csv");
类示例
{
公共字符串字段1{get;set;}
公共int字段2{get;set;}
}
列表源=新列表()
//填写清单
CsvExport导出=新CsvExport(源);
export.ExportToFile(“yourFile.csv”);
因此,基本上创建
CsvExport
和现有对象的路径列表到它如果您需要使用该类的帮助,那么我会说您需要这样做
为您的数据创建一个类
public class MyData
{
public string Column1Data {get;set;}
public string Column2Data {get;set;}
public string Column3Data {get;set;}
}
将MyData类型的列表传递到CsvExport类中,如下所示
List<MyData> list = new List<MyData>();
//populate the list here
CsvExport<MyData> export = new CsvExport<MyData>();
export.ExportToFile(@"C:\MyExportFile.txt");
要使用不同的输入类,只需将MyData引用切换到您的类所称的内容您的问题是什么???那么您的问题是什么?如何使用这个类?或者如何让你的班级与之合作?导出CSV非常简单,我更喜欢编写自己的类来处理它,而我的列表不是list,我是否应该将其更改为我的实际列表如果我想从另一个列表导出结果呢class@bugz:您正在使用的CsvExport类使用反射基于属性创建列值。因此,您传递给它的类只需要具有您希望在导出中使用的属性。在我看来,无论如何,这不是一个好方法。您的输入类有哪些数据?如何存储列标题和数据?该类返回我需要返回结果的确切数据。其中(i=>IForEmployee(facade,i.Employee)).ToList();是它返回的内容,包括Employee=Employee,Manager=Manager,Weeks=new List{new Week(info.Week)。ToString(“end”)}result是我需要的列表var result=new List()@bugz:你能发布你的类的结构吗(在一个单独的代码块中-所以它有滚动条)啊,我认为它可以工作,问题是程序需要永远运行
Column1Data,Column2Data,Column3Data
r1c1,r1c2,r1c3
r2c1,r2c2,r2c3
r3c1,r1c2,r3c3