IronXL 操作指南 Csharp导入excel How to Import Excel Files in C# Curtis Chau 已更新:八月 20, 2025 Download IronXL NuGet 下载 DLL 下载 Start Free Trial Copy for LLMs Copy for LLMs Copy page as Markdown for LLMs Open in ChatGPT Ask ChatGPT about this page Open in Gemini Ask Gemini about this page Open in Grok Ask Grok about this page Open in Perplexity Ask Perplexity about this page Share Share on Facebook Share on X (Twitter) Share on LinkedIn Copy URL Email article This article was translated from English: Does it need improvement? Translated View the article in English As developers, we often need to import data from Excel files and use it to fulfill our application and data management requirements. Without requiring many lines of code, IronXL gives us an easy way to import exactly the data we need directly into a C# project and then manipulate it programmatically. Quickstart: Instantly Load Your Excel File With just one method call using IronXL’s timeout-free API, you can load any supported Excel sheet (XLSX, CSV, etc.) in seconds—no Interop, no fuss. Begin interacting with the workbook immediately by accessing cells, ranges, or sheets as needed. Get started making PDFs with NuGet now: Install IronXL with NuGet Package Manager PM > Install-Package IronXL.Excel Copy and run this code snippet. WorkBook wb = IronXL.WorkBook.Load("path/to/data.xlsx"); Deploy to test on your live environment Start using IronXL in your project today with a free trial Free 30 day Trial Import Excel Data C# Import Data with the IronXL Library Import Excel data in C# Import data of specific cell range Import Excel data with aggregate functions SUM, AVG, MIN, MAX, and more Minimal Workflow (5 steps) Download and install the C# library to import Excel files Prepare the Excel files to be imported Use the Load method to import spreadsheet Edit the loaded Excel file using intuitive APIs Export the edited Excel file in various types Step 1 1. Import Data with the IronXL Library Import data using the functions provided by the IronXL Excel library, which we'll be using in this tutorial. The software is available free for development. Install into your C# Project via DLL Download or navigate using the NuGet package. Install-Package IronXL.Excel How to Tutorial 2. Access WorkSheet for Project For our project needs today, we will be importing Excel data into our C# application, using the IronXL software installed in step 1. For step 2, we will load our Excel WorkBook in our CSharp project by using the WorkBook.Load() function of IronXL. We pass the path of the Excel WorkBook as a string parameter in this function, like this: // Load Excel file WorkBook wb = WorkBook.Load("Path"); // Load Excel file WorkBook wb = WorkBook.Load("Path"); ' Load Excel file Dim wb As WorkBook = WorkBook.Load("Path") $vbLabelText $csharpLabel The Excel file at the specified path will be loaded into wb. Next, we need to access a specific WorkSheet of the Excel file whose data will be imported into the project. For this purpose, we can use the GetWorkSheet() function of IronXL, passing the sheet name as a string parameter to specify which sheet of the WorkBook to import. // Specify sheet name of Excel WorkBook WorkSheet ws = wb.GetWorkSheet("SheetName"); // Specify sheet name of Excel WorkBook WorkSheet ws = wb.GetWorkSheet("SheetName"); ' Specify sheet name of Excel WorkBook Dim ws As WorkSheet = wb.GetWorkSheet("SheetName") $vbLabelText $csharpLabel The WorkSheet will be imported as ws, and wb is the WorkBook which we have defined in the above code sample. There are also the following alternative ways to import an Excel WorkSheet into the project. // Import WorkSheet by various methods // by sheet indexing WorkSheet mySheet = wb.WorkSheets[SheetIndex]; // get default WorkSheet WorkSheet defaultSheet = wb.DefaultWorkSheet; // get first WorkSheet WorkSheet firstSheet = wb.WorkSheets.First(); // for the first or default sheet WorkSheet firstOrDefaultSheet = wb.WorkSheets.FirstOrDefault(); // Import WorkSheet by various methods // by sheet indexing WorkSheet mySheet = wb.WorkSheets[SheetIndex]; // get default WorkSheet WorkSheet defaultSheet = wb.DefaultWorkSheet; // get first WorkSheet WorkSheet firstSheet = wb.WorkSheets.First(); // for the first or default sheet WorkSheet firstOrDefaultSheet = wb.WorkSheets.FirstOrDefault(); ' Import WorkSheet by various methods ' by sheet indexing Dim mySheet As WorkSheet = wb.WorkSheets(SheetIndex) ' get default WorkSheet Dim defaultSheet As WorkSheet = wb.DefaultWorkSheet ' get first WorkSheet Dim firstSheet As WorkSheet = wb.WorkSheets.First() ' for the first or default sheet Dim firstOrDefaultSheet As WorkSheet = wb.WorkSheets.FirstOrDefault() $vbLabelText $csharpLabel Now, we can easily import any type of data from the specified Excel files. Let's see all the possible aspects which we use to import Excel file data in our project. 3. Import Excel Data in C# This is the basic aspect of importing Excel file data into our project. For this purpose, we can use a cell addressing system to specify which cell data we need to import. It will return the value of a specific cell address from the Excel file. var cellValue = ws["Cell Address"]; var cellValue = ws["Cell Address"]; Dim cellValue = ws("Cell Address") $vbLabelText $csharpLabel We can also import cell data from Excel files by using row and column indexes. This line of code will return the value of the specified row and column index. var cellValueByIndex = ws.Rows[RowIndex].Columns[ColumnIndex]; var cellValueByIndex = ws.Rows[RowIndex].Columns[ColumnIndex]; Dim cellValueByIndex = ws.Rows(RowIndex).Columns(ColumnIndex) $vbLabelText $csharpLabel If we want to assign imported cell values to variables, we can use this code. // Import Data by Cell Address // by cell addressing string val = ws["Cell Address"].ToString(); // by row and column indexing string valWithIndexing = ws.Rows[RowIndex].Columns[ColumnIndex].Value.ToString(); // Import Data by Cell Address // by cell addressing string val = ws["Cell Address"].ToString(); // by row and column indexing string valWithIndexing = ws.Rows[RowIndex].Columns[ColumnIndex].Value.ToString(); ' Import Data by Cell Address ' by cell addressing Dim val As String = ws("Cell Address").ToString() ' by row and column indexing Dim valWithIndexing As String = ws.Rows(RowIndex).Columns(ColumnIndex).Value.ToString() $vbLabelText $csharpLabel In the above examples, the row and column index starts at 0. 4. Import Excel Data of Specific Range If we want to import data in a specific range from an Excel WorkBook, it can easily be done by using the range function. To define the range, we need to describe the starting and ending cell addresses. This way, it will return all the cell values within the specified range. var rangeData = ws["Starting Cell Address:Ending Cell Address"]; var rangeData = ws["Starting Cell Address:Ending Cell Address"]; Dim rangeData = ws("Starting Cell Address:Ending Cell Address") $vbLabelText $csharpLabel For more information about working with range in Excel files, check out the provided code examples. :path=/static-assets/excel/content-code-examples/how-to/csharp-import-excel-import.cs using IronXL; using System; // Import Excel WorkBook WorkBook wb = WorkBook.Load("sample.xlsx"); // Specify WorkSheet WorkSheet ws = wb.GetWorkSheet("Sheet1"); // Import data of specific cell string val = ws["A4"].Value.ToString(); Console.WriteLine("Import Value of A4 Cell address: {0}", val); Console.WriteLine("import Values in Range From B3 To B9 :\n"); // Import data in specific range foreach (var item in ws["B3:B9"]) { Console.WriteLine(item.Value.ToString()); } Console.ReadKey(); IRON VB CONVERTER ERROR developers@ironsoftware.com $vbLabelText $csharpLabel The above code displays the following output: With the values of Excel file sample.xlsx as: 5. Import Excel Data by Aggregate Functions We can also apply aggregate functions to Excel files and import the resulting data from these aggregate functions. Here are some examples of the different functions and how to use them. Sum() // To find the sum of a specific cell range var sum = ws["Starting Cell Address:Ending Cell Address"].Sum(); // To find the sum of a specific cell range var sum = ws["Starting Cell Address:Ending Cell Address"].Sum(); ' To find the sum of a specific cell range Dim sum = ws("Starting Cell Address:Ending Cell Address").Sum() $vbLabelText $csharpLabel Average() // To find the average of a specific cell range var average = ws["Starting Cell Address:Ending Cell Address"].Avg(); // To find the average of a specific cell range var average = ws["Starting Cell Address:Ending Cell Address"].Avg(); ' To find the average of a specific cell range Dim average = ws("Starting Cell Address:Ending Cell Address").Avg() $vbLabelText $csharpLabel Min() // To find the minimum in a specific cell range var minimum = ws["Starting Cell Address:Ending Cell Address"].Min(); // To find the minimum in a specific cell range var minimum = ws["Starting Cell Address:Ending Cell Address"].Min(); ' To find the minimum in a specific cell range Dim minimum = ws("Starting Cell Address:Ending Cell Address").Min() $vbLabelText $csharpLabel Max() // To find the maximum in a specific cell range var maximum = ws["Starting Cell Address:Ending Cell Address"].Max(); // To find the maximum in a specific cell range var maximum = ws["Starting Cell Address:Ending Cell Address"].Max(); ' To find the maximum in a specific cell range Dim maximum = ws("Starting Cell Address:Ending Cell Address").Max() $vbLabelText $csharpLabel You can read more about working with aggregate functions in Excel for C# and learn more about pulling data in different methods. Let's see an example of how to import Excel file data by applying these functions. :path=/static-assets/excel/content-code-examples/how-to/csharp-import-excel-math-functions.cs using IronXL; using System; // Import Excel file WorkBook wb = WorkBook.Load("sample.xlsx"); // Specify WorkSheet WorkSheet ws = wb.GetWorkSheet("Sheet1"); // Import Excel file data by applying aggregate functions decimal sum = ws["D2:D9"].Sum(); decimal avg = ws["D2:D9"].Avg(); decimal min = ws["D2:D9"].Min(); decimal max = ws["D2:D9"].Max(); Console.WriteLine("Sum From D2 To D9: {0}", sum); Console.WriteLine("Avg From D2 To D9: {0}", avg); Console.WriteLine("Min From D2 To D9: {0}", min); Console.WriteLine("Max From D2 To D9: {0}", max); Console.ReadKey(); IRON VB CONVERTER ERROR developers@ironsoftware.com $vbLabelText $csharpLabel The above code gives us this output: And our file sample.xlsx will have these values: 6. Import Complete Excel File Data If we want to import complete Excel file data into our C# project, we can first parse our loaded WorkBook into a DataSet. In this way, the complete Excel data would be imported into the DataSet, and WorkSheets on Excel files become DataTables within that DataSet. Here it is in action: // Import WorkBook into DataSet DataSet ds = wb.ToDataSet(); // Import WorkBook into DataSet DataSet ds = wb.ToDataSet(); ' Import WorkBook into DataSet Dim ds As DataSet = wb.ToDataSet() $vbLabelText $csharpLabel In this way, our specified WorkSheet will be imported into a DataSet that we can use according to our requirements. Often, the first column of an Excel file is used as ColumnName. In this case, we need to make the first column a DataTable ColumnName. To do this, we set the boolean parameter of ToDataSet() function of IronXL as follows: // Import WorkBook into DataSet with first row as ColumnNames DataSet ds = wb.ToDataSet(true); // Import WorkBook into DataSet with first row as ColumnNames DataSet ds = wb.ToDataSet(true); ' Import WorkBook into DataSet with first row as ColumnNames Dim ds As DataSet = wb.ToDataSet(True) $vbLabelText $csharpLabel This will make the first column of the Excel file as a DataTable ColumnName. Let's see a complete example of how to import Excel data into a DataSet and use the first column of an Excel WorkSheet as a DataTable ColumnName: :path=/static-assets/excel/content-code-examples/how-to/csharp-import-excel-dataset.cs using IronXL; using System; using System.Data; WorkBook wb = WorkBook.Load("sample.xlsx"); WorkSheet ws = wb.GetWorkSheet("Sheet1"); // Import Excel data into a DataSet DataSet ds = wb.ToDataSet(true); Console.WriteLine("Excel file data imported to dataset successfully."); Console.ReadKey(); IRON VB CONVERTER ERROR developers@ironsoftware.com $vbLabelText $csharpLabel Working with Excel Dataset and Datatable functions can be complicated, but we have more examples available for incorporating file data into your C# project. Library Quick Access Explore the IronXL Reference Learn more about pulling Excel data via cells, range, datasets and datatables in our full documentation API Reference for IronXl. Explore the IronXL Reference 常见问题解答 如何不使用Interop在C#中导入Excel文件? 您可以使用IronXL在C#中导入Excel文件,而无需Interop。只需使用WorkBook.Load()方法加载文件,并通过GetWorkSheet()等各种函数访问数据。 如何在C#项目中安装IronXL库? 通过在包管理器控制台中执行命令Install-Package IronXL.Excel,或直接从IronXL网站下载DLL来安装IronXL。 在IronXL中可用于访问特定工作表的方法有哪些? 您可以使用GetWorkSheet()方法通过提供工作表名称作为参数来访问IronXL中特定的工作表。 如何从特定单元格范围导入数据到C#? 使用IronXL,您可以通过在范围函数中定义开始和结束单元格地址来从特定单元格范围导入数据,从而实现精确的数据操作。 使用IronXL进行Excel数据管理在C#中的好处是什么? IronXL提供了Excel数据管理的灵活性,通过单元格、范围和聚合函数导入数据的功能。它简化了无需Interop的操作,并为高级数据处理提供了强大的API。 在导入Excel数据时,聚合函数如何使用? 您可以使用IronXL在从Excel文件导入数据时执行如Sum、Average、Min、Max等聚合函数,增强数据分析能力。 是否可以将Excel工作表转换为C#中的DataTables? 可以的,您可以通过使用IronXL加载工作簿并利用ToDataSet()方法,将Excel工作表转换为C#中的DataTables,从而促进广泛的数据管理。 我可以在C#中将Excel表格的第一行用作列标题吗? IronXL允许您通过将ToDataSet()函数的布尔参数设置为true来使用Excel表格的第一行作为列标题,从而将数据转换为结构化的DataTable。 我在哪里可以找到有关使用IronXL进行Excel操作的更多信息? 有关使用IronXL进行Excel操作的更多详情,请浏览IronXL提供的综合API参考,它提供了数据操作技术和高级功能的见解。 Curtis Chau 立即与工程团队聊天 技术作家 Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。 准备开始了吗? Nuget 下载 1,686,155 | 版本: 2025.11 刚刚发布 免费 NuGet 下载 总下载量:1,686,155 查看许可证