C#: 导入excel文件到 dataGridView 控件

说明:文档介绍将 excel文件导入到 dataGridView 控件中的方法。

1.创建一个 dataGridView 控件 dataGridView_import_data,然后放置一个按键,给按键添加一个触发事件函数,函数内容如下。

2.在事件函数末尾添加了内存回收代码 ,测试时发现导入一个3M的excel文件后,软件占用内存会增加900M左右,添加GC.Collect();是为了快速让系统回收内存,如果不添加约几分钟后系统也会自动回收多余的内存。

GC.Collect();//强制进行垃圾回收

//导入excel数据到表格

private void button_wav_import_data_Click(object sender, EventArgs e)
{
	OpenFileDialog openFileDialog = new OpenFileDialog();
	openFileDialog.Filter = "Excel Files|*.xlsx";
	openFileDialog.Title = "Select an Excel File";

	if (openFileDialog.ShowDialog() == DialogResult.OK)
	{
		string filePath = openFileDialog.FileName;
		try
		{
			using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
			{
				IWorkbook workbook = new XSSFWorkbook(fs);
				ISheet sheet = workbook.GetSheetAt(0); // 获取第一个工作表

				DataTable dt = new DataTable();
				IRow headerRow = sheet.GetRow(0);

				// 创建列
				for (int i = 0; i < headerRow.LastCellNum; i++)
				{
					dt.Columns.Add(Convert.ToString(headerRow.GetCell(i)));
				}

				// 添加行数据
				for (int i = (sheet.FirstRowNum + 1); i <= sheet.LastRowNum; i++)
				{
					IRow row = sheet.GetRow(i);
					DataRow dataRow = dt.NewRow();

					for (int j = row.FirstCellNum; j < row.LastCellNum; j++)
					{
						if (row.GetCell(j) != null)
						{
							dataRow[j] = row.GetCell(j).ToString();
						}
					}

					dt.Rows.Add(dataRow);
				}
				dataGridView_import_data.DataSource = dt;
			}

			// 把列抬头名称赋值给 列的name
			string Columns_name = "";
			for (int i = 0; i < dataGridView_import_data.Columns.Count; i++)
			{
				Columns_name = dataGridView_import_data.Columns[i].HeaderText;
				dataGridView_import_data.Columns[i].Name = Columns_name;
			}
		}
		catch (Exception ex)
		{
			MessageBox.Show("Error: " + ex.Message);
		}
	}
	GC.Collect();//强制进行垃圾回收
}

  • 14
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好,使用C#DataGridView控件导入Excel文件的步骤如下: 1. 引用Microsoft.Office.Interop.Excel命名空间,以便使用Excel相关的类和方法。 2. 创建Excel应用程序对象和工作簿对象,打开Excel文件。 3. 获取Excel文件中的工作表,遍历每个工作表,将数据读取到DataTable中。 4. 将DataTable中的数据绑定到DataGridView控件中,显示在界面上。 下面是一个简单的示例代码: using System.Data; using Microsoft.Office.Interop.Excel; // 导入Excel文件DataGridView控件中 private void ImportExcelToDataGridView(string filePath) { // 创建Excel应用程序对象和工作簿对象 Application excelApp = new Application(); Workbook excelWorkbook = excelApp.Workbooks.Open(filePath); // 获取Excel文件中的工作表 foreach (Worksheet excelWorksheet in excelWorkbook.Worksheets) { // 将工作表中的数据读取到DataTable中 DataTable dataTable = new DataTable(); Range excelRange = excelWorksheet.UsedRange; for (int i = 1; i <= excelRange.Columns.Count; i++) { dataTable.Columns.Add(excelRange.Cells[1, i].Value.ToString()); } for (int i = 2; i <= excelRange.Rows.Count; i++) { DataRow dataRow = dataTable.NewRow(); for (int j = 1; j <= excelRange.Columns.Count; j++) { dataRow[j - 1] = excelRange.Cells[i, j].Value; } dataTable.Rows.Add(dataRow); } // 将DataTable中的数据绑定到DataGridView控件dataGridView1.DataSource = dataTable; } // 关闭Excel应用程序对象和工作簿对象 excelWorkbook.Close(); excelApp.Quit(); }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值