C# 生成DBF,无需注册Microsoft.Jet.OLEDB。
1 namespace Consoleapplication 2 { 3 class PRogram 4 { 5 static void Main(string[] args) 6 { 7 Test(); 8 Console.ReadKey(); 9 }10 11 private static void Test()12 {13 string testPath = AppDomain.CurrentDomain.BaseDirectory;14 var odbf = new DbfFile(Encoding.GetEncoding(936));15 odbf.Open(Path.Combine(testPath, "test.dbf"), FileMode.Create);16 17 //创建列头18 odbf.Header.AddColumn(new DbfColumn("编号", DbfColumn.DbfColumnType.Character, 20, 0));19 odbf.Header.AddColumn(new DbfColumn("名称", DbfColumn.DbfColumnType.Character, 20, 0));20 odbf.Header.AddColumn(new DbfColumn("地址", DbfColumn.DbfColumnType.Character, 20, 0));21 odbf.Header.AddColumn(new DbfColumn("时间", DbfColumn.DbfColumnType.Date));22 odbf.Header.AddColumn(new DbfColumn("余额", DbfColumn.DbfColumnType.Number, 15, 3));23 24 var orec = new DbfRecord(odbf.Header) { AllowDecimalTruncate = true };25 List<User> list = User.GetList();26 foreach (var item in list)27 {28 orec[0] = item.UserCode;29 orec[1] = item.UserName;30 orec[2] = item.Address;31 orec[3] = item.date.ToString("yyyy-MM-dd HH:mm:ss");32 orec[4] = item.money.ToString();33 odbf.Write(orec, true);34 }35 odbf.Close();36 }37 }38 39 public class User40 {41 public string UserCode { get; set; }42 public string UserName { get; set; }43 public string Address { get; set; }44 public DateTime date { get; set; }45 public decimal money { get; set; }46 47 public static List<User> GetList()48 {49 List<User> list = new List<User>();50 list.Add(new User() { UserCode = "A1", UserName = "张三", Address = "上海杨浦", date = DateTime.Now, money = 1000.12m });51 list.Add(new User() { UserCode = "A2", UserName = "李四", Address = "湖北武汉", date = DateTime.Now, money = 31000.008m });52 list.Add(new User() { UserCode = "A3", UserName = "王子龙", Address = "陕西西安", date = DateTime.Now, money = 2000.12m });53 list.Add(new User() { UserCode = "A4", UserName = "李三", Address = "北京", date = DateTime.Now, money = 3000.12m });54 return list;55 }56 }57 58 }
生成的文件截图:
操作DBF文件的部分代码:
1 /// 2 /// Author: Ahmed Lacevic 3 /// Date: 12/1/2007 4 /// Desc: 5 /// 6 /// Revision History: 7 /// ----------------------------------- 8 /// Author: 9 /// Date: 10 /// Desc: 11 12 13 using System; 14 using System.Collections.Generic; 15 using System.Text; 16 using System.IO; 17 using System.Globalization; 18 19 20 namespace SocialExplorer.IO.FastDBF 21 { 22 23 /// <summary> 24 /// Use this class to create a record and write it to a dbf file. You can use one record object to write all records!! 25 /// It was designed for this kind of use. You can do this by clearing the record of all data 26 /// (call Clear() method) or setting values to all fields again, then write to dbf file. 27 /// This eliminates creating and destroying objects and optimizes memory use. 28 /// 29 /// Once you create a record the header can no longer be modified, since modifying the header would make a corrupt DBF file. 30 /// </summary> 31 public class DbfRecord 32 { 33 34 /// <summary> 35 /// Header provides information on all field types, sizes, precision and other useful information about the DBF. 36 /// </summary> 37 private DbfHeader mHeader = null; 38 39 /// <summary> 40 /// Dbf data are a mix of ASCII characters and binary, which neatly fit in a byte array. 41 /// BinaryWriter would esentially perform the same conversion using the same Encoding class. 42 /// </summary> 43 private byte[] mData = null; 44 45 /// <summary> 46 /// Zero based record index. -1 when not set, new records for example. 47 /// </summary> 48 private int mRecordIndex = -1; 49 50 /// <summary> 51 /// Empty Record array reference used to clear fields quickly (or entire record). 52 /// </summary> 53 private readonly byte[] mEmptyRecord = null; 54 55 56 /// <summary> 57 /// Specifies whether we allow strings to be truncated. If false and string is longer than we can fit in the field, an exception is thrown. 58 /// </summary> 59 private bool mAllowStringTruncate = true; 60 61 /// <summary> 62 /// Specifies whether we allow the decimal portion of numbers to be truncated. 63 /// If false and decimal digits overflow the field, an exception is thrown. 64 /// </summary> 65 private bool mAllowDecimalTruncate = false; 66 67 /// <summary> 68 /// Specifies whether we allow the integer portion of numbers to be truncated. 69 /// If false and integer digits overflow the field, an exception is thrown. 70 /// </summary> 71 private bool mAllowIntegerTruncate = false; 72 73 74 //array used to clear decimals, we can clear up to 40 decimals which is much more than is allowed under DBF spec anyway. 75 //Note: 48 is ASCII code for 0. 76 private static readonly byte[] mDecimalClear = new byte[] {48,48,48,48,48,48,48,48,48,48,48,48,48,48,48, 77 48,48,48,48,48,48,48,48,48,48,48,48,48,48,48, 78 48,48,48,48,48,48,48,48,48,48,48,48,48,48,48}; 79 80 81 //Warning: do not make this one static because that would not be thread safe!! The reason I have 82 //placed this here is to skip small memory allocation/deallocation which fragments memory in .net. 83 private int[] mTempIntVal = { 0 }; 84 85 86 //Ascii Encoder 87 private readonly Encoding encoding = Encoding.ASCII; 88 89 /// <summary> 90 /// Column Name to Column Index map 91 /// </summary> 92 private readonly Dictionary<string, int> mColNameToConIdx = new Dictionary<string, int>(StringComparer.InvariantCulture); 93 94 95 96 /// <summary> 97 /// 98 /// </summary> 99 /// <param name="oHeader">Dbf Header will be locked once a record is created 100 /// since the record size is fixed and if the header was modified it would corrupt the DBF file.</param>101 public DbfRecord(DbfHeader oHeader)102 {103 mHeader = oHeader;104 mHeader.Locked = true;105 106 //create a buffer to hold all record data. We will reuse this buffer to write all data to the file.107 mData = new byte[mHeader.RecordLength];108 mEmptyRecord = mHeader.EmptyDataRecord;109 encoding = oHeader.encoding;110 111 for (int i = 0; i < oHeader.mFields.Count; i++)112 mColNameToConIdx[oHeader.mFields[i].Name] = i;113 }114 115 116 /// <summary>117 /// Set string data to a column, if the string is longer than specified column length it will be truncated!118 /// If dbf column type is not a strin
新闻热点
疑难解答