Introduction
The very first thing to do is to make sure you' ve downloaded and installed the Visual FoxPro Driver to your host machine. Here is the download link from Microsoft.com.
Use OleDbConnection to get the records into a DataTable and later export/write the DataTable to Excel.
Here is the complete code.
using System;
using System.Data;
using System.Data.OleDb;
using System.IO;
namespace Convert_DBF_to_Excel
{
class Program
{
static Missing mv = Missing.Value;
static void Main(string[] args)
{
string dbfFileName = @"D:\myData.dbf";
string constr = "Provider=VFPOLEDB.1;Data Source=" + Directory.GetParent(dbfFileName).FullName;
string ExcelFileName = AppDomain.CurrentDomain.BaseDirectory + "converted_file.xls";
using (OleDbConnection con = new OleDbConnection(constr))
{
var sql = "select * from " + Path.GetFileName(dbfFileName) + ";";
OleDbCommand cmd = new OleDbCommand(sql, con);
DataTable dt = new DataTable();
try
{
con.Open();
}
catch (Exception ex)
{
Console.WriteLine("Error connecting database: " + ex.Message);
return;
}
if (con.State == ConnectionState.Open)
{
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
Console.Write("Reading database... ");
da.Fill(dt);
Console.WriteLine("Completed.");
}
if (con.State == ConnectionState.Open)
{
try
{
con.Close();
}
catch { }
}
if (dt != null && dt.Rows.Count > 0)
{
GenerateExcel(dt, ExcelFileName);
}
}
}
static void GenerateExcel(DataTable sourceDataTable, string ExcelFileName)
{
Console.Write("Generating Excel File...");
Excel.Application excelApp = new Excel.Application();
Excel.Workbook wkb = excelApp.Workbooks.Add(mv);
Excel.Worksheet wks = wkb.Sheets[1];
for (int i = 0; i < sourceDataTable.Columns.Count; ++i)
{
((Excel.Range)wks.Cells[1, i + 1]).Value = sourceDataTable.Columns[i].ColumnName;
}
Excel.Range header = wks.get_Range((object)wks.Cells[1, 1], (object)wks.Cells[1, sourceDataTable.Columns.Count]);
header.EntireColumn.NumberFormat = "@";
object[,] sourceDataTableObjectArray = new object[sourceDataTable.Rows.Count, sourceDataTable.Columns.Count];
for (int row = 0; row < sourceDataTable.Rows.Count; ++row)
{
for (int col = 0; col < sourceDataTable.Columns.Count; ++col)
{
sourceDataTableObjectArray[row, col] = sourceDataTable.Rows[row][col].ToString();
}
}
((Excel.Range)wks.get_Range((object)wks.Cells[2, 1], (object)wks.Cells[sourceDataTable.Rows.Count, sourceDataTable.Columns.Count])).Value2 = sourceDataTableObjectArray;
header.EntireColumn.AutoFit();
header.Font.Bold = true;
wks.Application.ActiveWindow.SplitRow = 1;
wks.Application.ActiveWindow.FreezePanes = true;
wks.SaveAs(ExcelFileName, Excel.XlFileFormat.xlExcel8, mv, mv, mv, mv, mv, mv, mv, mv);
wks = null;
wkb = null;
excelApp.Quit();
Console.WriteLine("Completed.");
}
}
}

tikboy penganPosted Aug 4, 2016, 9:55 AM
Hello sir. My problem is not all the data is converted. Only half of the dbf data is converted. How can i fix this?
Jeff StonePosted Aug 7, 2014, 3:51 PM
Nevermind... I figured it out.
Jeff StonePosted Aug 6, 2014, 5:16 PM
Just to clarify... the above error happens at: da.Fill(dt);
Jeff StonePosted Aug 6, 2014, 5:00 PM
Hi Sunny, What version of Visual Studio did you use? When I try to compile this with VS Express 2013 I get compile errors and need to add: using Excel = Microsoft.Office.Interop.Excel;using System.Reflection; using System.CodeDom.Compiler; After it compiles cleanly, I still cannot get it to run as I'm getting "An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll Additional information; File 'temp.dbf' does not exist." However, the file does exist. I have tried placing the file in multiple directories but nothing seems to resolve this error. Any suggestions would be appreciated. Regards, Jeff
Jessica PeraltaPosted Jan 13, 2014, 9:02 PM
can you please help me, how can i convert dbf foxpro to excel using the vb.net, please help me.... tnx in advance
Sunny SharmaPosted Jan 12, 2014, 1:01 PM
right Sir
Sam HobbsPosted Jan 12, 2014, 12:20 PM
Actually, except for the indexes, the format of a DBF file is quite simple.