DBF文件初步了解(二)——DBF数据导出代码实现

2024-05-12 22:58

本文主要是介绍DBF文件初步了解(二)——DBF数据导出代码实现,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

    上篇博客中主要记录一些关于DBF数据文件的概念性知识。包括DBF的数据结构和用处。在这里记录一下在C#中导出DBF文件的实现方式。
    DBF文件也是一种数据库文件,我们导出DBF文件也就是将数据库中的数据导出到DBF文件中。所以最主要的就是讲DataTable转换成DBF,包括数据和数据类型。
  
<span style="font-size:24px;">private bool WriteDBFfile(string filepath, DataTable dt, List<string> columnNames, string tableName, Dictionary<string, char> nametype = null){#region ***************************************写t**************************************nametype = ConvertColumnType(dt, tableName); ;FileStream fs = null;BinaryWriter bw = null;try{int rowCount = dt.Rows.Count;int columnCount = dt.Columns.Count;//if (rowCount > 0 && columnCount > 0)//{fs = new FileStream(filepath, FileMode.Create, FileAccess.Write);bw = new BinaryWriter(fs);byte tempByte;byte[] tempBytes;int tempInt;int[] fieldLength = new int[columnCount];int tempMax = 0;for (int i = 0; i < columnCount; i++){tempMax = 0;for (int j = 0; j < rowCount; j++){if (dt.Rows[j][i].ToString() != null && dt.Rows[j][i].ToString() != ""){if (dt.Rows[j][i].ToString().Length > tempMax){tempMax = dt.Rows[j][i].ToString().Length;}}}char dbfType = 'C';if (nametype.ContainsKey(columnNames[i].ToLower())){dbfType = nametype[columnNames[i].ToLower()];}if (tableName.StartsWith("F") && dbfType == 'N'){tempMax = 8;}else if (dbfType == 'N'){tempMax = 1;}//数êy字×?类àà型Dí长3¤度è至á少éù13,£?此′?处′|直±接ó设éè置?所ù有óD列áD最×?小D?长3¤度è为a13fieldLength[i] = tempMax * 2 < 13 ? 13 : tempMax * 2;}/tempByte = 0x03;bw.Write(tempByte);tempBytes = new byte[3];tempInt = DateTime.Now.Year - 1900;tempBytes[0] = System.Convert.ToByte(tempInt);tempBytes[1] = System.Convert.ToByte(DateTime.Now.Month);tempBytes[2] = (byte)(DateTime.Now.Day);bw.Write(tempBytes);bw.Write(rowCount);tempInt = 33 + columnCount * 32;bw.Write((Int16)tempInt);tempInt = 1;for (int i = 0; i < columnCount; i++){tempInt += fieldLength[i];}bw.Write((Int16)tempInt);tempBytes = new byte[20];bw.Write(tempBytes);string tempColumnName;for (int i = 0; i < columnCount; i++){tempColumnName = columnNames[i];tempBytes = ConvertStringToBytes(tempColumnName, 11);bw.Write(tempBytes);char dbfType = 'C';if (nametype.ContainsKey(tempColumnName.ToLower())){dbfType = nametype[tempColumnName.ToLower()];}tempByte = (byte)dbfType;//数êy据Y类àà型Dí为a字×?符·?串′?bw.Write(tempByte);//第μú12-15为a保±£留á?字×?节útempBytes = new byte[4];bw.Write(tempBytes);//第μú16个?字×?节ú为a字×?段?长3¤度ètempMax = fieldLength[i];tempByte = (byte)tempMax;bw.Write(tempByte);//接ó着×?第μú17-31都?为a保±£留á?字×?节útempBytes = new byte[15];if (tableName.StartsWith("F") && dbfType == 'N' && tempColumnName.StartsWith("z")){tempBytes[0] = (byte)2;/}bw.Write(tempBytes);}tempByte = 0x0D;bw.Write(tempByte);object tempValue;for (int i = 0; i < rowCount; i++){//每?一ò?行DD第μú一ò?个?字×?节ú默?认è?为a20tempByte = 0x20;bw.Write(tempByte);for (int j = 0; j < columnCount; j++){tempValue = dt.Rows[i][j];if (tempValue != null){double val;if (double.TryParse(tempValue.ToString(), out val) && Equals(val, DbfsjdcViewModel_2015.FXB_DEF)){tempValue = "";}tempBytes = ConvertStringToBytes(tempValue.ToString(), fieldLength[j], true);bw.Write(tempBytes);}else{tempBytes = ConvertStringToBytes("", fieldLength[j], true);bw.Write(tempBytes);}}}}catch (Exception except){MessageBox.Show(except.Message);}finally{if (bw != null){bw.Close();bw.Dispose();}if (fs != null){fs.Close();fs.Dispose();}nametype.Clear();}return System.IO.File.Exists(filepath);#endregion  ***************************************写D′入è?DBF文?件t**************************************}类型转换将String类型转换为Bytes类型方法:private byte[] ConvertStringToBytes(string tempStr, int limitLength, bool needAppendWhiteSpace = false){try{//originaldqdm,sourcelsgxmc,sourcelsgxdm,//org_dqdm,src_lsgxmc,src_lsgxdmswitch (tempStr){case "originaldqdm":tempStr = "org_dqdm";break;case "sourcelsgxmc":tempStr = "src_lsgxmc";break;case "sourcelsgxdm":tempStr = "src_lsgxdm";break;}byte[] tempBytes = UTF8Encoding.GetEncoding("gb2312").GetBytes(tempStr);byte[] result = null;if (tempBytes.Length == limitLength){result = tempBytes;}else if (tempBytes.Length > limitLength){result = new byte[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];}if (needAppendWhiteSpace){for (int i = tempBytes.Length; i < limitLength; i++){result[i] = (byte)32;}}}return result;}catch (Exception except){MessageBox.Show(except.Message);return null;}}提取DataTable 列名,并转化为 Listprivate Dictionary<string, char> ConvertColumnType(DataTable dt, string tableName){Dictionary<string, char> lstType = new Dictionary<string, char>();foreach (DataColumn dc in dt.Columns){string colName = dc.ColumnName;  string colType = dc.DataType.ToString();    char dbfType = 'C';switch (colType){case "System.Int32":case "System.Int64":case "System.Double":case "System.Decimal":case "System.UInt16":case "System.UInt32":case "System.UInt64":case "System.Byte":case "System.SByte":dbfType = 'N';break;case "System.DateTime":dbfType = 'D';break;case "System.Int16":case "System.String":dbfType = 'C';break;default:dbfType = 'C';break;}if (!lstType.ContainsKey(colName.ToLower())){lstType.Add(colName.ToLower(), dbfType);}}return lstType;}</span>

这篇关于DBF文件初步了解(二)——DBF数据导出代码实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/983980

相关文章

java实现docker镜像上传到harbor仓库的方式

《java实现docker镜像上传到harbor仓库的方式》:本文主要介绍java实现docker镜像上传到harbor仓库的方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录1. 前 言2. 编写工具类2.1 引入依赖包2.2 使用当前服务器的docker环境推送镜像2.2

C++20管道运算符的实现示例

《C++20管道运算符的实现示例》本文简要介绍C++20管道运算符的使用与实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录标准库的管道运算符使用自己实现类似的管道运算符我们不打算介绍太多,因为它实际属于c++20最为重要的

Java easyExcel实现导入多sheet的Excel

《JavaeasyExcel实现导入多sheet的Excel》这篇文章主要为大家详细介绍了如何使用JavaeasyExcel实现导入多sheet的Excel,文中的示例代码讲解详细,感兴趣的小伙伴可... 目录1.官网2.Excel样式3.代码1.官网easyExcel官网2.Excel样式3.代码

Java中调用数据库存储过程的示例代码

《Java中调用数据库存储过程的示例代码》本文介绍Java通过JDBC调用数据库存储过程的方法,涵盖参数类型、执行步骤及数据库差异,需注意异常处理与资源管理,以优化性能并实现复杂业务逻辑,感兴趣的朋友... 目录一、存储过程概述二、Java调用存储过程的基本javascript步骤三、Java调用存储过程示

Visual Studio 2022 编译C++20代码的图文步骤

《VisualStudio2022编译C++20代码的图文步骤》在VisualStudio中启用C++20import功能,需设置语言标准为ISOC++20,开启扫描源查找模块依赖及实验性标... 默认创建Visual Studio桌面控制台项目代码包含C++20的import方法。右键项目的属性:

MyBatisPlus如何优化千万级数据的CRUD

《MyBatisPlus如何优化千万级数据的CRUD》最近负责的一个项目,数据库表量级破千万,每次执行CRUD都像走钢丝,稍有不慎就引起数据库报警,本文就结合这个项目的实战经验,聊聊MyBatisPl... 目录背景一、MyBATis Plus 简介二、千万级数据的挑战三、优化 CRUD 的关键策略1. 查

python实现对数据公钥加密与私钥解密

《python实现对数据公钥加密与私钥解密》这篇文章主要为大家详细介绍了如何使用python实现对数据公钥加密与私钥解密,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录公钥私钥的生成使用公钥加密使用私钥解密公钥私钥的生成这一部分,使用python生成公钥与私钥,然后保存在两个文

mysql中的数据目录用法及说明

《mysql中的数据目录用法及说明》:本文主要介绍mysql中的数据目录用法及说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、背景2、版本3、数据目录4、总结1、背景安装mysql之后,在安装目录下会有一个data目录,我们创建的数据库、创建的表、插入的

浏览器插件cursor实现自动注册、续杯的详细过程

《浏览器插件cursor实现自动注册、续杯的详细过程》Cursor简易注册助手脚本通过自动化邮箱填写和验证码获取流程,大大简化了Cursor的注册过程,它不仅提高了注册效率,还通过友好的用户界面和详细... 目录前言功能概述使用方法安装脚本使用流程邮箱输入页面验证码页面实战演示技术实现核心功能实现1. 随机

Golang如何对cron进行二次封装实现指定时间执行定时任务

《Golang如何对cron进行二次封装实现指定时间执行定时任务》:本文主要介绍Golang如何对cron进行二次封装实现指定时间执行定时任务问题,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录背景cron库下载代码示例【1】结构体定义【2】定时任务开启【3】使用示例【4】控制台输出总结背景