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常见限流算法具体实现

《通俗易懂的Java常见限流算法具体实现》:本文主要介绍Java常见限流算法具体实现的相关资料,包括漏桶算法、令牌桶算法、Nginx限流和Redis+Lua限流的实现原理和具体步骤,并比较了它们的... 目录一、漏桶算法1.漏桶算法的思想和原理2.具体实现二、令牌桶算法1.令牌桶算法流程:2.具体实现2.1

Python使用Pandas对比两列数据取最大值的五种方法

《Python使用Pandas对比两列数据取最大值的五种方法》本文主要介绍使用Pandas对比两列数据取最大值的五种方法,包括使用max方法、apply方法结合lambda函数、函数、clip方法、w... 目录引言一、使用max方法二、使用apply方法结合lambda函数三、使用np.maximum函数

MySQL8.0设置redo缓存大小的实现

《MySQL8.0设置redo缓存大小的实现》本文主要在MySQL8.0.30及之后版本中使用innodb_redo_log_capacity参数在线更改redo缓存文件大小,下面就来介绍一下,具有一... mysql 8.0.30及之后版本可以使用innodb_redo_log_capacity参数来更改

C++使用栈实现括号匹配的代码详解

《C++使用栈实现括号匹配的代码详解》在编程中,括号匹配是一个常见问题,尤其是在处理数学表达式、编译器解析等任务时,栈是一种非常适合处理此类问题的数据结构,能够精确地管理括号的匹配问题,本文将通过C+... 目录引言问题描述代码讲解代码解析栈的状态表示测试总结引言在编程中,括号匹配是一个常见问题,尤其是在

Java实现检查多个时间段是否有重合

《Java实现检查多个时间段是否有重合》这篇文章主要为大家详细介绍了如何使用Java实现检查多个时间段是否有重合,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录流程概述步骤详解China编程步骤1:定义时间段类步骤2:添加时间段步骤3:检查时间段是否有重合步骤4:输出结果示例代码结语作

使用C++实现链表元素的反转

《使用C++实现链表元素的反转》反转链表是链表操作中一个经典的问题,也是面试中常见的考题,本文将从思路到实现一步步地讲解如何实现链表的反转,帮助初学者理解这一操作,我们将使用C++代码演示具体实现,同... 目录问题定义思路分析代码实现带头节点的链表代码讲解其他实现方式时间和空间复杂度分析总结问题定义给定

Java覆盖第三方jar包中的某一个类的实现方法

《Java覆盖第三方jar包中的某一个类的实现方法》在我们日常的开发中,经常需要使用第三方的jar包,有时候我们会发现第三方的jar包中的某一个类有问题,或者我们需要定制化修改其中的逻辑,那么应该如何... 目录一、需求描述二、示例描述三、操作步骤四、验证结果五、实现原理一、需求描述需求描述如下:需要在

如何使用Java实现请求deepseek

《如何使用Java实现请求deepseek》这篇文章主要为大家详细介绍了如何使用Java实现请求deepseek功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1.deepseek的api创建2.Java实现请求deepseek2.1 pom文件2.2 json转化文件2.2

Java调用DeepSeek API的最佳实践及详细代码示例

《Java调用DeepSeekAPI的最佳实践及详细代码示例》:本文主要介绍如何使用Java调用DeepSeekAPI,包括获取API密钥、添加HTTP客户端依赖、创建HTTP请求、处理响应、... 目录1. 获取API密钥2. 添加HTTP客户端依赖3. 创建HTTP请求4. 处理响应5. 错误处理6.

python使用fastapi实现多语言国际化的操作指南

《python使用fastapi实现多语言国际化的操作指南》本文介绍了使用Python和FastAPI实现多语言国际化的操作指南,包括多语言架构技术栈、翻译管理、前端本地化、语言切换机制以及常见陷阱和... 目录多语言国际化实现指南项目多语言架构技术栈目录结构翻译工作流1. 翻译数据存储2. 翻译生成脚本