如何通过Next按钮逐行读取Excel数据并输出到文本框?
实现Excel逐行读取的Next按钮功能
现有用于读取Excel的C#代码如下,但当前ReadCell方法仅能读取指定单元格,无法配合Next按钮实现逐行读取并输出下一行数据的功能,需要调整代码实现该需求。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Microsoft.Office.Interop.Excel; using _Excel = Microsoft.Office.Interop.Excel; namespace text_extract { internal class Excel { string path = ""; _Application excel = new _Excel.Application(); Workbook wb; Worksheet ws; public Excel(string path, int Sheet) { this.path = path; wb = excel.Workbooks.Open(path); ws = wb.Worksheets[Sheet]; } public string ReadCell(int i, int j) { i++; j++; if (ws.Cells[i, j].Value2 != null) return ws.Cells[i, j].Value2; else return ""; } } }
解决方案步骤
1. 修改Excel类,添加行跟踪状态
在Excel类中添加私有字段记录当前读取行号,新增读取下一行数据的方法,同时实现资源释放逻辑避免Excel进程残留。
修改后的Excel类代码:
using System; using System.Collections.Generic; using Microsoft.Office.Interop.Excel; using _Excel = Microsoft.Office.Interop.Excel; namespace text_extract { internal class Excel : IDisposable { private string _path = ""; private _Application _excel = new _Excel.Application(); private Workbook _wb; private Worksheet _ws; private int _currentRow = 1; // 起始行,根据需求调整 public Excel(string path, int sheetIndex) { _path = path; _wb = _excel.Workbooks.Open(path); _ws = _wb.Worksheets[sheetIndex]; } // 读取当前行所有非空列数据,读完后自动切换到下一行 public List<string> GetNextRow() { var rowData = new List<string>(); int column = 1; // 逐列读取直到遇到空单元格 while (true) { var cellValue = _ws.Cells[_currentRow, column].Value2; if (cellValue == null) break; rowData.Add(cellValue.ToString()); column++; } if (rowData.Count > 0) _currentRow++; return rowData; } // 重载:读取当前行指定列范围的数据 public List<string> GetNextRow(int startColumn, int endColumn) { var rowData = new List<string>(); for (int column = startColumn; column <= endColumn; column++) { var cellValue = _ws.Cells[_currentRow, column].Value2; rowData.Add(cellValue?.ToString() ?? string.Empty); } _currentRow++; return rowData; } // 释放Excel资源,避免进程残留 public void Dispose() { _wb?.Close(); _excel?.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(_ws); System.Runtime.InteropServices.Marshal.ReleaseComObject(_wb); System.Runtime.InteropServices.Marshal.ReleaseComObject(_excel); } } }
2. 在UI层处理Next按钮点击事件
以WinForms为例,在窗体中实例化Excel读取器,在按钮点击事件中调用方法获取下一行数据并显示:
using System; using System.Windows.Forms; using text_extract; namespace YourAppNamespace { public partial class MainForm : Form { private Excel _excelReader; public MainForm() { InitializeComponent(); // 替换为你的Excel文件路径和工作表索引(从1开始) _excelReader = new Excel(@"C:\test.xlsx", 1); } private void btnNext_Click(object sender, EventArgs e) { var nextRow = _excelReader.GetNextRow(); if (nextRow.Count == 0) { MessageBox.Show("已读取到最后一行"); return; } // 将行数据拼接后显示在TextBox中 txtOutput.Text = string.Join(" | ", nextRow); } // 窗体关闭时释放资源 private void MainForm_FormClosed(object sender, FormClosedEventArgs e) { _excelReader?.Dispose(); } } }
关键说明
_currentRow字段跟踪当前读取位置,每次调用GetNextRow后自动递增,实现逐行读取逻辑。- 实现
IDisposable接口确保Excel进程被正确关闭,避免后台残留。 - 提供两种读取重载方法,可灵活选择读取所有非空列或指定列范围。
内容的提问来源于stack exchange,提问作者Gearzo
相关产品推荐
相关产品推荐

