You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让WinForm自动读取Excel数据并每10秒切换显示下一行?

实现WinForm自动轮询Excel数据(每10秒切换下一行)

针对你的需求,我们可以通过Timer控件实现定时自动切换,同时优化代码结构避免重复创建Excel实例,具体实现如下:

核心修改点

  • 添加System.Windows.Forms.Timer控件实现10秒定时触发
  • 维护类级变量记录当前读取的行索引,避免硬编码
  • 复用Excel实例减少文件IO操作
  • 封装数据读取逻辑,提升代码复用性

完整修改后代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace test_read_data_from_excel
{
    public partial class Form1 : Form
    {
        // 类级变量:记录当前读取的行号,复用Excel实例
        private int _currentRow = 1;
        private Excel _excel;

        public Form1()
        {
            InitializeComponent();
            // 初始化Timer
            InitializeTimer();
        }

        private void InitializeTimer()
        {
            // 创建Timer控件(也可以在设计器拖入)
            var timer = new Timer();
            timer.Interval = 10000; // 10秒,单位毫秒
            timer.Tick += Timer_Tick;
            timer.Enabled = true; // 启动定时器
            this.Controls.Add(timer);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // 初始化Excel实例并加载初始数据
            _excel = new Excel(@"Test.xlsx", 1);
            LoadDataByRow(_currentRow);
        }

        /// <summary>
        /// 根据行号加载对应单元格数据
        /// </summary>
        /// <param name="row">目标行号</param>
        private void LoadDataByRow(int row)
        {
            textBox1.Text = _excel.ReadCell(row, 0);
            textBox2.Text = _excel.ReadCell(row, 1);
            // 从你的代码逻辑看,textBox3对应的行是当前行+2(初始1→3,按钮点击2→4)
            textBox3.Text = _excel.ReadCell(row + 2, 2);
        }

        private void Timer_Tick(object sender, EventArgs e)
        {
            _currentRow++;
            // 可选:添加边界判断,比如到第10行就回到第1行循环
            // if (_currentRow > 10) _currentRow = 1;
            
            LoadDataByRow(_currentRow);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 frm = new Form2();
            frm.Show();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
        }

        private void button2_Click(object sender, EventArgs e)
        {
            // 手动切换下一行,和自动逻辑保持一致
            _currentRow++;
            LoadDataByRow(_currentRow);
        }

        // 释放Excel资源,避免内存泄漏
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            _excel?.Dispose(); // 需确保你的Excel类实现了IDisposable接口
        }
    }
}

关键说明

  1. Timer控件:通过Interval设置触发间隔(10000毫秒=10秒),Tick事件处理每次定时触发的逻辑,自动更新行号并加载数据。
  2. 复用Excel实例:在Form加载时创建一次Excel对象,避免每次读取都打开文件,提升性能。
  3. 行号逻辑:_currentRow跟踪当前读取的行,手动按钮点击和自动定时触发共用同一逻辑,保证行为一致。
  4. 资源释放:在Form关闭时调用Dispose释放Excel资源,防止内存泄漏(需要你的Excel类实现IDisposable接口)。
  5. 边界处理:如果需要限制最大行号或循环读取,可以在Timer_Tick事件中添加判断逻辑(示例代码中已注释)。

内容的提问来源于stack exchange,提问作者Gearzo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 14:20:38