public QuickDBF(string FileName, DataTable DTStruct) { strFileName = FileName; //根据数据结构信息创建数据结构组 DictColomns.Clear(); Int32 offset = 1; for (int i = 0; i < DTStruct.Rows.Count; i++) { DBFColumnInfo colInfo = new DBFColumnInfo(); colInfo.ColName = (string)DTStruct.Rows[i]["FIELD_NAME"]; colInfo.colType = 'C'; //目前中国结算实时接口所有字段均为字符型,暂时不做处理 colInfo.colLength = byte.Parse((string)DTStruct.Rows[i]["FIELD_LENGTH"]); colInfo.colOffset = offset; //第一个字段偏移从1开始 偏移0为记录删除标志 offset = offset + colInfo.colLength; //累加计算下一个字段的偏移,最后一次执行完以后该值就是记录长度 DictColomns.Add(colInfo.ColName, colInfo); } DBFh.FileType = 0x03; DBFh.LastModDate = new byte[3]; DBFh.LastModDate[0] = (byte)(DateTime.Now.Year - 2000); //最后修改日期三个字段分别放年月日,其中年只放后两位,嗯哼,千年虫问题。 DBFh.LastModDate[1] = (byte)(DateTime.Now.Month); DBFh.LastModDate[2] = (byte)(DateTime.Now.Day); DBFh.RowCount = 0; //初始化的时候暂不知数据量 DBFh.HeadLength = (short)((DictColomns.Count + 1) * 32 + 1); //头的长度=头信息32字节+ 每个字段32字节 + 一个字节的结束标志 DBFh.RowLength = (short)offset; }
/// <summary> /// 获取DBF文件格式 /// </summary> /// <returns></returns> private void GetDBFStru() { try { //开启文件 FileStream Reader = new FileStream(strFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); // //获取DBF文件头 Byte[] ReadBuffer = new Byte[32]; Reader.Read(ReadBuffer, 0, ReadBuffer.Length); //读取前32个字节文件头部 object DBFhType = DBFh; //装箱处理,将结构体变成引用类型 ByteArrayToStructure(ReadBuffer, ref DBFhType, 0); DBFh = (DBFHead)DBFhType; //拆箱 //获取DBF文件字段集合 DBFColumnInfo DBFc = new DBFColumnInfo(); long ColumnCount = (DBFh.HeadLength - 1 - 32) / 32; //DBF字段数等于头总长减去1(头部结束标志符 0x0D)再减去DBF信息数据块的32个字节,然后每个字段信息32个字节 DictColomns.Clear(); //清空 for (int i = 0; i < ColumnCount; i++) { Reader.Read(ReadBuffer, 0, ReadBuffer.Length); //逐个读取字段信息 DBFhType = DBFc; //装箱 ByteArrayToStructure(ReadBuffer, ref DBFhType, 0); DBFc = (DBFColumnInfo)DBFhType; //拆箱 DictColomns.Add(DBFc.ColName, DBFc); } Reader.Close(); } catch (Exception ex) { Log.writelog(ex.Message); MessageBox.Show("DBF文件处理错误" + ex.Message); } }