如何在C#中将数据从Excel传输到DataGridView?

发布于 2025-02-01 14:28:42 字数 254 浏览 3 评论 0原文

我已经尝试了许多我在线看到的方法,将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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

那伤。 2025-02-08 14:28:42

您应该将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!

情场扛把子 2025-02-08 14:28:42
  1. 右键单击项目的名称或参考,然后选择管理Nuget软件包

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

  3. 使用下面的代码将数据从Excel传输到DataGridView。

      private void button1_click(对象发送者,EventArgs e)
     {
         Workbook Workbook = New Workbook();
         workbook.loadfromfile(@“ c:\ users \ thermandator \ desktop \ data.xlsx”);
         工作表工作表= Workbook.worksheets [0];
    
         //在工作表中导出数据到数据表 
         DataTable DT = Worksheet.exportDatabable();
    
         //此过载使您可以指定是否导出范围以及是否导出列名和公式的实际值
         // DataTable DT = Worksheet.exportDataTable(worksheet.range [“ a1:c10”],true,true);
    
         datagridview1.datasource = dt;
     }
     
  1. Right-click on your project's name or References and select Manage NuGet Packages.
    enter image description here

  2. Under the Browse tab, search for free spire.xls and install it.
    enter image description here

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

     private void button1_Click(object sender, EventArgs e)
     {
         Workbook workbook = new Workbook();
         workbook.LoadFromFile(@"C:\Users\Administrator\Desktop\Data.xlsx");
         Worksheet worksheet = workbook.Worksheets[0];
    
         //Export data in the worksheet to a DataTable 
         DataTable dt = worksheet.ExportDataTable();
    
         //This overload enables you to specify the range to be exported along with whether to export column names and the actual values of formulas
         //DataTable dt = worksheet.ExportDataTable(worksheet.Range["A1:C10"], true, true);
    
         dataGridView1.DataSource = dt;
     }
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文