c# - ExcelReader Factory 在最后写入行失败

标签 c# excel filestream excel-reader

我有一个 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 



            }

        }

    }

最佳答案

我的做法是使用一个易于使用且优秀的工具ExcelDataReader from nuget

  using (var fileStream = new FileStream(_fileLocation, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
        using (var rdr = ExcelReaderFactory.CreateReader(fileStream))
        { 

            while(rdr.Read())
            {

                //add it to a data table 
            }

        } 

然后使用SqlBulkCopyDataTable写入服务器

关于c# - ExcelReader Factory 在最后写入行失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53949636/

相关文章:

当循环位于timer1_Tick函数内部时C#错误

c# - JIT 编译器如何编译泛型?

c# - C# 中的不间断异常

r - 使用readxl时出错: 'exdir' does not exist

javascript - 我如何访问我的项目中的 js 文件?

.net - 何时使用 Using 语句

c# - 无法安装 System.Data.SqlClient

vba - 在excel VBA中为组合框赋值

excel - 勾选或取消勾选复选框时输入时间戳

c# - 为什么 StreamWriter 会覆盖我的文件?