0

我正在尝试使用来自 SQL 服务器的一些数据填充一个 excel 文件,

我的代码如下,

   public static void SQLToExcel(Excel.Workbook xlWorkBook, string SQL, string tFORMAT)
    {
        string connectionString = ConfigurationManager.ConnectionStrings["ANSICConnection"].ConnectionString;
        SqlConnection conn;
        conn = new SqlConnection(connectionString);
        conn.Open();


        using (SqlCommand cmd = new SqlCommand(SQL, conn))
        {
            SqlDataReader reader = cmd.ExecuteReader();

            if (reader.HasRows)
            {
                reader.Read();

                ((Range)xlWorkBook.Sheets["COVER"].Range("D6")).CopyFromRecordset(reader);


            }
            reader.Close();
        }


        conn.Close();


    }

我收到以下错误

{“不支持此类接口(HRESULT 异常:0x80004002 (E_NOINTERFACE))”}

我需要将阅读器转换为数组吗?

4

1 回答 1

0

为了能够使用CopyFromRecordset,您需要替换SqlDataReaderADODB.Recordset. 不要忘记添加对Microsoft ActiveX Data Objects 2.x库的引用。

于 2019-04-05T06:09:11.590 回答