DBF文件读取与写入程序设计
下面介绍使用程序来读取和写入DBF文件:
首先新建一个窗体项目,各主要控件如下图所示:
读取DBF文件
读取DBF文件的内容并写到一个DataGridView控件中,采用如下函数:
/// <summary>
/// 读取DBF文件
/// </summary>
/// <param name="filepath">DBF文件路径</param>
/// <param name="dataGridView">DataGridView控件</param>
private void ReadDBFFile(string filepath, DataGridView dataGridView)
{
try
{
dataGridView.Rows.Clear();
dataGridView.Columns.Clear();
//打开文件
FileStream fs = new FileStream(filepath, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
//跳过前面4个
br.ReadBytes(4);
//文件中的记录条数,即行数
int rowCount = br.ReadInt32();//4
//文件头的长度
int headLength = br.ReadInt16();//2
//每行的长度
int rowLength = br.ReadInt16();//2
//计算字段数目
int columnCount = (headLength - 33) / 32;
//跳过32-4-4-2-2=20个,直接到32
br.ReadBytes(20);
int i, j;
byte[] tempBytes = null;
byte tempByte;
int[] fieldLength = null;
//添加表格的列
if (columnCount > 0)
{
string tempColumnName;
fieldLength = new int[columnCount];
for (i = 0; i < columnCount; i++)
{
//读取前11个,为列名称
tempBytes = br.ReadBytes(11);
tempColumnName = ConvertBytesToString(tempBytes);
dataGridView.Columns.Add(tempColumnName, tempColumnName);
//跳过5个
br.ReadBytes(5);
//获取字段长度
tempByte = br.ReadByte();
fieldLength[i] = (int)tempByte;
//跳过剩下的字节,有32-11-5-1=15个
br.ReadBytes(15);
}
}
//获取文件头的最后一个字节,值应该为0D
tempByte = br.ReadByte();
if (tempByte == 0x0D)
{
//添加表格的行及数据
if (rowCount > 0)
{
string tempStr;
for (i = 0; i < rowCount; i++)
{
dataGridView.Rows.Add();
//每行数据中第一个20,跳过
br.ReadByte();
for (j = 0; j < columnCount; j++)
{
tempBytes = br.ReadBytes(fieldLength[j]);
tempStr = ConvertBytesToString(tempBytes);
dataGridView[j, i].Value = tempStr;
}
}
}
}
else
{
MessageBox.Show("文件头定义出现错误!",
"提示!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
br.Close();
fs.Close();
}
catch (Exception except)
{
MessageBox.Show(except.Message,
"提示!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
/// <summary>
/// 将字节组转换成为字符串
/// </summary>
/// <param name="tempBytes">字节数组</param>
/// <returns>DataGridView控件</returns>
public static string ConvertBytesToString(byte[] tempBytes)
{
string result = System.Text.Encoding.GetEncoding("gb2312").GetString(tempBytes);
return result;
}
/// <summary>
/// 将字节组转换成为整型
/// </summary>
/// <param name="tempBytes">字节数组</param>
/// <returns></returns>
public static Int32 ConvertBytesToInt32(byte[] tempBytes)
{
Int32 result = System.BitConverter.ToInt32(tempBytes, 0);
return result;
}
所以,在按钮“读取DBF文件button1”的单击事件添加调用上面读取DBF文件的代码:
private void 读取DBF文件button1_Click(object sender, EventArgs e)
{
if (File.Exists(filepathtextBox1.Text))
{
ReadDBFFile(filepathtextBox1.Text, dataGridView1);
}
}
写入DBF文件
同样,编写用于存储DBF文件的函数,如下:
/// <summary>
/// 保存DBF文件
/// </summary>
/// <param name="filepath">DBF文件保存路径</param>
/// <param name="dataGridView">表格对象</param>
private void WriteDBFFile(string filepath, DataGridView dataGridView)
{
try
{
int rowCount = dataGridView.RowCount;
int columnCount = dataGridView.ColumnCount;
if (rowCount > 0 && columnCount > 0)
{
FileStream fs = new FileStream(filepath, FileMode.Create, FileAccess.Write);
BinaryWriter bw = new BinaryWriter(fs);
byte tempByte;
byte[] tempBytes;
int i, j, tempInt;
int[] fieldLength = new int[columnCount];
int tempMax = 0;
for (i = 0; i < columnCount; i++)
{
tempMax = 0;
for (j = 0; j < rowCount; j++)
{
if (dataGridView[i, j].Value != null
&& dataGridView[i, j].Value.ToString() != "")
{
if (dataGridView[i, j].Value.ToString().Length > tempMax)
{
tempMax = dataGridView[i, j].Value.ToString().Length;
}
}
}
fieldLength[i] = tempMax * 2;
}
//写入文件头
{
//第0个字节为数据库类型
tempByte = 0x03;
bw.Write(tempByte);
tempBytes = new byte[3];
//第1个字节为保存时的年份-1990
tempInt = DateTime.Now.Year - 1900;
tempBytes[0] = System.Convert.ToByte(tempInt);
//第2个字节为保存时的月份
tempBytes[1] = System.Convert.ToByte(DateTime.Now.Month);
//第3个字节为保存时的日
tempBytes[2] = (byte)(DateTime.Now.Day);
bw.Write(tempBytes);
//第4-7个字节为行数
bw.Write(rowCount);
//第8-9字节为文件头的长度
tempInt = 33 + columnCount * 32;
bw.Write((Int16)tempInt);
//第10-11为每行数据所占长度
tempInt = 1;
for (i = 0; i < columnCount; i++)
{
tempInt += fieldLength[i];
}
bw.Write((Int16)tempInt);
//第12-31为保留字段,默认为0
tempBytes = new byte[20];
bw.Write(tempBytes);
string tempColumnName;
//开始写字段
for (i = 0; i < columnCount; i++)
{
tempColumnName = dataGridView.Columns[i].Name;
tempBytes = ConvertStringToBytes(tempColumnName, 11);
bw.Write(tempBytes);
//第11个字节为数据类型
tempByte = (byte)('C');//数据类型为字符串
bw.Write(tempByte);
//第12-15为保留字节
tempBytes = new byte[4];
bw.Write(tempBytes);
//第16个字节为字段长度
tempByte = (byte)fieldLength[i];
bw.Write(tempByte);
//接着第17-31都为保留字节
tempBytes = new byte[15];
bw.Write(tempBytes);
}
//最后以0D结尾
tempByte = 0x0D;
bw.Write(tempByte);
}
object tempValue;
//写入单元格数据
for (i = 0; i < rowCount; i++)
{
//每一行第一个字节默认为20
tempByte = 0x20;
bw.Write(tempByte);
for (j = 0; j < columnCount; j++)
{
tempValue = dataGridView[j, i].Value;
if (tempValue != null)
{
tempBytes = ConvertStringToBytes(tempValue.ToString(), fieldLength[j]);
bw.Write(tempBytes);
}
else
{
tempBytes = ConvertStringToBytes("", fieldLength[j]);
bw.Write(tempBytes);
}
}
}
bw.Close();
fs.Close();
}
else
{
MessageBox.Show("表格中没有数据",
"提示!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
catch (Exception except)
{
MessageBox.Show(except.Message,
"提示!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
/// <summary>
/// 将字符串转换成为字节数组
/// </summary>
/// <param name="tempStr"></param>
/// <param name="limitLength"></param>
/// <returns></returns>
public static byte[] ConvertStringToBytes(string tempStr,int limitLength)
{
byte[] result = null;
byte[] tempBytes = UTF8Encoding.GetEncoding("gb2312").GetBytes(tempStr);
if (tempBytes.Length == limitLength)
{
result = tempBytes;
}
else if (tempBytes.Length > limitLength)
{
for (int i = 0; i < limitLength; i++)
{
result[i] = tempBytes[i];
}
}
else if (tempBytes.Length < limitLength)
{
result = new byte[limitLength];
for (int i = 0; i < tempBytes.Length; i++)
{
result[i] = tempBytes[i];
}
}
return result;
}
同样,在按钮“保存DBF文件button1”的Click事件中添加调用上面的函数:
private void 保存DBF文件button1_Click(object sender, EventArgs e)
{
SaveFileDialog saveDg = new SaveFileDialog();
saveDg.Title = "保存DBF文件";
saveDg.Filter = "DBF文件(*.dbf)|*.dbf|所有文件(*.*)|*.*";
if (saveDg.ShowDialog() == DialogResult.OK)
{
WriteDBFFile(saveDg.FileName, dataGridView1);
}
}
分享到:
相关推荐
虚拟机磁盘文件如何在宿主机系统上读取文件的方法虚拟机磁盘文件如何在宿主机系统上读取文件的方法
DNF 清理游戏更新残留文件,和更新安装之后的一些无用文件。一键清理。。。。。。。。。
读写8类。。读写DNF驱动。。。达到显现图标作用。。。
易语言DNF配置文件注入DLL例子,最新稳定注入方式。可以替代输入法注入。
先打开解决驱动蓝屏 初始化一下 重启电脑 再打开解决驱动蓝屏 点一下 重启初始化 OK了 打开驱动加载 把驱动文件安装 启动 上游戏打开OD就可以看见DNF文件了
清除垃圾滴一个文件
DNF上不去的时候使用的工具,用一下即可,方便实用,可以清理垃圾清理缓存
DNF配置文件 - /etc/dnf/dnf.conf;配置DNF - 修改配置参数;创建本地软件源仓库;添加软件源;启用和禁用软件源;管理软件包;列出软件包清单;显示RPM包信息;下载、安装和删除RPM包;管理软件包组;显示软件包组信息;安装和...
dellb1265dnf驱动是专为黑白激光多功能一体机B1265dnf打造的打印机驱动程序,打印机要想正常的与电脑连接,进行打印、复印和扫描等工作,就需要安装此驱动程序,欢迎有需要的朋友下载使用!官方介绍戴尔B1265dnf,...
有朋友玩DNF的时候提示TenSLX.dll出错或者缺损的,可以下载,解压后覆盖到游戏目录\start\TenProtect下就可以进游戏了
鼠标指到物品就显示该物品的代码
DNF 过DNF保护 VC编程 VC编程过DNF保护
首先进入游戏,把鼠标放在想要获取代码的装备上,在软件的窗口里就可以看到代码了,这个小程序无毒,请放心使用!~
DNF隐藏器(dnf窗口化快捷键)V2012 经典版.zip 不想用什么助手之类的,可以用这个简单的。
一个简单的dnf横版游戏的实现,有兴趣的童鞋可以参考哦。 一个简单的dnf横版游戏的实现,有兴趣的童鞋可以参考哦。 一个简单的dnf横版游戏的实现,有兴趣的童鞋可以参考哦。 一个简单的dnf横版游戏的实现,有兴趣...
Zipios是一个用于读取和写入zip文件的小型C ++库。 结构和公共接口(有点宽松)基于java.util.zip包。 为访问zip文件中的各个条目而创建的流基于标准iostream库。 Zipios还为应用程序提供了一种透明地支持来自多个...
DNF Extractor修改DNF背景图片与时装.docx
自制的图像合成程序,可以将动画身体和衣服合成为bmp的融合文件
DNF驱动图标过TP下段找检测,找功能。
这是我自己用易语言制作出来的软件 大家可以看看 查询您是否封号用 DNF 查询是否封号 DNF 查询是否封号