gpt4 book ai didi

c# - 我如何更改 CSVHelper 中的字段顺序和字段名称

转载 作者:行者123 更新时间:2023-12-02 03:44:35 25 4
gpt4 key购买 nike

我正在开发一个 asp.net mvc 4 Web 应用程序 + Entity Framework 5.0,并且我有以下方法调用名为 AdvanceSearchStoredProcedure 的函数,该函数将调用存储过程:-

     public IQueryable<AdvanceSearchSP_Result> AdvanceSearch(NetworkAdvanceSearch na)
{

var r = entities.AdvanceSearchStoredProcedure("network",na.AssetTypeID,
na.ip, na.ipselection, na.mac, na.macselection);
return r.AsQueryable();

}

然后我从我的操作方法中调用此方法,如下所示:-

public ActionResult AdvanceSearchIndexExport(AdvanceSearchSP_Result2 ns)

{
var result = WriteCsvToMemory(repository.AdvanceSearch(ns.NetwotkAS).ToList());
var memoryStream = new MemoryStream(result);
return new FileStreamResult(memoryStream, "text/csv") { FileDownloadName = "export.csv" };
}
public byte[] WriteCsvToMemory(IEnumerable<AdvanceSearchSP_Result> records)
{
using (var memoryStream = new MemoryStream())
using (var streamWriter = new StreamWriter(memoryStream))
using (var csvWriter = new CsvWriter(streamWriter))
{
csvWriter.WriteRecords(records);
streamWriter.Flush();
return memoryStream.ToArray();
}
}

这是我的 AdvanceSearchSP_Result 的部分类(class)及其 MetadataType 类:-

[MetadataType(typeof(AdvanceSearchSP_Result_Validation))]

public partial class AdvanceSearchSP_Result
{


}
public class AdvanceSearchSP_Result_Validation{
[Display(Name="Service Tag")]
public string SERVICETAG { get; set; }
[Display(Name = "Serial Number")]
public string SERIALNO { get; set; }
[Display(Name = "Resource Tag")]
public string Tag { get; set; }
[Display(Name = "Resource Name")]
public string RESOURCENAME { get; set; }
[Display(Name = "State")]
public string StateName { get; set; }
[Display(Name = "Type")]
public string TypeName { get; set; }
[Display(Name = "Site Name")]
public string sitename { get; set; }
[Display(Name = "Customer Name")]
public string cusotmername { get; set; }
public bool ismanaged { get; set; }
}

现在我在生成的 .CSV 中面临这些问题文件:-

  1. 字段标题将等于数据库内的列名称,而不是像 AdvanceSearchSP_Result_Validation 中定义的那样。 MetadataType 类。

  2. 第二个问题,我不确定如何更改列的顺序?目前,顺序将与存储过程中定义的顺序相同。例如,我如何强制 ismanaged成为 .csv 文件中的第一列?

我正在使用 CSVHelper https://www.nuget.org/packages/CsvHelper/库来执行实际的 CSV 导出。

编辑现在我添加了这个类:-

public sealed class SPMap : ClassMap<AdvanceSearchSP_Result>
{
public SPMap()
{
Map(m => m.Tag).Name("Resource Tag").Index(0);
Map(m => m.RESOURCENAME).Name("Asset Name").Index(1);
Map(m => m.cusotmername).Name("Customer Name").Index(2);
Map(m => m.sitename).Name("Site Name").Index(3);
Map(m => m.StateName).Name("Asset State").Index(4);
Map(m => m.ismanaged).Name("ismanaged").Index(5);
Map(m => m.TypeName).Name("Technology Type ").Index(6);
Map(m => m.SERVICETAG).Name("Service Tag").Index(7);
Map(m => m.SERIALNO).Name("Serial Number").Index(8);

}
}

然后我修改我的方法以包含如下配置:-

 public byte[] WriteCsvToMemory(IEnumerable<AdvanceSearchSP_Result> records)
{
using (var memoryStream = new MemoryStream())
using (var streamWriter = new StreamWriter(memoryStream))
using (var csvWriter = new CsvWriter(streamWriter))
{
csvWriter.WriteRecords(records);
csvWriter.Configuration.RegisterClassMap<SPMap>();
streamWriter.Flush();
return memoryStream.ToArray();
}
}

但我仍然得到旧名称和旧顺序..看起来 public sealed class SPMap : ClassMap<AdvanceSearchSP_Result>没有任何效果...

最佳答案

我认为您需要一个映射来指定列的名称和索引:

public sealed class SPMap : ClassMap<AdvanceSearchSP_Result>
{
public SPMap()
{
Map(m => m.SERVICETAG).Name("Service Tag").Index(0);
Map(m => m.sitename).Name("Site Name").Index(1);
/*repeat for other properties */
..
..
}
}

然后你需要注册这个:

csvWriter.Configuration.RegisterClassMap<SPMap>();

我相信这会起作用。

参见: http://joshclose.github.io/CsvHelper/mapping

关于c# - 我如何更改 CSVHelper 中的字段顺序和字段名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47227645/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com