在.NET开发中,与Excel文件进行交互是一个常见的需求。无论是读取Excel数据还是将数据写入Excel,都需要高效且方便的解决方案。本文将详细介绍如何轻松实现.NET与Excel的无缝对接,包括读取、写入和格式化Excel数据。
一、准备工作
在开始之前,请确保您的开发环境中已经安装了以下内容:
- .NET开发环境:Visual Studio或其他支持.NET的开发工具。
- Excel库:常用的库有EPPlus、ClosedXML等。
以下是使用EPPlus库的示例代码,您需要在项目中引用EPPlus NuGet包。
using OfficeOpenXml;
二、读取Excel数据
2.1 使用EPPlus读取Excel文件
EPPlus是一个.NET库,可以用来读取和写入Excel文件。以下是一个简单的示例,展示如何使用EPPlus读取Excel文件中的数据。
using OfficeOpenXml;
using System;
using System.IO;
class Program
{
static void Main(string[] args)
{
string filePath = @"C:\path\to\your\file.xlsx";
using (ExcelPackage package = new ExcelPackage(new FileInfo(filePath)))
{
ExcelWorksheet worksheet = package.Workbook.Worksheets[0];
int rowCount = worksheet.Dimension.Rows;
int colCount = worksheet.Dimension.Columns;
for (int row = 1; row <= rowCount; row++)
{
for (int col = 1; col <= colCount; col++)
{
Console.Write(worksheet.Cells[row, col].Value + "\t");
}
Console.WriteLine();
}
}
}
}
2.2 使用ClosedXML读取Excel数据
ClosedXML也是一个常用的库,用于读取和写入Excel文件。以下是一个使用ClosedXML读取Excel文件的示例。
using ClosedXML;
using System;
using System.IO;
class Program
{
static void Main(string[] args)
{
string filePath = @"C:\path\to\your\file.xlsx";
using (var workbook = new XLWorkbook(filePath))
{
var worksheet = workbook.Worksheet(1);
var range = worksheet.Range("A1:C10");
foreach (var cell in range)
{
Console.WriteLine(cell.Value);
}
}
}
}
三、写入Excel数据
3.1 使用EPPlus写入Excel文件
以下是一个使用EPPlus将数据写入Excel文件的示例。
using OfficeOpenXml;
using System;
using System.IO;
class Program
{
static void Main(string[] args)
{
string filePath = @"C:\path\to\your\file.xlsx";
using (ExcelPackage package = new ExcelPackage())
{
ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("Sheet1");
worksheet.Cells["A1"].Value = "Name";
worksheet.Cells["B1"].Value = "Age";
worksheet.Cells["C1"].Value = "Country";
worksheet.Cells["A2"].Value = "John";
worksheet.Cells["B2"].Value = 25;
worksheet.Cells["C2"].Value = "USA";
package.SaveAs(new FileInfo(filePath));
}
}
}
3.2 使用ClosedXML写入Excel文件
以下是一个使用ClosedXML将数据写入Excel文件的示例。
using ClosedXML;
using System;
using System.IO;
class Program
{
static void Main(string[] args)
{
string filePath = @"C:\path\to\your\file.xlsx";
using (var workbook = new XLWorkbook())
{
var worksheet = workbook.Worksheet("Sheet1");
worksheet.Cell(1, 1).Value = "Name";
worksheet.Cell(1, 2).Value = "Age";
worksheet.Cell(1, 3).Value = "Country";
worksheet.Cell(2, 1).Value = "John";
worksheet.Cell(2, 2).Value = 25;
worksheet.Cell(2, 3).Value = "USA";
workbook.SaveAs(filePath);
}
}
}
四、格式化Excel数据
4.1 使用EPPlus格式化Excel数据
以下是一个使用EPPlus格式化Excel数据的示例。
using OfficeOpenXml;
using System;
using System.IO;
class Program
{
static void Main(string[] args)
{
string filePath = @"C:\path\to\your\file.xlsx";
using (ExcelPackage package = new ExcelPackage())
{
ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("Sheet1");
worksheet.Cells["A1"].Value = "Name";
worksheet.Cells["B1"].Value = "Age";
worksheet.Cells["C1"].Value = "Country";
worksheet.Cells["A2"].Value = "John";
worksheet.Cells["B2"].Value = 25;
worksheet.Cells["C2"].Value = "USA";
// Set the format for the age column
worksheet.Column(2).Style.Numberformat.Format = "#,##0";
package.SaveAs(new FileInfo(filePath));
}
}
}
4.2 使用ClosedXML格式化Excel数据
以下是一个使用ClosedXML格式化Excel数据的示例。
using ClosedXML;
using System;
using System.IO;
class Program
{
static void Main(string[] args)
{
string filePath = @"C:\path\to\your\file.xlsx";
using (var workbook = new XLWorkbook())
{
var worksheet = workbook.Worksheet("Sheet1");
worksheet.Cell(1, 1).Value = "Name";
worksheet.Cell(1, 2).Value = "Age";
worksheet.Cell(1, 3).Value = "Country";
worksheet.Cell(2, 1).Value = "John";
worksheet.Cell(2, 2).Value = 25;
worksheet.Cell(2, 3).Value = "USA";
// Set the format for the age column
worksheet.Column(2).Style.Numberformat.Format = "#,##0";
workbook.SaveAs(filePath);
}
}
}
五、总结
通过本文的介绍,您应该已经掌握了如何在.NET中轻松实现与Excel的无缝对接。无论是读取、写入还是格式化Excel数据,都可以使用EPPlus或ClosedXML等库来完成。希望本文对您的开发工作有所帮助。
