读取DBF文件的部分代码

        private void BtnOpenInitial_Click(object sender, EventArgs e)
        {
            OpenFileDialog file = new OpenFileDialog();
            if (file.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string path = file.FileName;
                DTable dbf  = new DTable();
                dbf.Load(path);
                if (dbf.Table.Rows.Count > 0)
                {
                    this.GrcInitialData.DataSource = dbf.Table;
                    _table = new DataTable();
                    _table = dbf.Table;
                }
            }
        }

	/// <summary>
        /// 从硬盘读取dbf文件
        /// </summary>
        /// <param name="filepath">路径</param>
        /// <returns></returns>
        public int Load(String filepath)
        {
            FileInfo FFile = new FileInfo(filepath);
            if (FFile.Exists)
            {
                Stream fStream = new BufferedStream(new FileStream(filepath, 

FileMode.Open));//main.dbf
                try
                {
                    BinaryReader bReader = new BinaryReader(fStream);
                    //ByteDump(fStream);
                    return ReadDBF(bReader);
                }
                catch
                {
                    ClearDataTable();
                    return -1;
                }
                finally
                {
                    fStream.Close();
                }
            }
            else
            {
                return -1;
            }
        }

        /// <summary>
        /// 读取dbf
        /// </summary>
        /// <param name="bReader"></param>
        /// <returns></returns>
        private int ReadDBF(BinaryReader bReader)
        {
            if (ReadHeader(bReader) == 0)
            {
                if (ReadFieldDesc(bReader) == 0)
                {
                    return readRecord(bReader);
                }
                else
                    return -1;
            }
            else
            {
                return -1;
            }
        }

        /// <summary>
        /// 读取记录
        /// </summary>
        /// <param name="bReader"></param>
        /// <returns></returns>
        private int readRecord(BinaryReader bReader)
        {
            try
            {
                int iRecCount = _hearder.RecordCount;
                for (int i = 0; i < iRecCount; i++)
                {
                    DRow row = new DRow();
                    DataRow datarow = _table.NewRow();
                    row.RowIndex = i;

                    byte bRecordFlag = bReader.ReadByte();

                    if (bRecordFlag == 0x20)
                    {
                        row.DeleteFlag = false;
                    }
                    else if (bRecordFlag == 0x2a)
                    {
                        row.DeleteFlag = true;
                    }
                    else
                    {
                        ClearDataTable();
                        return -1;
                    }

                    for (short j = 0; j < _columns.ColumnCount; j++)
                    {
                        DColumn col = _columns.GetColumnByIndex(j);
                        if (col != null)
                        {

                            DField field = new DField();
                            field.Column = col;
                            byte[] bValue = bReader.ReadBytes(col.ColumnLength);
                            field.FieldValue = Encoding.Default.GetString(bValue);
                            row.AddField(field);
                            UpdateRowField(datarow, field);
                        }
                        else
                        {
                            return -1;
                        }
                    }
                    _rows.AddRow(row);
                    if (!row.DeleteFlag)
                        _table.Rows.Add(datarow);
                }

                //if (_hearder.RecordCount > 0)
                //{
                //    byte endflag = bReader.ReadByte();
                //    if (endflag != 0x1A)
                //    {
                //        ClearDataTable();
                //        return -1;
                //    }
                //}
                return 0;
            }
            catch
            {
                ClearDataTable();
                return -1;
            }
        }
时间: 2024-08-29 21:50:41

读取DBF文件的部分代码的相关文章

PHP读取超大文件的实例代码

数据量大带来的问题就是单个文件很大,能够打开这个文件相当不容易,记事本就不要指望了,果断死机 去年年底的各种网站帐号信息的数据库泄漏,很是给力啊,趁机也下载了几个数据库,准备学学数据分析家来分析一下这些帐号信息.虽然这些数据信息都已经被“整理”过的,不过自己拿来学习也挺有用的,毕竟有这么大的数据量. 数 据量大带来的问题就是单个文件很大,能够打开这个文件相当不容易,记事本就不要指望了,果断死机.用MSSQL的客户端也打不开这么大的SQL文件,直接 报内存不足,原因据说是MSSQL在读取数据的时候

(实用篇)PHPExcel读取Excel文件的实现代码

用PHPExcel读取Excel 2007 或者Excel2003文件,需要的朋友,可以参考下. 涉及知识点:  php对excel文件进行循环读取 php对字符进行ascii编码转化,将字符转为十进制数 php对excel日期格式读取,并进行显示转化 php对汉字乱码进行编码转化 <?php require_once 'PHPExcel.php'; /**对excel里的日期进行格式转化*/ function GetData($val){ $jd = GregorianToJD(1, 1, 1

PHPExcel读取Excel文件的实现代码

<?php require_once 'PHPExcel.php'; /**对excel里的日期进行格式转化*/ function GetData($val){ $jd = GregorianToJD(1, 1, 1970); $gregorian = JDToGregorian($jd+intval($val)-25569); return $gregorian;/**显示格式为 “月/日/年” */ } $filePath = 'test.xlsx'; $PHPExcel = new PHP

CPP读取dbf文件

prop系统导出的交收数据为dbf文件格式,linux需要能够解析该格式文件, 找到一个开源库shapelib可以实现该功能: 1.下载库源码 http://download.osgeo.org/shapelib/ 选择shapelib-1.4.0.zip这个版本: 2.解压下载的源码,编译, [[email protected] dbf_demo]$ ./configure --prefix=/home/fm/dbf_demo/shapelib 此时会在源码目录生成MakeFile文件,注意修

java读取dbf文件

1.下载DBFReader jar包 2.实例代码 package service; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.sql.Connection; import java.sql

读取DBF文件数据

#region 返回DBF表 public static System.Data.DataTable getDTFromDBF(string fullPath) { string pDir = System.IO.Path.GetDirectoryName(fullPath); string pFile = System.IO.Path.GetFileNameWithoutExtension(fullPath); return getDTFromDBF(pDir, pFile); } publi

新手上路之DBF文件的读取

初次了解DBF文件,因为有需求要将其中的数据导出,并插入到数据库中.开始的时候用Excel把它打开了,以为它就是一个Excel文件,就想着用NPOI来实现,显然是作为一个新人太天真了,后来在别人的博客上了解到,读取这个文件有多种方式,根据不同的难易用不同的方法,由于我所接触的这个文件中没有过多的约束,我也就用了最简单的一种. /// <summary> /// 读取DBF文件,此方法适用于简单的DBF文件,即类似深交所的文件 /// </summary> /// <param

shp系列(六)——利用C++进行Dbf文件的写(创建)

上一篇介绍了shp文件的创建,接下来介绍dbf的创建. 推荐结合读取dbf的博客一起看! 推荐结合读取dbf的博客一起看! 推荐结合读取dbf的博客一起看! 1.Dbf头文件的创建 Dbf头文件的结构如下: 记录项数组说明: 字段类型说明: 关于每项的具体含义参照读取dbf文件的解释,这里重点解释几项: HeaderByteNum指dbf头文件的字节数,数值不用除于2,具体为:从version到Reserved2(共32) + n个字段 * 每一个字段长度 32 + terminator. Re

net core封装DBF文件处理

前言 在.net core项目中需要读写dbf文件,可以使用FastDBF操作dbf文件,十分方便,使用Nuget可以添加FastDBF 代码实现 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 = t