XLS to DBF File Conversion using ExcelDataReader

You can easily read an Excel file to create a .DBF file using ExcelDataReader. ExcelDataReader is a lightweight and fast library written in C# for reading Microsoft Excel files (2.0-2007). Below is code that will read an xls file, skip the headers and write data in DBF format. Please note that if you want to create a specific DBF file that already has its defined types you must first use DBF Viewer to find out which type you will be writing the data as. Also note that for setting up a date type you do not specify a size and length for the type.

private void ProcessExceltoDBF()
string FilePath = textBox1.Text;
using (var fileStream = new FileStream(FilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
using (var rdr = ExcelReaderFactory.CreateReader(fileStream))
using (Stream fos = File.Open(@"C:\directory\filename.dbf", FileMode.OpenOrCreate, FileAccess.ReadWrite))
//Create New File or Open New File.
var writer = new DBFWriter();
var bsi_code = new DBFField("HIS_ID", NativeDbType.Numeric, 25, 0);
var f_name = new DBFField("FNAME", NativeDbType.Char, 50, 0);
var t_w_ton = new DBFField("T_W_TON", NativeDbType.Numeric, 12, 5);
var w_k_end = new DBFField("WKEND", NativeDbType.Date);
writer.Fields = new[] { bsi_code, f_name, t_w_ton,w_k_end };
while (rdr.Read())
{//Begin Reading Delivery File (XLS)
var bsicode = rdr[0];
var fname = rdr[1];
var twton = rdr[2];
if (bsicode.ToString() == "ID")
//Skip First Header Record Format
else if (Convert.ToDouble(twton.ToString()) > 0)
DateTime firstdate = DateTime.ParseExact(w_k_endx, new string[] { "M/d/yyyy hh:mm:ss tt", "MM/dd/yyyy hh:mm:ss tt" }, CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None);
writer.AddRecord(Convert.ToInt32(bsicode), fname, Convert.ToDecimal(twton), firstdate);

    Top 10 Cryptocurrency Investments

    Total Page Visits: 1058 – Today Page Visits: 1

    Total Page Visits: 1058 - Today Page Visits: 1