简体   繁体   English

将closedxml转换为数据表而不循环

[英]closedxml to datatable without looping

at work I've used closedXML in the past to go from a datatable or dataset to excel extremely quickly without looping. 在工作中,我过去使用closeXML可以从数据表或数据集快速实现卓越而不会循环。 Now I need to go the other way but the only documentation I can find on closed XMl or anything else to go from excel to datatable is to loop. 现在,我需要走另一条路,但是我可以在封闭的XMl或从excel到datatable的任何其他内容中找到的唯一文档是循环。 I can't imagine with the current demand for speed, the large amounts of data that can go into excel and the widespread use of Office that nobody has figured out a faster way than looping. 我无法想象当前对速度的需求,可以进入excel的大量数据以及Office的广泛使用,没有人能找到比循环更快的方法。

Is there a way in closed XML or another reasonably sized, safe library that quickly moves excel to datables or other system.data objects such as datasets without looping? 封闭的XML或另一个大小合理,安全的库中是否可以将excel快速移至datable或其他system.data对象(如数据集)而不会循环?

You can use a plain OleDb connection to read data from a worksheet into a DataTable : 您可以使用普通的OleDb连接将数据从工作表读取到DataTable

string strExcelConn = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\filename.xlsx;Extended Properties='Excel 12.0 Xml;HDR=YES;'";
using (OleDbConnection connExcel = new OleDbConnection(strExcelConn))
{
    string selectString = "SELECT * FROM [CA$A1:D500]";
    using (OleDbCommand cmdExcel = new OleDbCommand(selectString,connExcel))
    {
        cmdExcel.Connection = connExcel;
        connExcel.Open();
        DataTable dt=new DataTable();                    
        OleDbDataAdapter adp = new OleDbDataAdapter();
        adp.SelectCommand = cmdExcel;
        adp.FillSchema(dt, SchemaType.Source);
        adp.Fill(dt);
        int range=dt.Columns.Count;
        int row = dt.Rows.Count;
    }
}

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

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