-
通过 C# 将数据写入到 Excel 表格
通过 C# 将数据写入到 Excel 表格
在日常的开发工作中,我们经常会遇到需要将数据导出到 Excel 表格的需求。C# 提供了多种方式来实现这一功能,其中较为常用的是使用 EPPlus 和 Microsoft.Office.Interop.Excel 两种库。本文将详细介绍如何使用这两种库将数据写入到 Excel 表格中,并探讨它们的优缺点。
一、使用 EPPlus 库
- 引入库
首先,需要通过 NuGet 包管理器安装 EPPlus 库。在 Visual Studio 中,打开 “工具” > “NuGet 包管理器” > “管理解决方案的 NuGet 包”,搜索并安装 “EPPlus” 库。
- 创建 Excel 文件
using OfficeOpenXml;
using System;
using System.Collections.Generic;
class Program
{
static void Main(string[] args)
{
// 创建一个新的 Excel 包
var package = new ExcelPackage();
var workbook = package.Workbook;
var worksheet = workbook.Worksheets.Add("Sheet1");
// 准备数据
List<Person> people = new List<Person>
{
new Person { Name = "John Doe", Age = 30, Email = "john.doe@example.com" },
new Person { Name = "Jane Smith", Age = 25, Email = "jane.smith@example.com" },
new Person { Name = "Bob Johnson", Age = 35, Email = "bob.johnson@example.com" }
};
// 写入表头
worksheet.Cells[1, 1].Value = "Name";
worksheet.Cells[1, 2].Value = "Age";
worksheet.Cells[1, 3].Value = "Email";
// 写入数据
for (int i = 0; i < people.Count; i++)
{
worksheet.Cells[i + 2, 1].Value = people[i].Name;
worksheet.Cells[i + 2, 2].Value = people[i].Age;
worksheet.Cells[i + 2, 3].Value = people[i].Email;
}
// 保存文件
package.SaveAs(new System.IO.FileInfo("People.xlsx"));
Console.WriteLine("Excel 文件已创建!");
}
}
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public string Email { get; set; }
}
- 读取 Excel 文件
using OfficeOpenXml;
using System;
using System.Collections.Generic;
class Program
{
static void Main(string[] args)
{
// 打开现有的 Excel 包
var package = new ExcelPackage(new System.IO.FileInfo("People.xlsx"));
var workbook = package.Workbook;
var worksheet = workbook.Worksheets["Sheet1"];
// 读取数据
List<Person> people = new List<Person>();
for (int i = 1; i <= worksheet.Dimension.Rows; i++)
{
if (i == 1) continue; // 跳过表头
people.Add(new Person
{
Name = worksheet.Cells[i, 1].Text,
Age = int.Parse(worksheet.Cells[i, 2].Text),
Email = worksheet.Cells[i, 3].Text
});
}
// 输出数据
foreach (var person in people)
{
Console.WriteLine($"Name: {person.Name}, Age: {person.Age}, Email: {person.Email}");
}
}
}
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public string Email { get; set; }
}
- 优点
- 易于使用:EPPlus 提供了简单易用的 API,可以轻松地创建和操作 Excel 文件。
- 高性能:EPPlus 在处理大数据量时表现出色,不会出现性能瓶颈。
- 无需安装 Excel:EPPlus 不需要在服务器上安装 Excel,适合在服务器端使用。
- 缺点
- 功能有限:EPPlus 的功能相对有限,无法实现一些高级功能,如数据透视表和图表。
二、使用 Microsoft.Office.Interop.Excel 库
- 引入库
首先,需要通过 NuGet 包管理器安装 Microsoft.Office.Interop.Excel 库。在 Visual Studio 中,打开 “工具” > “NuGet 包管理器” > “管理解决方案的 NuGet 包”,搜索并安装 “Microsoft.Office.Interop.Excel” 库。
- 创建 Excel 文件
using Microsoft.Office.Interop.Excel;
using System;
using System.Collections.Generic;
class Program
{
static void Main(string[] args)
{
// 创建一个新的 Excel 应用程序
Application excelApp = new Application();
Workbook workbook = excelApp.Workbooks.Add();
Worksheet worksheet = workbook.Sheets[1];
// 准备数据
List<Person> people = new List<Person>
{
new Person { Name = "John Doe", Age = 30, Email = "john.doe@example.com" },
new Person { Name = "Jane Smith", Age = 25, Email = "jane.smith@example.com" },
new Person { Name = "Bob Johnson", Age = 35, Email = "bob.johnson@example.com" }
};
// 写入表头
worksheet.Cells[1, 1].Value = "Name";
worksheet.Cells[1, 2].Value = "Age";
worksheet.Cells[1, 3].Value = "Email";
// 写入数据
for (int i = 0; i < people.Count; i++)
{
worksheet.Cells[i + 2, 1].Value = people[i].Name;
worksheet.Cells[i + 2, 2].Value = people[i].Age;
worksheet.Cells[i + 2, 3].Value = people[i].Email;
}
// 保存文件
workbook.SaveAs("People.xlsx");
workbook.Close();
excelApp.Quit();
Console.WriteLine("Excel 文件已创建!");
}
}
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public string Email { get; set; }
}
3. 读取 Excel 文件
using Microsoft.Office.Interop.Excel;
using System;
using System.Collections.Generic;
class Program
{
static void Main(string[] args)
{
// 创建一个新的 Excel 应用程序
Application excelApp = new Application();
Workbook workbook = excelApp.Workbooks.Open("People.xlsx");
Worksheet worksheet = workbook.Sheets[1];
// 读取数据
List<Person> people = new List<Person>();
for (int i = 1; i <= worksheet.UsedRange.Rows.Count; i++)
{
if (i == 1) continue; // 跳过表头
people.Add(new Person
{
Name = worksheet.Cells[i, 1].Text,
Age = int.Parse(worksheet.Cells[i, 2].Text),
Email = worksheet.Cells[i, 3].Text
});
}
// 输出数据
foreach (var person in people)
{
Console.WriteLine($"Name: {person.Name}, Age: {person.Age}, Email: {person.Email}");
}
workbook.Close();
excelApp.Quit();
}
}
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public string Email { get; set; }
}
- 优点
- 功能强大:Microsoft.Office.Interop.Excel 提供了丰富的功能,可以实现几乎所有的 Excel 操作。
- 与 Excel 集成:Microsoft.Office.Interop.Excel 与 Excel 应用程序紧密集成,可以实现与 Excel 应用程序的无缝交互。
- 缺点
- 需要安装 Excel:Microsoft.Office.Interop.Excel 需要在服务器上安装 Excel,不适合在服务器端使用。
- 性能较低:Microsoft.Office.Interop.Excel 在处理大数据量时性能较低,容易出现性能瓶颈。
三、总结
通过 C# 将数据写入到 Excel 表格中,可以使用 EPPlus 和 Microsoft.Office.Interop.Excel 两种库。EPPlus 提供了简单易用的 API,适合在服务器端使用;Microsoft.Office.Interop.Excel 提供了丰富的功能,适合与 Excel 应用程序进行交互。在实际应用中,可以根据需求选择合适的库来实现数据的导出功能。
最后,如果你对python语言还有任何疑问或者需要进一步的帮助,请访问https://www.xin3721.com 本站原创,转载请注明出处:https://www.xin3721.com