将Excel导入Datagrid

时间:2017-11-07 18:59:40

标签: c# excel datagrid oledb oledbdataadapter

我正在尝试将excel文件放入我的程序中,以便我可以处理数据并将其导出。我正在使用OleDB访问访问文件,并尝试将其传递给数据表,以便我可以查看它并在winform中使用它。这是我正在使用的代码:

$ givenDate="2017-11-07 19:20:37 $(date +%:::z)"

我发生的错误是当我尝试使用OleDBAdapter(dataAdapter)填充dataTable时。尽管只有DataTable的覆盖,它抛出一个异常,要求一个ADODBrecordset。我该如何解决这个问题?或者我错过了什么?

提前谢谢。

2 个答案:

答案 0 :(得分:0)

你可以使用像EpPlus这样的库。这种支持在不使用ole或excel的情况下优于原生。

您可以将数据作为数组加载并直接访问它。

http://epplus.codeplex.com/wikipage?title=LinqExample

答案 1 :(得分:0)

您可以尝试使用Exportable阅读Excel内容。是我之前开发的一个小型库,现在作为OpenSource在Nuget上发布。

基本上,您可以读取这样的Excel文件:

IImportEngine engine = new ExcelImportEngine();
var key = engine.AddContainer<DummyPersonWithAttributes>();
engine.SetDocument(pathToFile); //or MemoryStream instance
var data = engine.GetList<DummyPersonWithAttributes>(key);

您可以获得有关here

的更多信息