Нужно записать числовые значения в файл DBF, используя ExcelReaderFactory - PullRequest
0 голосов
/ 02 января 2019

Приведенный ниже код протестирован и записывает файл dbf, но значения являются строками, потому что я не могу инициализировать или преобразовать значения в удвоенные или плавающие значения перед записью в файл.Кто-нибудь может помочь и посмотреть, как, например, первая переменная (bsi_code) может быть записана как double или float?Можно ли объявлять заголовки как var .... как преобразовать и добавить эту запись как число с плавающей запятой или двойное число.

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 = "";

                            using (simisdbEntities db = new simisdbEntities())
                            {
                                var getdata = db.GetFuelRatesByBsiCode(3, bsicodex); // Get the current crop year




                                foreach (var z in getdata)
                                {

                                locx = z.LOC;
                                b_namex = z.B_NAME;
                                a_namex = z.AREA;
                                fuel_ratex = z.FUELRATE.ToString();



                                }

                            }

                            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);

            }

        }
    }
...