У меня есть файл Excel WKDEL.xls
.Я хочу прочитать все записи и импортировать их в таблицу SQL, которую я уже создал.
Пример файла Excel:
ID | FNAME | T_W_TON | Y_D_TON | WKEND
--------+--------------+----------+----------+--------------
8118.00 | TOM, COLLIIN | 0.00000 | 0.00000 | 12/8/2018
3.00 | JOHN, DOE | 60.22500 | 60.22500 | 12/8/2018
8.00 | KIM, JUNG | 0.00000 | 0.0000 | 12/8/2018
Мне нужны все записи, которые имеют T_W_TON
и в основном больше 0, и импортировать их в мою таблицу SQL:
IDCODE: real
FNAME : nvarchar(50)
T_W_TON: real
Y_D_TON: real
WEKEND: date
CROP_SEASON: int(3 as default)
До сих пор я был в состоянии загрузить файл Excel, но не уверен, как перебирать значения.Ошибки кода в цикле for.Я также хотел бы пропустить значения заголовка.
private void GetWeeklyDeliveries()
{
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:\Fuel\ZINVOW.dbf", FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
//Create New File or Open New File.
var writer = new DBFWriter();
var bsi_code = new DBFField("HIS_ID", NativeDbType.Char, 25, 0);
var f_name = new DBFField("FNAME", NativeDbType.Char, 50, 0);
var t_w_ton = new DBFField("T_W_TON", NativeDbType.Char, 25, 0);
var y_d_ton = new DBFField("Y_D_TON", NativeDbType.Char, 25, 0);
var w_k_end = new DBFField("WKEND", NativeDbType.Char, 25, 0);
var loc = new DBFField("LOC", NativeDbType.Char, 20, 0);
var b_name = new DBFField("B_NAME", NativeDbType.Char, 50, 0);
var a_name = new DBFField("A_NAME", NativeDbType.Char, 50, 0);
var fuel_rate = new DBFField("FUELRATE", NativeDbType.Numeric, 25, 5);
var payment = new DBFField("PAYMENT", NativeDbType.Numeric, 25, 5);
var hun_fig = new DBFField("HUNFIG", NativeDbType.Char, 12, 0);
var ten_fig = new DBFField("TENFIG", NativeDbType.Char, 12, 0);
var one_fig = new DBFField("ONEFIG", NativeDbType.Char, 12, 0);
var hundred = new DBFField("HUNDRED", NativeDbType.Char, 12, 0);
var teen = new DBFField("TEEN", NativeDbType.Char, 12, 0);
var ten = new DBFField("TEN", NativeDbType.Char, 12, 0);
var one = new DBFField("ONE", NativeDbType.Char, 12, 0);
var words = new DBFField("WORDS", NativeDbType.Char, 50, 0);
var authority = new DBFField("AUTHORITY", NativeDbType.Char, 12, 0);
var thoufig = new DBFField("THOUFIG", NativeDbType.Char, 12, 0);
var thousand = new DBFField("THOUSAND", NativeDbType.Char, 12, 0);
var newrate = new DBFField("NEWRATE", NativeDbType.Numeric, 25, 5);
var sn = new DBFField("SN", NativeDbType.Numeric, 25, 0);
var lic = new DBFField("LIC", NativeDbType.Char, 12, 0);
var dealloc = new DBFField("DELALOC", NativeDbType.Char, 12, 0);
var fee = new DBFField("FEE", NativeDbType.Numeric, 12, 5);
writer.Fields = new[] { bsi_code, f_name, t_w_ton, y_d_ton, w_k_end, loc, b_name, a_name, fuel_rate, payment, hun_fig, ten_fig, one_fig, hundred, teen, ten, one, words, authority, thoufig, thousand, newrate, sn, lic, dealloc, fee };
while (rdr.Read())
{//Begin Reading Delivery File (XLS)
var bsicode = rdr[0];
var fname = rdr[1];
var twton = rdr[2];
var ydton = rdr[3];
var wkend = rdr[4];
if (bsicode.ToString() == "HIS_ID")
{
//Skip First Record
}
else if(Convert.ToDouble(twton.ToString()) > 0)
{
var bsicodex = bsicode.ToString();
var fnamex = fname.ToString();
var t_w_tonx = twton.ToString();
var y_d_tonx = ydton.ToString();
var w_k_endx = wkend.ToString();
var locx = "";
var b_namex = "";
var a_namex = "";
var fuel_ratex = "";
var paymentx = "70.00";//need to Calculate
var hun_figx = "";
var ten_figx = "";
var one_figx = "";
var hundredx = "";
var teenx = "";
var tenx = "";
var onex = "";
var wordsx = "";
var authorithyx = "";
var thoufigx = "";
var thousandx = "";
var newratex = 1.19 * 6; //Need to Calculate
var snx = 1234; //Need to autogenerate can Be Primary Key
var licx = "";
var deallocx = "";
var feex = 1.3*34; //Need to Calculate
writer.AddRecord(bsicodex, fnamex, t_w_tonx, y_d_tonx, w_k_endx, locx, b_namex, a_namex, fuel_ratex.ToString(), paymentx, hun_figx, ten_figx, one_figx, hundredx, teenx, tenx, onex, wordsx, authorithyx, thoufigx, thousandx, newratex.ToString(),snx,licx,deallocx,feex.ToString());
}
}
writer.Write(fos);//CRASHES HERE
}
}
}