如何在C#中将数据从Excel传输到DataGridView?
我已经尝试了许多我在线看到的方法,将Excel文件复制到DataGridView,但没有用。我的主要问题是,使用这些方法,有一个问题是完全理解它们如何在没有适当解释的情况下如何工作,或者只是给我奇怪的错误,就像一个告诉我我想复制的Excel表是对象的无效实例。 我想指定我可以轻松地使用其他功能在Excel文件上工作,并且根本没有错误,我也不需要在Excel文件上进行任何类型的操作,只是一种简单的方法来复制它。 顺便说一句,我就在2天前就开始使用C#上学的舞台,这就是为什么我不太了解的原因。
I've tried numerous methods I've seen online to copy a Excel file to a DataGridView, but none worked. My main problem is that with those methods there's a problem about fully understanding how they work without a proper explanation or they just give me strange errors, like one that told me that the Excel sheet I wanted to copy was a null instance of an object.
I want to specify that I can easily work on the Excel file with other functions and I get no errors at all, I also don't need any kind of manipulation on the Excel file for this, just a plain way to copy it.
Btw I started using C# just 2 days ago for a stage-thing for school, that's why I don't know much.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该将Excel-File转换为C#-Object,例如列表,DataTable或其他可以绑定到DataGridView的东西。
查看 csvhelper ,这可能会让您开始。
然后,只需将您的对象绑定到您的datagridView,例如如何显示在这里。
祝你好运!
You should convert your Excel-File to a C#-Object, like a List, DataTable, or something else that you can bind to a DataGridView.
Check out CsvHelper, this might get you started.
Then, simply bind to your Object to your DataGridView, e. g. how it was shown here.
Good Luck!
右键单击项目的名称或参考,然后选择管理Nuget软件包。

在浏览选项卡下,搜索 free spire.xls 并安装它。

使用下面的代码将数据从Excel传输到DataGridView。
Right-click on your project's name or References and select Manage NuGet Packages.

Under the Browse tab, search for free spire.xls and install it.

Use the code below to transfer data from Excel to DataGridView.