当我尝试使用 ExcelDataReader 将文件上传到数据库时出现 "Specified method is not supported" 错误

I get a "Specified method is not supported" error when i try to use ExcelDataReader to upload file to database

我写了一个方法,BulkCopy,将我的 Excel 文件上传到 SQL 服务器数据库 table。我正在尝试对此进行单元测试,但每次都失败 "System.NotSupportedException : Specified method is not supported"。

如果有人能看一下,将不胜感激。

亲切的问候,

艾美特

    public static void BulkCopy(string inputFilePath, string tableName)
    {

        System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
        var stream = File.Open(inputFilePath, FileMode.Open, FileAccess.Read);
        using (var reader = ExcelReaderFactory.CreateReader(stream))
        {
            using (var bulkCopy = new SqlBulkCopy(ConnectionString))
            {

                bulkCopy.EnableStreaming = true;
                bulkCopy.DestinationTableName = tableName;
                reader.Read();
                var cols = Enumerable.Range(0, reader.FieldCount).Select(i => reader.GetValue(i)).ToArray();
                foreach (var col in cols)
                {
                    var column = cols.GetValue(0).ToString();

                    if (column.Trim() == "Column 1")
                    {
                        bulkCopy.ColumnMappings.Add(column, "Column 1");
                    }

                    if (column.Trim() == "Column 2")
                    {
                        bulkCopy.ColumnMappings.Add(column, "Column 2");
                    }

                    if (column.Trim() == "Column 3")
                    {
                        bulkCopy.ColumnMappings.Add(column, "Column 3");
                    }

                //continued for column mappings...

                }

                bulkCopy.WriteToServer(reader);
            }
            Console.WriteLine("Copy data to database done (DataReader).");
        }
    }

我测试了你的代码,问题显示在下面的代码中,你reader不正确。

bulkCopy.WriteToServer(reader);

将数据表传入bulk,试试下面的代码

public static void BulkCopy(string inputFilePath, string tableName)
    {
        System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
        var stream = System.IO.File.Open(inputFilePath, FileMode.Open, FileAccess.Read);
        IExcelDataReader reader;

        if (inputFilePath.EndsWith(".xls"))
            reader = ExcelReaderFactory.CreateBinaryReader(stream);
        else if (inputFilePath.EndsWith(".xlsx"))
            reader = ExcelReaderFactory.CreateOpenXmlReader(stream);
        else
            throw new Exception("The file to be processed is not an Excel file");
        var conf = new ExcelDataSetConfiguration
        {
            ConfigureDataTable = _ => new ExcelDataTableConfiguration
            {
                UseHeaderRow = true
            }
        };
        var dataSet = reader.AsDataSet(conf);

        // Now you can get data from each sheet by its index or its "name"
        var dataTable = dataSet.Tables[0];

        using (var bulkCopy = new SqlBulkCopy(ConnectionString))
            {
                bulkCopy.EnableStreaming = true;
                bulkCopy.DestinationTableName = tableName;
                reader.Read();
                var cols = Enumerable.Range(0, reader.FieldCount).Select(i => reader.GetValue(i)).ToArray();
                foreach (var col in cols)
                {
                    var column =col.ToString();

                    if (column.Trim() == "Column 1")
                    {
                        bulkCopy.ColumnMappings.Add(column, "Column1");
                    }

                    if (column.Trim() == "Column 2")
                    {
                        bulkCopy.ColumnMappings.Add(column, "Column2");
                    }

                    if (column.Trim() == "Column 3")
                    {
                        bulkCopy.ColumnMappings.Add(column, "Column3");
                    }

                    //continued for column mappings...

                }
                bulkCopy.WriteToServer(dataTable);
            }
            Console.WriteLine("Copy data to database done (DataReader).");           
    }

将 dataReader 批量传递,如图所示更改您的 foreach 部分

               for (var i = 0; i<cols.Count();i++)
                {
                    if (cols[i].ToString().Trim() == "Column 1")
                    {
                        bulkCopy.ColumnMappings.Add(i, "Column1");
                    }

                    if (cols[i].ToString().Trim() == "Column 2")
                    {
                        bulkCopy.ColumnMappings.Add(i, "Column2");
                    }

                    if (cols[i].ToString().Trim() == "Column 3")
                    {
                        bulkCopy.ColumnMappings.Add(i, "Column3");
                    }

                    //continued for column mappings...

                }

您需要更改它设置列映射的方式。如果删除它们,它将起作用。在您发布的示例 link 中,他们使用 GetName 获取列。我试过了,但由于某种原因它不起作用。我收到 GetOrdinal 错误。这与您的代码中出现的错误相同:ExcelDataReader.ExcelDataReader`2.GetOrdinal(String name).

不确定它有多漂亮,但它确实有效。

public static void BulkCopy(string inputFilePath, string tableName)
    {

        System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
        var stream = File.Open(inputFilePath, FileMode.Open, FileAccess.Read);
        using (var reader = ExcelReaderFactory.CreateReader(stream))
        {
            using (var bulkCopy = new SqlBulkCopy(ConnectionString))
            {

                bulkCopy.EnableStreaming = true;
                bulkCopy.DestinationTableName = tableName;
                reader.Read();
                var cols = Enumerable.Range(0, reader.FieldCount).Select(i => reader.GetValue(i)).ToArray();
                foreach (var col in cols)
                {
                     if (cols[i].ToString() == "Column 1")
                     {
                          bulkCopy.ColumnMappings.Add(i, "Column 1");
                     }

                     if (cols[i].ToString() == "Column 2")
                     {
                          bulkCopy.ColumnMappings.Add(i, "Column 2");
                     }

                     if (cols[i].ToString() == "Column 3")
                     {
                          bulkCopy.ColumnMappings.Add(i, "Column 3");
                     }

                    //continued for column mappings...

                }

                bulkCopy.WriteToServer(reader);
            }
            Console.WriteLine("Copy data to database done (DataReader).");
        }
    }