net core封装DBF文件处理

时间:2021-12-14 07:36:37

前言

 在.net core项目中需要读写dbf文件,可以使用FastDBF操作dbf文件,十分方便,使用Nuget可以添加FastDBF

net core封装DBF文件处理

 

代码实现

  1  public class DBFUtil
  2     {
  3         private static List<KeyValuePair<string, PropertyInfo>> Mapping<T>() where T : class
  4         {
  5             Type type = typeof(T);
  6             var properties = type.GetProperties();
  7 
  8             List<KeyValuePair<string, PropertyInfo>> result = new List<KeyValuePair<string, PropertyInfo>>(); ;
  9             //保存dbf字段和实体的映射关系
 10             //实体没有ColumnAttribute,则认为dbf字段名称和实体名称一致
 11             foreach (var pro in properties)
 12             {
 13                 var attrs = pro.GetCustomAttributes(typeof(ColumnAttribute), false);
 14                 if (attrs == null)
 15                 {
 16                     result.Add(new KeyValuePair<string, PropertyInfo>(pro.Name, pro));
 17                 }
 18                 else
 19                 {
 20                     ColumnAttribute ar = (ColumnAttribute)attrs[0];
 21                     if (ar == null)
 22                     {
 23                         result.Add(new KeyValuePair<string, PropertyInfo>(pro.Name, pro));
 24 
 25                     }
 26                     else
 27                     {
 28                         result.Add(new KeyValuePair<string, PropertyInfo>(ar.Name, pro));
 29                     }
 30                 }
 31             }
 32             return result;
 33         }
 34 
 35 
 36         /// <summary>
 37         /// 导出dbf
 38         /// </summary>
 39         /// <typeparam name="T"></typeparam>
 40         /// <param name="list">列表数据</param>
 41         /// <param name="dbfPath">dbf保存路径</param>
 42         /// <returns></returns>
 43         public static string WritePdf<T>(List<T> list, string dbfPath) where T : class
 44         {
 45             if (list == null)
 46             {
 47                 return null;
 48             }
 49             try
 50             {
 51                 var dicProperty = Mapping<T>();
 52                 //创建dbf文件
 53                 var odbf = new DbfFile(Encoding.Default);
 54                 odbf.Open(Path.Combine(dbfPath), FileMode.Create);
 55                 //create a header
 56                 foreach (var it in dicProperty)
 57                 {
 58                     odbf.Header.AddColumn(new DbfColumn(it.Key, DbfColumn.DbfColumnType.Character, 255, 0));
 59                 }
 60 
 61                 foreach (var it in list)
 62                 {
 63                     var orec = new DbfRecord(odbf.Header) { AllowDecimalTruncate = true };
 64                     foreach (var col in dicProperty)
 65                     {
 66                         var pro = col.Value;
 67                         object value = pro.GetValue(it);
 68                         if (value == null || value == DBNull.Value)
 69                         {
 70                             value = "";
 71                         }
 72                         orec[col.Key] = value.ToString();
 73                     }
 74                     odbf.Write(orec, true);
 75                 }
 76 
 77                 odbf.WriteHeader();
 78 
 79                 odbf.Close();
 80                 return dbfPath;
 81 
 82             }
 83             catch (Exception ex)
 84             {
 85                 throw ex;
 86             }
 87 
 88 
 89         }
 90         /// <summary>
 91         /// 读取dbf文件
 92         /// </summary>
 93         /// <typeparam name="T"></typeparam>
 94         /// <param name="dbfPath">dbf文件路径</param>
 95         /// <returns></returns>
 96         public static List<T> ReadPdf<T>(string dbfPath) where T : class
 97         {
 98             if (!File.Exists(dbfPath))
 99             {
100                 return null;
101             }
102             List<T> list = new List<T>();
103 
104             try
105             {
106                 var dicProperty = Mapping<T>();
107                 //获取一个DBF文件对象
108                 DbfFile dbf = new DbfFile(Encoding.Default);
109                 dbf.Open(dbfPath, FileMode.Open);
110 
111                 //读取dbf的字段名
112                 DbfHeader dh = dbf.Header;
113                 List<string> dbfColumns = new List<string>();
114                 for (int index = 0; index < dh.ColumnCount; index  )
115                 {
116                     dbfColumns.Add(dh[index].Name);
117                 }
118                 //读取数据
119                 int i = 0;
120                 while (dbf.Read(i) != null)
121                 {
122                     //读取一行
123                     DbfRecord record = dbf.Read(i);
124                     if (record == null)
125                     {
126                         continue;
127                     }
128                     T t = Activator.CreateInstance<T>();
129                     foreach (var col in dbfColumns)
130                     {
131                         var data = dicProperty.FirstOrDefault(r => r.Key == col);
132                         if (data.Key == null)
133                         {
134                             continue;
135                         }
136                         var pro = data.Value;
137                         if (pro == null || !pro.CanWrite)
138                         {
139                             continue;
140                         }
141                         string value = record[col];
142                         if (value == null)
143                         {
144                             continue;
145                         }
146                         pro.SetValue(t, Convert.ChangeType(value.Trim(), pro.PropertyType));
147                     }
148                     list.Add(t);
149                     i  ;
150                 }
151                 dbf.Close();
152                 return list;
153             }
154             catch (Exception ex)
155             {
156                 throw ex;
157             }
158 
159         }
160     }