简体   繁体   English

C# 从 excel 加载数据适配器,是否有最大列数?

[英]C# Loading data adapter from excel, is there a maximum column count?

I'm running into an issue trying to create ac# data table from an excel spreadsheet.我在尝试从 Excel 电子表格创建 ac# 数据表时遇到问题。 The code is simple but for some reason when I step through it, the data table only loads with 256 of the 700 columns that are in the spreadsheet.代码很简单,但由于某种原因,当我逐步执行它时,数据表只加载了电子表格中 700 列中的 256 列。 Looking at the code below, Settings.Excel_Proj_Select is just "Select * from Project_Data$".查看下面的代码,Settings.Excel_Proj_Select 只是“从 Project_Data$ 中选择 *”。 Is there a maximum column count and if so, is there a way around this to get the full sheet into a data table?是否有最大列数,如果有,有没有办法将整个工作表放入数据表中?

        public static DataTable getRowData()
        {

        OleDbConnection cnn = new OleDbConnection(Settings.ExcelCN);
        OleDbCommand oconn = new OleDbCommand(Settings.Excel_Proj_Select, cnn);
        cnn.Open();
        OleDbDataAdapter adp = new OleDbDataAdapter(oconn);
        DataTable dt = new DataTable();
        adp.Fill(dt);
        cnn.Close();

        dt.Columns.Add("Extract_Date", typeof(string));
        foreach (DataRow row in dt.Rows)
        {
            row["Extract_Date"] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        }

        return dt;
    }

Any info is appreciated.任何信息表示赞赏。 Thank you!谢谢!

There appears to be a 255 max number of columns when using the Ole provider for this, yes.使用 Ole 提供程序时,列的最大数量似乎为 255,是的。

But you can use a simple workaround by specifying the range in your queries:但是您可以通过在查询中指定范围来使用简单的解决方法:

//Modify the range to suit your needs
SELECT * FROM Project_Data$[RANGE]:[HERE]

You can then run multiple queries, each which SELECT different ranges, then combine the data.然后您可以运行多个查询,每个查询选择不同的范围,然后组合数据。

Please see here for another method using OpenXML.请参阅此处了解使用 OpenXML 的另一种方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM