在实际环境中测试
在生产中测试无水印。
随时随地为您服务。
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()
通过IronXL库,可以非常方便、安全地将常用电子表格文件格式转换为.Net项目下一流程所需的格式。可用格式如下所示:
工作簿
类来加载或创建新的 XLS 或 XLSX工作簿
类来导出电子表格。将 Excel 导出为System.Data.DataSet
和System.Data.DataTable
对象,可轻松实现与DataGrids
、SQL 和 EF 的互操作或集成。
您可以从以下链接下载文件项目 链接.
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)
IronXL是一款适用于C#和.NET的Excel库,它允许开发人员在不使用Microsoft.Office.Interop.Excel_的情况下,从XLSX、XLS、XLSM、XLTX、CSV和TSV文件中加载/读取Excel数据。虽然所有可用的文件格式都可以用 Load
方法读取,但建议使用 LoadCSV
方法读取 CSV 文件格式。
工作表代表工作簿中的一个页面或选项卡。每个工作表都可以选择阅读和操作。
workBook.WorkSheets [0]
workBook.GetWorkSheet("工作表")
workBook.DefaultWorkSheet
属性此外,还可以从工作表中选择每个范围、行和列**,以访问/修改单元格的数据并应用公式。
导航至 选择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")
IronXL 库使使用 C# 代码管理工作表变得尽可能简单。创建和删除工作表、更改工作表位置、设置 Excel 文件中的活动工作表等操作无需使用 Office Interop 即可实现。
使用 CreateWorkSheet
方法可以创建工作表。它只需要工作表名称作为参数。
SetSheetPosition
方法可用于更改或移动工作表的位置。需要两个参数。工作表名称(字符串)及其索引位置(整数)。
设置活动工作表是指设置打开工作簿时默认打开哪个工作表。为此,请使用带有工作表索引位置的 SetActiveTab
方法。
也可以使用 IronXL 删除工作表。使用 RemoveWorkSheet
方法以及工作表的索引位置。如果工作表的位置未知,也可以使用工作表的名称来删除工作表。
请注意,上述所有索引位置均采用基于零的索引方式。
创建工作表
方法创建工作表设置工作表位置
设置活动标签
设置默认打开哪个工作表删除工作表
C# 函数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")
IronXL 库可用于创建 XLS 和 XLSX 格式的 Excel 文档。使用 IronXL 直观的 API 编辑和填充工作簿。使用 Value
属性访问单元格的值。使用 IronXL 还可以更改单元格的样式。
下面的 "样式 "属性都可以配置:
请注意,对于 CSV、TSV、JSON 和 XML 文件格式,将为每个工作表创建相应的文件。命名约定为 fileName.sheetName.format
。在上面的示例中,CSV 格式的输出将是 sample.new_sheet.csv
.
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
将任何 XLSX、XLS、XLSM、XLTX、CSV 和 TSV 文件转换为 "System.Data.DataTable",以便与 "System.Data.SQL "完全互操作或填充数据网格。数据表 "对象可包含表、关系和约束的集合。
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
将 XLSX、XLS、XLSM、XLTX、CSV 和 TSV 文件转换为 "System.Data.DataTable",以便与 "System.Data.SQL "完全互操作或填充 "DataGrid"。
向 ToDataTable
方法输入 true
,可将第一行设置为表格的列名。数据表 "可以填充 "数据网格"。
您可以从此处下载文件项目 链接.
数据表
从数据中使用 To数据表
.IronXL允许您在.NET应用程序中快速读取和写入Excel电子表格。读取和编辑XLS/XLSX/CSV/TSV - 保存和导出为XLS/XLSX/CSV/TSV/JSON。IronXL在解析、编辑和保存Excel文件方面优于免费的和Interop库。
了解更多IronXL 使用 Visual Studio 快速创建 XLSX 文件。通过 NuGet 或 DLL 安装。
PM > Install-Package IronXL.Excel 下载DLLC# Excel XLSX
C# Excel 电子表格
C# Excel 数据
Iron的团队在.NET软件组件市场拥有超过10年的经验。