跳至页脚内容
C# VB.NET: 在Core中读取Excel文件 在Core中读取Excel文件
using IronXL;
using System;
using System.Linq;

// Supported for XLSX, XLS, XLSM, XLTX, CSV and TSV
WorkBook workBook = WorkBook.Load("sample.xlsx");

// Select worksheet at index 0
WorkSheet workSheet = workBook.WorkSheets[0];

// Get any existing worksheet
WorkSheet firstSheet = workBook.DefaultWorkSheet;

// Select a cell and return the converted value
int cellValue = workSheet["A2"].IntValue;

// Read from ranges of cells elegantly.
foreach (var cell in workSheet["A2:A10"])
{
    Console.WriteLine("Cell {0} has value '{1}'", cell.AddressString, cell.Text);
}

// Calculate aggregate values such as Min, Max and Sum
decimal sum = workSheet["A2:A10"].Sum();

// Linq compatible
decimal max = workSheet["A2:A10"].Max(c => c.DecimalValue);
Imports IronXL
Imports System
Imports System.Linq

' Supported for XLSX, XLS, XLSM, XLTX, CSV and TSV
Private workBook As WorkBook = WorkBook.Load("sample.xlsx")

' Select worksheet at index 0
Private workSheet As WorkSheet = workBook.WorkSheets(0)

' Get any existing worksheet
Private firstSheet As WorkSheet = workBook.DefaultWorkSheet

' Select a cell and return the converted value
Private cellValue As Integer = workSheet("A2").IntValue

' Read from ranges of cells elegantly.
For Each cell In workSheet("A2:A10")
	Console.WriteLine("Cell {0} has value '{1}'", cell.AddressString, cell.Text)
Next cell

' Calculate aggregate values such as Min, Max and Sum
Dim sum As Decimal = workSheet("A2:A10").Sum()

' Linq compatible
Dim max As Decimal = workSheet("A2:A10").Max(Function(c) c.DecimalValue)
Install-Package IronXL.Excel

The Load feature in IronXL allows you to easily load and manipulate existing Excel files in various formats (XLSX, XLS, CSV, etc.). By loading a workbook, you gain access to its worksheets and data, enabling you to extract values, loop through cell ranges, and perform calculations like sums or maximum values. You can access specific cells, convert their values to the appropriate data type (e.g., integers, decimals), and even use LINQ for advanced data operations. This feature simplifies working with Excel data in .NET, making it ideal for tasks like data analysis, report generation, and automation.

The WorkBook object is the key starting point for interacting with Excel files in IronXL. By using the WorkBook.Load("sample.xlsx") method, you open an existing Excel file, gaining access to its content and structure for further manipulation. This allows developers to work with pre-existing data, automate tasks, and update spreadsheets without having to create a new one from scratch.

Next, the WorkSheets[0] property is used to access the first worksheet in the workbook. In Excel, a workbook can contain multiple sheets, each representing a set of data organized into rows and columns. By selecting the first sheet, you're targeting the main area where your data is stored or where you'll perform operations like reading values or calculating results.

The range variable defines a specific group of cells, in this case, the range from A2 to A10. This is where IronXL allows you to interact with multiple cells at once, either by reading values, applying formatting, or performing calculations across ranges. In this example, it captures a column range, making it easy to work with sequential data.

The .Sum() method is used to calculate the sum of the numeric values in the selected range. This highlights how IronXL allows developers to perform quick, built-in calculations on ranges of data, eliminating the need for manual iteration and summing.

Finally, the Console.WriteLine() statement outputs the calculated sum to the console. This is a simple but powerful way to display the result of your operations, such as the sum of values in a range, directly within your application.

With this approach, developers can easily automate data aggregation tasks or integrate them into larger workflows.

Learn to Load and Manipulate Excel Files with IronXL

C# VB.NET: 处理中Excel工作表 处理中Excel工作表
using IronXL;

// Create new Excel spreadsheet
WorkBook workBook = WorkBook.Create(ExcelFileFormat.XLSX);

// Create worksheets (workSheet1, workSheet2, workSheet3)
WorkSheet workSheet1 = workBook.CreateWorkSheet("workSheet1");
WorkSheet workSheet2 = workBook.CreateWorkSheet("workSheet2");
WorkSheet workSheet3 = workBook.CreateWorkSheet("workSheet3");

// Set worksheet position (workSheet2, workSheet1, workSheet3)
workBook.SetSheetPosition("workSheet2", 0);

// Set active for workSheet3
workBook.SetActiveTab(2);

// Remove workSheet1
workBook.RemoveWorkSheet(1);

workBook.SaveAs("manageWorkSheet.xlsx");
Imports IronXL

' Create new Excel spreadsheet
Private workBook As WorkBook = WorkBook.Create(ExcelFileFormat.XLSX)

' Create worksheets (workSheet1, workSheet2, workSheet3)
Private workSheet1 As WorkSheet = workBook.CreateWorkSheet("workSheet1")
Private workSheet2 As WorkSheet = workBook.CreateWorkSheet("workSheet2")
Private workSheet3 As WorkSheet = workBook.CreateWorkSheet("workSheet3")

' Set worksheet position (workSheet2, workSheet1, workSheet3)
workBook.SetSheetPosition("workSheet2", 0)

' Set active for workSheet3
workBook.SetActiveTab(2)

' Remove workSheet1
workBook.RemoveWorkSheet(1)

workBook.SaveAs("manageWorkSheet.xlsx")
Install-Package IronXL.Excel

IronXL library makes managing worksheets using C# code as easy as possible. The actions of creating & deleting worksheets, changing worksheet positions, and setting the active worksheet in an Excel file can be achieved without using Office Interop.

Create Worksheet

The CreateWorkSheet method allows creating a worksheet. It requires the worksheet name as the only parameter.

  • This code snippet creates a new workbook and a new worksheet named "NewSheet" within that workbook.

Set Worksheet Position

The SetSheetPosition method can be used to change or move a worksheet's position. Two parameters are required: the worksheet name as a string and its index position as an integer.

  • This modifies the position of the worksheet named "SheetName" to the second position in the workbook (remember, indexing is zero-based).

Set Active Worksheet

Setting the active worksheet means setting which worksheet will be opened by default when the workbook is accessed. To achieve this, use the SetActiveTab method with the index position of the worksheet.

  • This sets the first worksheet as the active tab that opens by default.

Remove Worksheet

Removing a worksheet can also be done with IronXL. Use the RemoveWorkSheet method along with the index position of the worksheet. If the worksheet's position is unknown, the name of the worksheet can also be used to delete it.

  • The first example removes the first worksheet in the workbook by index.
  • The second example removes the worksheet named "SheetName".

Please note that all the index positions mentioned above utilize zero-based indexing, meaning the first element has an index of 0.

C# VB.NET: 创建新的Excel文件 创建新的Excel文件
using IronXL;

// Create new Excel WorkBook document
WorkBook workBook = WorkBook.Create();

// Convert XLSX to XLS
WorkBook xlsWorkBook = WorkBook.Create(ExcelFileFormat.XLS);

// Create a blank WorkSheet
WorkSheet workSheet = workBook.CreateWorkSheet("new_sheet");

// Add data and styles to the new worksheet
workSheet["A1"].Value = "Hello World";
workSheet["A1"].Style.WrapText = true;
workSheet["A2"].BoolValue = true;
workSheet["A2"].Style.BottomBorder.Type = IronXL.Styles.BorderType.Double;

// Save the excel file as XLS, XLSX, CSV, TSV, JSON, XML, HTML and streams
workBook.SaveAs("sample.xlsx");
Imports IronXL

' Create new Excel WorkBook document
Private workBook As WorkBook = WorkBook.Create()

' Convert XLSX to XLS
Private xlsWorkBook As WorkBook = WorkBook.Create(ExcelFileFormat.XLS)

' Create a blank WorkSheet
Private workSheet As WorkSheet = workBook.CreateWorkSheet("new_sheet")

' Add data and styles to the new worksheet
Private workSheet("A1").Value = "Hello World"
Private workSheet("A1").Style.WrapText = True
Private workSheet("A2").BoolValue = True
Private workSheet("A2").Style.BottomBorder.Type = IronXL.Styles.BorderType.Double

' Save the excel file as XLS, XLSX, CSV, TSV, JSON, XML, HTML and streams
workBook.SaveAs("sample.xlsx")
Install-Package IronXL.Excel

The Create Excel feature provides developers with a powerful way to generate and manage Excel files directly within .NET applications.

It streamlines tasks like creating new workbooks, adding worksheets, and inputting data while offering advanced styling options such as text wrapping, borders, and formatting. This feature is perfect for automating reports, exporting data, or creating dynamic spreadsheets. IronXL supports multiple file formats, including XLS, XLSX, CSV, and more, ensuring compatibility with various systems. With its user-friendly API, developers can focus on core functionality while saving time and reducing complexity in Excel file management.

The WorkBook object serves as the foundation of any Excel file. By initializing a new workbook, developers gain access to a blank Excel document, ready to be populated with data and formatted as needed.

Every Excel workbook requires at least one worksheet. The next step demonstrates how to create a new sheet within the workbook and assign it a custom name. Worksheets act as the primary structure for organizing data in rows and columns.

The snippet demonstrates how to add content by assigning the value "Hello World" to a specific cell, A1. This shows how easy it is to populate data into an Excel sheet programmatically, enabling developers to automate tasks like report generation and data export.

Additionally, the snippet applies styling to the cell by enabling the WrapText property. This ensures that lengthy content in the cell is displayed neatly across multiple lines, improving readability and preventing text from being cut off. Such styling options empower developers to create visually appealing and professional spreadsheets.

Explore the Step-by-Step Guide to Create Excel Files in C#

C# VB.NET: 转换电子表格文件类型 转换电子表格文件类型
using IronXL;
using System.IO;

// Import any XLSX, XLS, XLSM, XLTX, CSV and TSV
WorkBook workBook = WorkBook.Load("sample.xlsx");

// Export the excel file as XLS, XLSX, XLSM, CSV, TSV, JSON, XML
workBook.SaveAs("sample.xls");
workBook.SaveAs("sample.xlsx");
workBook.SaveAs("sample.tsv");
workBook.SaveAsCsv("sample.csv");
workBook.SaveAsJson("sample.json");
workBook.SaveAsXml("sample.xml");

// Export the excel file as Html, Html string
workBook.ExportToHtml("sample.html");
string htmlString = workBook.ExportToHtmlString();

// Export the excel file as Binary, Byte array, Data set, Stream
byte[] binary = workBook.ToBinary();
byte[] byteArray = workBook.ToByteArray();
System.Data.DataSet dataSet = workBook.ToDataSet(); // Allow easy integration with DataGrids, SQL and EF
Stream stream = workBook.ToStream();
Imports IronXL
Imports System.IO

' Import any XLSX, XLS, XLSM, XLTX, CSV and TSV
Private workBook As WorkBook = WorkBook.Load("sample.xlsx")

' Export the excel file as XLS, XLSX, XLSM, CSV, TSV, JSON, XML
workBook.SaveAs("sample.xls")
workBook.SaveAs("sample.xlsx")
workBook.SaveAs("sample.tsv")
workBook.SaveAsCsv("sample.csv")
workBook.SaveAsJson("sample.json")
workBook.SaveAsXml("sample.xml")

' Export the excel file as Html, Html string
workBook.ExportToHtml("sample.html")
Dim htmlString As String = workBook.ExportToHtmlString()

' Export the excel file as Binary, Byte array, Data set, Stream
Dim binary() As Byte = workBook.ToBinary()
Dim byteArray() As Byte = workBook.ToByteArray()
Dim dataSet As System.Data.DataSet = workBook.ToDataSet() ' Allow easy integration with DataGrids, SQL and EF
Dim stream As Stream = workBook.ToStream()
Install-Package IronXL.Excel

IronXL offers a simple and flexible way for developers to work with Excel files, allowing easy import and export across various formats. It supports file types like XLS, XLSX, CSV, JSON, XML, and HTML, making it ideal for different application needs. Developers can convert Excel data into formats suitable for web integration, APIs, or further data processing with just a few lines of code.

For example, exporting Excel files as HTML or JSON makes it easy to embed data into web applications, while converting them to binary, byte arrays, or streams helps integrate with databases or other systems. Converting Excel content into a DataSet simplifies connecting to SQL databases or populating UI components like data grids. This reduces manual coding, enhances productivity, and makes IronXL a valuable tool for modern, data-driven applications.

5 Steps to Export Excel Files in Multiple Formats

This code demonstrates the versatility of IronXL in handling and exporting Excel data across multiple formats. The process begins by loading an existing Excel file, Sample.xlsx, into a WorkBook object. This object acts as the central hub for manipulating and exporting data. Using the SaveAs method, the workbook is saved as an .xlsx file, ensuring compatibility with modern Excel applications.

Next, the SaveAsJson method converts the workbook into JSON format, making it suitable for API responses or lightweight data exchanges. The ToDataSet method enables seamless integration with databases or UI components by converting the workbook into a DataSet object, which is a .NET-native structure for handling tabular data.

Finally, the ExportToHtml method generates an HTML file, enabling developers to display the Excel data directly on web pages. These capabilities streamline data processing tasks, making IronXL an exceptional tool for developers working with Excel files.

Learn to Convert Excel File Types Effortlessly

C# VB.NET: 通过System.Data.DataSet将Excel转换为SQL 通过System.Data.DataSet将Excel转换为SQL
using IronXL;
using System;
using System.Data;

// Supported for XLSX, XLS, XLSM, XLTX, CSV and TSV
WorkBook workBook = WorkBook.Load("sample.xlsx");

// Convert the whole Excel WorkBook to a DataSet
DataSet dataSet = workBook.ToDataSet();

foreach (DataTable table in dataSet.Tables)
{
    Console.WriteLine(table.TableName);

    // Enumerate by rows or columns first at your preference
    foreach (DataRow row in table.Rows)
    {
        for (int i = 0 ; i < table.Columns.Count ; i++)
        {
            Console.Write(row[i]);
        }
    }
}
Imports IronXL
Imports System
Imports System.Data

' Supported for XLSX, XLS, XLSM, XLTX, CSV and TSV
Private workBook As WorkBook = WorkBook.Load("sample.xlsx")

' Convert the whole Excel WorkBook to a DataSet
Private dataSet As DataSet = workBook.ToDataSet()

For Each table As DataTable In dataSet.Tables
	Console.WriteLine(table.TableName)

	' Enumerate by rows or columns first at your preference
	For Each row As DataRow In table.Rows
		For i As Integer = 0 To table.Columns.Count - 1
			Console.Write(row(i))
		Next i
	Next row
Next table
Install-Package IronXL.Excel

Convert any XLSX, XLS, XLSM, XLTX, CSV and TSV file to a System.Data.DataTable for full interoperability with System.Data.SQL or to populate a DataGrid. This DataTable object can contain a collection of tables, relationships, and constraints.

Learn how to export DataTables in C# with our guide.

C# VB.NET: 通过DataTable将Excel转换为SQL和DataGrid 通过DataTable将Excel转换为SQL和DataGrid
using IronXL;
using System;
using System.Data;

// Supported for XLSX, XLS, XLSM, XLTX, CSV and TSV
WorkBook workBook = WorkBook.Load("sample.xlsx");

// Select default sheet
WorkSheet workSheet = workBook.DefaultWorkSheet;

// Convert the worksheet to DataTable
DataTable dataTable = workSheet.ToDataTable(true);

// Enumerate by rows or columns first at your preference
foreach (DataRow row in dataTable.Rows)
{
    for (int i = 0 ; i < dataTable.Columns.Count ; i++)
    {
        Console.Write(row[i]);
    }
}
Imports IronXL
Imports System
Imports System.Data

' Supported for XLSX, XLS, XLSM, XLTX, CSV and TSV
Private workBook As WorkBook = WorkBook.Load("sample.xlsx")

' Select default sheet
Private workSheet As WorkSheet = workBook.DefaultWorkSheet

' Convert the worksheet to DataTable
Private dataTable As DataTable = workSheet.ToDataTable(True)

' Enumerate by rows or columns first at your preference
For Each row As DataRow In dataTable.Rows
	For i As Integer = 0 To dataTable.Columns.Count - 1
		Console.Write(row(i))
	Next i
Next row
Install-Package IronXL.Excel

Convert XLSX, XLS, XLSM, XLTX, CSV and TSV file to a System.Data.DataTable for full interoperability with System.Data.SQL or to populate a DataGrid.

Input true to ToDataTable method to set the first row as column names of the table. The DataTable can populate a DataGrid.

Learn to Export DataTable to Excel in C#

Human Support related to .NET Core Excel库

来自.NET Core团队的技术支持

与我们的产品开发和支持团队取得联系。我们的团队随时为您解答关于许可、产品和您项目使用的问题。

提交支持票
Excel In Csharp NET Cropped related to .NET Core Excel库

在.NET Core中处理Excel文件

一种快速简单的方法,在dot net core中处理Excel和其他电子表格文档。在dotnet core中适用于包括Azure在内的多个平台,无需额外依赖或安装MS Office或Interop。

适用于.NET Core, C#, .NET, XLSX, VB.NET

功能列表
Read And Write Multiple Formats Cropped related to .NET Core Excel库

打开和保存多种文件格式

IronXL允许.NET工程师从dotnet Core应用撰写和导出Excel电子表格。打开和编辑XLS/XLSX/CSV/TSV - 保存和导出为XLS/XLSX/CSV/TSV/JSON。IronXL在解析、修改和保存Excel文档方面超过了免费和Interop库。

开始
Fast Native SQL Support Cropped related to .NET Core Excel库

SQL开箱即用的支持

通过将电子表格打开为System.Data.DataSet和System.Data.DataTable对象,在Excel、SQL和GridViews之间转移数据。

查看代码示例
Edit Excel Data Formulas Filters Sorting Cropped related to .NET Core Excel库

修改Excel数据

编辑微软Excel公式 - 每次工作表更改时重新计算。快速简单地格式化工作表[“A1:B10”]。合并并设置范围、列和行。

文档
Style Cells Cropped related to .NET Core Excel库

为您的Excel文档设定样式

设置字体、大小、背景、边框、对齐和数字格式。

开始
C#创建Excel电子表格.NET Core库 - 用于Excel文档创建和编辑。

易于使用Visual Studio构建.NET Core项目

IronXL通过intellisense支持将Excel工具置于您手中。通过NuGet为Visual Studio安装或手动下载DLL,免费用于.NET Core。您只需一眨眼的工夫即可设置完成。

下载DLL PM > Install-Package IronXL.Excel
支持:
  • .NET Core 2.0及以上
  • .NET Framework 4.0 及以上版本支持 C#、VB、F#
  • Microsoft Visual Studio。 .NET 开发 IDE 图标
  • NuGet 安装程序支持 Visual Studio
  • JetBrains ReSharper C# 语言助手兼容
  • Microsoft Azure C# .NET 托管平台兼容

.NET Core的许可和定价

免费用于开发。许可证从$749起。

项目 C# + VB.NET 库授权

项目

开发者C# + VB.NET库许可

开发者

组织C# + VB.NET库许可

组织

代理商C# + VB.NET库许可

代理商

SaaS C# + VB.NET库许可

SaaS

OEM C# + VB.NET库许可

OEM

查看完整的许可证选项  

.NET Core的Excel电子表格教程

如何在C#中创建Excel文件

C# Excel ASP.NET

Jonas Schmidt - C# 开发者

如何在.NET Core中创建Excel文件

查看Jonas如何使用IronXL生成Excel文件而不使用Office Interop...

查看Jonas的Excel文件生成教程
阅读和写入Excel教程+ C# & VB.NET代码示例

C# Excel XLS

Elijah Williams - 产品开发工程师

在C#或VB.NET中打开和编辑Excel文件

Elijah分享了一种务实的方法,将Excel数据读取到C#应用程序中...

查看Elijah的Excel阅读和编辑教程
教程 + 代码示例VB.NET PDF创建和编辑 | VB.NET & ASP.NET PDF

C# Excel 数据

Rebecca White - .NET解决方案总监

如何将Excel文件读取到.NET应用程序中。

Rebecca的代码讲解如何将Excel数据读取到.NET应用程序中...

阅读Becky的C#教程
.NET Core工程师使用IronXL...

会计和金融系统

  • # 收据
  • # 报告
  • # 发票打印
为ASP.NET会计和财务系统添加Excel支持

业务数字化

  • # 文档
  • # 订购和标签
  • # 纸质替代
.NET Core业务数字化用例

企业内容管理

  • # 内容制作
  • # 文档管理
  • # 内容分发
为.NET CMS添加Excel支持

数据和报告应用程序

  • # 性能跟踪
  • # 趋势映射
  • # 报告
.NET Core Excel报告
Iron Software企业.NET组件开发者

数千家企业、政府、中小企业和开发者信赖Iron软件产品。

Iron团队在.NET软件组件市场拥有超过10年的经验。

Iron Customer Icon
Iron Customer Icon
Iron Customer Icon
Iron Customer Icon
Iron Customer Icon
Iron Customer Icon
Iron Customer Icon
Iron Customer Icon