如何将excel.xlsb数据读入C#中的列表 [英] How to read excel.xlsb data to lists in C#
本文介绍了如何将excel.xlsb数据读入C#中的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从许多excel(.xlsb)工作簿中将数据收集到C#中。
是否有方法可以读取多个excel文件并将数据收集到List中(循环除外)?
我在代码下面我尝试首先在excel表读取中创建一个范围,然后尝试读入List。这也不行。
I would like to collect data into List in C# from many excel(.xlsb) workbooks.
Is there a method to read couple of excel files and collect data into List (except looping)?
I below code I tried to first create a range in excel sheet read that range into an array and then try to read into List. That's also not work.
xlRangeDB = xlWorkSheetDB.get_Range("A2", "BA" + lRow.ToString());
object[,] valueArray = xlRangeDB.Value2;
List<string> inputData = new List<string>();
inputData.AddRange(valueArray);
我尝试过:
我经历了很多相关的条目,但我发现是使用数组而不是List快速读取。
What I have tried:
I went through many related entries but what I found is to quickly read using arrays not List.
推荐答案
怎么样使用Excel文件作为数据库(如果工作表被组织为数据表)
How about using Excel files as a database (if sheets are organized as data table)
string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\"C:\MyExcelFile.xlsx\";Extended Properties=\"Excel 12.0 Xml;HDR=YES\"";
var _connection = new OleDbConnection();
_connection.ConnectionString = connectionString;
_connection.Open();
var _adapter = new OleDbDataAdapter("SELECT * FROM SheetName", _connection);
var _data = new DataSet("SheetName");
_adapter.Fill(this._data);
var inputData = valueArray.Cast<object>().Select(Convert.ToString).ToList();
此解决方案适用于将数组转换为列表。
This solution works for me converting arrays to list.
这篇关于如何将excel.xlsb数据读入C#中的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文