上篇博客中主要记录一些关于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数据导出代码实现相关推荐

  1. dbf文件mysql,dbf文件怎么打开?dbf是什么文件?

    dbf文件怎么打开?dbf文件是一种数据库格式文件,用于存储数据库的数据.一些用户在工作的过程中,可能会碰到后缀名为dbf的文件,正常双击是无法打开的,我们需要借助软件才能打开dbf文件.下面给大家推 ...

  2. python抓取表格数据_Python如何实现从PDF文件中爬取表格数据(代码示例)

    本篇文章给大家带来的内容是关于Python如何实现从PDF文件中爬取表格数据(代码示例),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助. 本文将展示一个稍微不一样点的爬虫. 以往我们的 ...

  3. python导出数据到excel文件_Python笔记:把数据导出到Excel文件上

    相信初学Python数据分析的小伙伴都发现了,只是把数据加载在Python的环境中做数据处理还不够,因为环境的问题(Python加载进来的只是一张虚拟表,数据暂存内存中),我们最终还是需要把粗加工好的 ...

  4. mapgis明码文件转为点线面文件_MAPGIS平台点线面符号数据导出与绘制

    为实现数据共享,有导出和采用MAPGIS.ARCGIS等平台符号的任务.而MAPGIS等平台提供了明码文件导出等功能,可以到达目标.而完成此任务前,需要仔细阅读帮助文件,以弄清楚操作步骤和文件中各行数 ...

  5. Netcdf 文件多属性,按照时间段导出代码示例

    由于个人即是java初学者,又是netcdf文件小白,所以为了实现最终效果,可以对一个初始nc文件进行筛选指定变量,并且可以设定时间区间,花得时间比想象的要多,最终实现了效果,代码示例如下: @Get ...

  6. xls/csv文件转换成dbf文件

    帮一个中科院小学妹写的一个小脚本,主要是利用python中的pandas,xlrd,dbfpy包将excel中的xls/csv文件中转化成dbf文件 一.安装相关包 pip install xlrd ...

  7. C语言调用dbf文件,C语言直接读取FOXBASE的DBF文件.pdf

    C语言直接读取FOXBASE的DBF文件 维普资讯 1◇-l 1(Bs I 据 的读取 . 一 . 问题的提出 表 1 DBF文件的文件参数表 FOXBAsE有时在与外界进行数据交换 .提高数据 位置 ...

  8. python批量读取dbf_python下用dbfread操作DBF文件

    今天要从同事发给我的一个文件中统计一些数字,一看还是数据库文件,以DBF结尾,近1个G呢.电脑上也没装ACCESS等数据库管理软件.后来找了个DBF阅读器,发现虽然能打开,但是筛选什么的不方便,也不好 ...

  9. oracle dbf 超大,oracle 数据库users01.dbf文件过大 转移方法

    如果出现 linux 拒绝错误,可以把目录权限 该为777 由于在安装的时候将Oracle安装到了C盘,表空间也创建到了C盘(当时没有在意),等项目进行到了中期,发现C盘的空间不够用了.此时,一个较好 ...

  10. python读取dbf文件、dbf转xls、入库Postgres

    python读取dbf文件,转xls文件 文章目录 python读取dbf文件,转xls文件 依赖库安装 代码实现 读取dbf dbf转xls dbf转xls(批量) dbf入库Postgres 参考 ...

最新文章

  1. mysql数据类型默认长度_mysql数据类型长度
  2. python2与python3在absolute import的区别
  3. 感受JTable 与 JTableModel
  4. [JavaWeb-HTML]HTML标签_文本标签_练习
  5. 奇技淫巧:在spring官网上下载历史版本的spring插件,springsource-tool-suite
  6. 2021年中国硬核创业者调研报告
  7. 线性表--链式实现方式
  8. 三部门部署开展非学科类校外培训收费专项整治工作
  9. 人工智能TensorFlow工作笔记004---还记得标准差嘛_标准差的由来
  10. 【报告分享】快手创作者商业价值报告.pdf
  11. 调用腾讯优图开放平台进行人脸识别-Java调用API实现
  12. create symbolic array
  13. 100-GAMP安装调试细则
  14. 目标跟踪 SiamRPN++(SiamRPN++:Evolution of Siamese Visual Tracking with Very Deep Networks)
  15. Queues.drain 一边读数据一边写数据
  16. Android RGB颜色对照表
  17. 复合函数的极限与连续
  18. php 显示英文日期,DEDECMS怎么显示英文日期时间
  19. 关于亚马逊人的财务自由
  20. 前端校招字节跳动面试 第三篇

热门文章

  1. DC学院数据分析师(入门)学习笔记----高级爬虫技巧
  2. MindMap学习使用
  3. Red Teaming Mind Map
  4. python 邮件合并的基本操作步骤_邮件合并操作过程
  5. 关于HTML系统学习(1)
  6. 详解:字符转换函数(大写转小写,小写转大写)
  7. JAVA语言基础知识总结
  8. Go 语言开发工具 LiteIDE x22 发布
  9. 过犹不及,别再在编程中高射炮打蚊子
  10. 计算机黑屏启动超慢,电脑开机慢黑屏时间长怎么解决