简体   繁体   English

FileHelpers动态固定字段位置

[英]FileHelpers dynamic fixed field positions

Is it possible to parse/read a fixed position layout flat file when the positions are only known at run time? 当仅在运行时知道位置时,可以解析/读取固定位置布局平面文件吗? I see there is a question on SO that relates to something similar using 'Runtime Records' but this relates reading a delimited file - Dynamically create a Fixed Length text file with FileHelpers 我看到有一个关于SO的问题,该问题与使用“运行时记录”相似,但这与读取定界文件有关- 使用FileHelpers动态创建固定长度的文本文件

My intention is to parse fixed length flat files with different fixed length formats to a common format, and the fixed length format is loaded at runtime. 我的意图是将具有不同固定长度格式的固定长度平面文件解析为通用格式,并在运行时加载固定长度格式。 FileHelpers tends to use attributes to define the position and length of the field, which is defined up front but I would like to specify this at runtime. FileHelpers倾向于使用属性来定义字段的位置和长度,这是在前面定义的,但是我想在运行时指定。

thanks. 谢谢。

This is what i did. 这就是我所做的。 Hope it may help you. 希望对您有所帮助。

  1. Created DB table and stored the different Fixed File layout formats (like Field Name size, Data Type and Length of the field etc...) 创建数据库表并存储不同的固定文件布局格式(如字段名称大小,数据类型和字段长度等)
  2. In UI first user selects the Fixed File Layout and then selects the Fixed File to import. 在UI中,第一个用户选择“固定文件布局”,然后选择要导入的固定文件。
  3. Based on selected Layout i create Fixed length Type class at run time using the below code. 基于选定的布局,我在运行时使用以下代码创建固定长度类型类。

private void ImportFiles() { DataTable dt = GetFixedFileSpecsFromDB(layoutName); //Get the specs //Create Dynamic class based on Specs above FixedLengthClassBuilder cb = GetSpecClass(dt); //Create FileHelpers engine instance FileHelperEngine engine = new FileHelperEngine(cb.CreateRecordClass()); //Read file data into Data table DataTable dtImportData = engine.ReadFileAsDT(ImportFilePath); } //Method to create the Fixed lentgh dynamic class public static FixedLengthClassBuilder GetSpecClass(DataTable dt) { string className = "ImportSpecifications"; FixedLengthClassBuilder cb = new FixedLengthClassBuilder(className); //Loop thru each field and prepare the class foreach (DataRow dr in dt.Rows) { int fieldLength = Convert.ToInt32(dr.Field<decimal>("FieldLength")); switch (dr["FieldDataType"].ToString()) { case "String": cb.AddField(dr.Field<string>("FieldName"), fieldLength, typeof(string)); cb.LastField.FieldNullValue = string.Empty; cb.LastField.TrimMode = FileHelpers.TrimMode.Both; break; case "Date": cb.AddField(dr.Field<string>("FieldName"), fieldLength, typeof(DateTime)); cb.LastField.FieldNullValue = string.Empty; break; case "Integer": cb.AddField(dr.Field<string>("FieldName"), fieldLength, typeof(int?)); //cb.LastField.FieldNullValue = 0; break; case "Long Integer": cb.AddField(dr.Field<string>("FieldName"), fieldLength, typeof(long)); cb.LastField.FieldNullValue = 0; break; case "Decimal": cb.AddField(dr.Field<string>("FieldName"), fieldLength, typeof(decimal?)); //cb.LastField.FieldNullValue = 0; break; default: break; } } return cb; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM