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

相关文章

Spring Boot 实现 IP 限流的原理、实践与利弊解析

《SpringBoot实现IP限流的原理、实践与利弊解析》在SpringBoot中实现IP限流是一种简单而有效的方式来保障系统的稳定性和可用性,本文给大家介绍SpringBoot实现IP限... 目录一、引言二、IP 限流原理2.1 令牌桶算法2.2 漏桶算法三、使用场景3.1 防止恶意攻击3.2 控制资源

Python如何去除图片干扰代码示例

《Python如何去除图片干扰代码示例》图片降噪是一个广泛应用于图像处理的技术,可以提高图像质量和相关应用的效果,:本文主要介绍Python如何去除图片干扰的相关资料,文中通过代码介绍的非常详细,... 目录一、噪声去除1. 高斯噪声(像素值正态分布扰动)2. 椒盐噪声(随机黑白像素点)3. 复杂噪声(如伪

springboot下载接口限速功能实现

《springboot下载接口限速功能实现》通过Redis统计并发数动态调整每个用户带宽,核心逻辑为每秒读取并发送限定数据量,防止单用户占用过多资源,确保整体下载均衡且高效,本文给大家介绍spring... 目录 一、整体目标 二、涉及的主要类/方法✅ 三、核心流程图解(简化) 四、关键代码详解1️⃣ 设置

Java Spring ApplicationEvent 代码示例解析

《JavaSpringApplicationEvent代码示例解析》本文解析了Spring事件机制,涵盖核心概念(发布-订阅/观察者模式)、代码实现(事件定义、发布、监听)及高级应用(异步处理、... 目录一、Spring 事件机制核心概念1. 事件驱动架构模型2. 核心组件二、代码示例解析1. 事件定义

Nginx 配置跨域的实现及常见问题解决

《Nginx配置跨域的实现及常见问题解决》本文主要介绍了Nginx配置跨域的实现及常见问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来... 目录1. 跨域1.1 同源策略1.2 跨域资源共享(CORS)2. Nginx 配置跨域的场景2.1

Python中提取文件名扩展名的多种方法实现

《Python中提取文件名扩展名的多种方法实现》在Python编程中,经常会遇到需要从文件名中提取扩展名的场景,Python提供了多种方法来实现这一功能,不同方法适用于不同的场景和需求,包括os.pa... 目录技术背景实现步骤方法一:使用os.path.splitext方法二:使用pathlib模块方法三

CSS实现元素撑满剩余空间的五种方法

《CSS实现元素撑满剩余空间的五种方法》在日常开发中,我们经常需要让某个元素占据容器的剩余空间,本文将介绍5种不同的方法来实现这个需求,并分析各种方法的优缺点,感兴趣的朋友一起看看吧... css实现元素撑满剩余空间的5种方法 在日常开发中,我们经常需要让某个元素占据容器的剩余空间。这是一个常见的布局需求

HTML5 getUserMedia API网页录音实现指南示例小结

《HTML5getUserMediaAPI网页录音实现指南示例小结》本教程将指导你如何利用这一API,结合WebAudioAPI,实现网页录音功能,从获取音频流到处理和保存录音,整个过程将逐步... 目录1. html5 getUserMedia API简介1.1 API概念与历史1.2 功能与优势1.3

Java实现删除文件中的指定内容

《Java实现删除文件中的指定内容》在日常开发中,经常需要对文本文件进行批量处理,其中,删除文件中指定内容是最常见的需求之一,下面我们就来看看如何使用java实现删除文件中的指定内容吧... 目录1. 项目背景详细介绍2. 项目需求详细介绍2.1 功能需求2.2 非功能需求3. 相关技术详细介绍3.1 Ja

使用Python和OpenCV库实现实时颜色识别系统

《使用Python和OpenCV库实现实时颜色识别系统》:本文主要介绍使用Python和OpenCV库实现的实时颜色识别系统,这个系统能够通过摄像头捕捉视频流,并在视频中指定区域内识别主要颜色(红... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间详解