上篇博客中主要记录一些关于DBF数据文件的概念性知识。包括DBF的数据结构和用处。在这里记录一下在C#中导出DBF文件的实现方式。
DBF文件也是一种数据库文件,我们导出DBF文件也就是将数据库中的数据导出到DBF文件中。所以最主要的就是讲DataTable转换成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("