FileHelpers 用法 z

用FileHelplers导出csv数据:

[DelimitedRecord(",")]
[IgnoreEmptyLines()]
[ConditionalRecord(RecordCondition.ExcludeIfMatchRegex, "^,+$")]
public class FormItemExport
{
[FieldQuoted(‘"’, QuoteMode.OptionalForBoth)]
[FieldNullValue(typeof(string), "")]
public string Id;

[FieldQuoted(‘"’, QuoteMode.OptionalForBoth)]
[FieldNullValue(typeof(string), "")]
public string FirstName;

[FieldQuoted(‘"’, QuoteMode.OptionalForBoth)]
[FieldNullValue(typeof(string), "")]
public string LastName;

[FieldQuoted(‘"’, QuoteMode.OptionalForBoth)]
[FieldNullValue(typeof(string), "")]
public string Email;

[FieldQuoted(‘"’, QuoteMode.OptionalForBoth)]
[FieldNullValue(typeof(string), "")]
public string RewardsCardHolder;

[FieldQuoted(‘"’, QuoteMode.OptionalForBoth)]
[FieldNullValue(typeof(string), "")]
public string CreateDatetime;

}

public class Utils
{
public delegate V ExportFill<T, V>(T inptOj);

public static FormItemExport FillForm(WscLandingFormEntity le)
{
FormItemExport lre = new FormItemExport();

lre.Id = le.Id.ToString();
lre.FirstName = le.FirstName;
lre.LastName = le.LastName;
lre.Email = le.Email;

if (le.RewardsCardHolder)
{
lre.RewardsCardHolder = "Yes";
}
else
{
lre.RewardsCardHolder = "No";
}

lre.CreateDatetime = le.CreateDatetime.ToString("MM/dd/yyyy hh:mm:ss");

return lre;
}

public static void Export<T, V>(EntityCollectionBase<T> ec, ExportFill<T, V> expF, string filename, string[] fieldsHeaders) where T : EntityBase
{
string delimiter = ",";
string qualifier = """;

List<V> recordCollection = new List<V>();
foreach (T obj in ec)
{
recordCollection.Add(expF(obj));
}

MemoryStream ms = new MemoryStream();
StreamWriter sw = new StreamWriter(ms);
ms.Position = 0;

FileHelperEngine engine = new FileHelperEngine(typeof(V));
V[] erc = recordCollection.ToArray();

StringBuilder strOut = new StringBuilder();

for (int i = 0; i < fieldsHeaders.Length; i++)
{
strOut.Append(qualifier);
strOut.Append(fieldsHeaders[i]);
strOut.Append(qualifier);
if (i < fieldsHeaders.Length – 1)
strOut.Append(delimiter);
}

engine.HeaderText = strOut.ToString();
engine.WriteStream(sw, erc);

HttpResponse response = HttpContext.Current.Response;
response.Clear();
response.AddHeader("content-disposition", "attachment; filename=" + filename);
response.ContentType = "application/vnd.ms-excel";

sw.Flush();
ms.Flush();
ms.WriteTo(response.OutputStream);

ms.Close();
response.Flush();
response.End();
}
}

 导出时:

Utils.Export < MclCommercialContactEntity, CommercialContactExport>
 (list,new Utils.ExportFill<MclCommercialContactEntity,CommercialContactExport>(CommercialContactExport.ExportCommercialContacts), "Commercial Contacts.csv",
new string[] { "Organization", "First Name", "Last Name", "Address", "City", "Province", "Country", "Postal Code", "Phone Number", "Alternate Phone", "Email" });

 

转载于:https://www.cnblogs.com/zeroone/p/4419551.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
inp格式文件转换为unv格式的方法与其他格式文件的转换类似,主要涉及读取和编写文件的过程。以下是一个基本的C#类库示例,用于将inp格式文件转换为unv格式: ``` using System.IO; using System.Collections.Generic; using FileHelpers; public class InpToUnvConverter { public static string Convert(string inpFilePath) { // Read the inp file data using FileHelpers var engine = new DelimitedFileEngine<InpRecord>(); var inpData = engine.ReadFile(inpFilePath); // Convert the inp data to unv format var unvData = new List<UnvRecord>(); // Loop over the inp data and convert each record to unv format foreach (var inpRecord in inpData) { var unvRecord = new UnvRecord(); // Convert the record data to unv format unvRecord.Id = inpRecord.Id; unvRecord.X = inpRecord.X; unvRecord.Y = inpRecord.Y; unvRecord.Z = inpRecord.Z; unvRecord.Name = inpRecord.Name; // Add the unv record to the list unvData.Add(unvRecord); } // Write the unv data to a new file var unvFilePath = Path.ChangeExtension(inpFilePath, ".unv"); var unvEngine = new DelimitedFileEngine<UnvRecord>(); unvEngine.WriteFile(unvFilePath, unvData); // Return the path to the new unv file return unvFilePath; } } [DelimitedRecord(",")] public class InpRecord { public int Id; public double X; public double Y; public double Z; public string Name; } [DelimitedRecord(",")] public class UnvRecord { public int Id; public double X; public double Y; public double Z; public string Name; } ``` 此示例假定inp文件中包含ID、X、Y、Z和名称字段。您需要根据自己的文件格式和要求进行修改和扩展。 请注意,此示例使用FileHelpers库来读取和编写文件。您可以使用其他库或手动读取和编写文件,具体取决于您的需求和偏好。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值