WinForm 实现打开并导入 Excel 文件读取功能
|
admin
2025年2月8日 0:32
本文热度 487
|
一、引言
在 Windows 桌面应用开发中,WinForm 是一种常见且强大的技术。而在许多业务场景下,需要从 Excel 文件中读取数据,例如数据分析、数据导入等。本文将详细介绍如何在 WinForm 应用程序中实现打开并导入 Excel 文件,以及读取其中的数据。
二、实现思路
要在 WinForm 中实现打开并读取 Excel 文件,主要步骤如下:
- 提供文件选择界面:使用
OpenFileDialog
控件让用户选择要打开的 Excel 文件。 - 选择合适的库读取 Excel:这里我们使用
Microsoft.Office.Interop.Excel
或 EPPlus
库,Microsoft.Office.Interop.Excel
需要安装 Microsoft Office,而 EPPlus
是一个开源库,无需安装 Office。本文将分别介绍这两种方法。 - 读取 Excel 文件内容:将 Excel 文件中的数据读取出来,并在界面上显示或进行后续处理。
三、使用 Microsoft.Office.Interop.Excel
实现
1. 创建 WinForm 项目
打开 Visual Studio,创建一个新的 C# WinForm 应用程序项目。
2. 添加引用
在项目中添加对 Microsoft.Office.Interop.Excel
的引用。可以通过“项目” -> “添加引用” -> “COM”,找到“Microsoft Excel XX.0 Object Library”(XX 为 Office 版本号)并添加。
3. 设计界面
在窗体上添加一个 Button
控件用于触发文件选择对话框,添加一个 DataGridView
控件用于显示读取的数据。
4. 编写代码
using System;
using System.Data;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;
namespace ExcelImportWinForm
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "Excel Files|*.xls;*.xlsx";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
string filePath = openFileDialog.FileName;
DataTable dataTable = ReadExcelWithInterop(filePath);
dataGridView1.DataSource = dataTable;
}
}
private DataTable ReadExcelWithInterop(string filePath)
{
DataTable dataTable = new DataTable();
Excel.Application excelApp = new Excel.Application();
Excel.Workbook workbook = excelApp.Workbooks.Open(filePath);
Excel.Worksheet worksheet = workbook.Sheets[1];
Excel.Range range = worksheet.UsedRange;
// 添加列
for (int col = 1; col <= range.Columns.Count; col++)
{
dataTable.Columns.Add(range.Cells[1, col].Value2.ToString());
}
// 添加行
for (int row = 2; row <= range.Rows.Count; row++)
{
DataRow dataRow = dataTable.NewRow();
for (int col = 1; col <= range.Columns.Count; col++)
{
dataRow[col - 1] = range.Cells[row, col].Value2;
}
dataTable.Rows.Add(dataRow);
}
workbook.Close(false);
excelApp.Quit();
return dataTable;
}
}
}
5. 代码解释
- 文件选择:使用
OpenFileDialog
让用户选择 Excel 文件。 - 读取 Excel:通过
Microsoft.Office.Interop.Excel
打开 Excel 文件,获取工作表和使用范围。 - 数据处理:将 Excel 中的数据逐行逐列读取到
DataTable
中,最后将 DataTable
绑定到 DataGridView
上显示。
四、使用 EPPlus
实现
1. 安装 EPPlus
库
通过 NuGet 包管理器搜索 EPPlus
并安装到项目中。
2. 编写代码
using System;
using System.Data;
using System.IO;
using System.Windows.Forms;
using OfficeOpenXml;
namespace ExcelImportWinForm
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "Excel Files|*.xls;*.xlsx";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
string filePath = openFileDialog.FileName;
DataTable dataTable = ReadExcelWithEPPlus(filePath);
dataGridView1.DataSource = dataTable;
}
}
private DataTable ReadExcelWithEPPlus(string filePath)
{
DataTable dataTable = new DataTable();
using (ExcelPackage package = new ExcelPackage(new FileInfo(filePath)))
{
ExcelWorksheet worksheet = package.Workbook.Worksheets[0];
if (worksheet.Dimension != null)
{
// 添加列
for (int col = 1; col <= worksheet.Dimension.End.Column; col++)
{
dataTable.Columns.Add(worksheet.Cells[1, col].Value?.ToString());
}
// 添加行
for (int row = 2; row <= worksheet.Dimension.End.Row; row++)
{
DataRow dataRow = dataTable.NewRow();
for (int col = 1; col <= worksheet.Dimension.End.Column; col++)
{
dataRow[col - 1] = worksheet.Cells[row, col].Value;
}
dataTable.Rows.Add(dataRow);
}
}
}
return dataTable;
}
}
}
3. 代码解释
- 文件选择:同样使用
OpenFileDialog
让用户选择 Excel 文件。 - 读取 Excel:使用
EPPlus
打开 Excel 文件,获取工作表和单元格范围。 - 数据处理:将 Excel 中的数据逐行逐列读取到
DataTable
中,最后将 DataTable
绑定到 DataGridView
上显示。
五、两种方法对比
- **
Microsoft.Office.Interop.Excel
**:优点是功能强大,能直接调用 Excel 的各种功能;缺点是需要安装 Microsoft Office,部署环境受限,且性能相对较低。 - **
EPPlus
**:优点是开源、无需安装 Office,性能较好,部署方便;缺点是功能相对 Microsoft.Office.Interop.Excel
可能不够全面。
六、总结
通过以上两种方法,我们可以在 WinForm 应用程序中实现打开并导入 Excel 文件,读取其中的数据。开发者可以根据实际需求和部署环境选择合适的方法。无论是使用 Microsoft.Office.Interop.Excel
还是 EPPlus
,都能满足基本的 Excel 数据读取需求,为后续的数据处理和分析提供基础。
阅读原文:原文链接
该文章在 2025/2/8 10:20:47 编辑过