progressbar 使用 Excel取数据

本文介绍了一个从Excel文件中读取数据并实时更新UI进度条的应用案例。该案例利用了.NET框架下的Microsoft Office Interop Excel库进行数据读取,并通过WPF实现了友好的用户界面交互。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近项目中需要使用Excel取数据。

引用 using Microsoft.Office.Interop.Excel;。需要在项目中引用11.0库类。

public  class BillRepository
    {
        public delegate void  ExcelRowsChangedDelegate(object sender, TaoBaoBillEventArsg e);//声明一个委托
        public event ExcelRowsChangedDelegate ExcelRowsChanged;//声明一个事件。
        /// <summary>
        /// 获取支付宝上的订单
        /// </summary>
        public  void getTaobaoBill(string filePath)
        {
            Bills bills=new Bills();
            UInt32 totalRows;
            Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
            Microsoft.Office.Interop.Excel.Workbook wk = excel.Workbooks.Open(filePath);
            Worksheet ws = wk.Worksheets[1];
            string DateCell = ws.Rows[3].Cells[1].Value;
            Regex reg = new Regex(@"\[(.+?)\]");
            var regColl = reg.Matches(DateCell);
            for (int i = 0; i < regColl.Count; i++)
            {
                string time = regColl[i].ToString().Replace("年", "-").Replace("月", "-").Replace("日", "");
                switch (i)
                {
                    case 0:
                        bills.firstDate = DateTime.Parse(time.Substring(1,time.Length-2));
                        break;
                    default:
                        bills.lastDate = DateTime.Parse(time.Substring(1, time.Length - 2));
                        break;
                }
            }
            Int32 firstRowINdex = 6;
            Int32 lastRowINdex = ws.UsedRange.Rows.Count - 4;
            totalRows = (UInt32)(lastRowINdex - firstRowINdex);
            if (ExcelRowsChanged != null)
            {
                ExcelRowsChanged(this, new TaoBaoBillEventArsg() { counterRows = totalRows, CurrentRowIndex = 0 });
            }


            PayType result;
            for (int i = firstRowINdex; i < lastRowINdex; i++)
            {
                Bill tempBill = new Bill();
                if (Enum.TryParse<PayType>((string)ws.Rows[i].Cells[11].Value, out result))
                {
                    Decimal payment = (Decimal)ws.Rows[i].Cells[8].Value + (Decimal)ws.Rows[i].Cells[7].Value;
                    string orderno = (string)ws.Rows[i].Cells[3].Value;
                    tempBill.order = orderno.Substring(5, orderno.Length - 5);
                    tempBill.payment = payment;
                    bills.Trades.Add(tempBill);

//当值更改时触发事件
                    if (ExcelRowsChanged != null)
                    {
                        ExcelRowsChanged(this, new TaoBaoBillEventArsg() { counterRows = totalRows, CurrentRowIndex = (double)i });
                    }
                }
            }
            excel.Quit();
        }
    }

这个类是准备为事件输入参数准备的。
    public class TaoBaoBillEventArsg:EventArgs
    {
        public UInt32 counterRows { get; set; }
        public double CurrentRowIndex { get; set; }
    }

//以下是在另一个cs文件中。

namespace TaoBaoBillCheck
{

delegate void updateProgressBar(System.Windows.DependencyProperty dp,object value);//这个是在类的外面建立的。

public void getTaobaoData(object sender, EventArgs e)
        {

//先分配一个progressBar的图形界面,需要建立grid,。
            string filePath = @"C:\Users\dypeng\Desktop\淘宝打印生成文件\test.xls";
            Border border = new Border();
            border.BorderThickness = new Thickness(2d);
            border.BorderBrush=new SolidColorBrush(Color.FromRgb(200,200,200));
            ProgressBar progbar = new ProgressBar();
            Canvas canvas = new Canvas();
            border.Child = canvas;
            border.Width = 300;
            border.Height = 100;
            canvas.Background = this.Background;
            Label label = new Label();
            label.Content = "解析进度";
            canvas.Children.Add(label);
            canvas.Children.Add(progbar);
            progbar.IsIndeterminate = false;
            progbar.Orientation = Orientation.Horizontal;
            progbar.Width = 250;
            progbar.Height = 15;
            StaPan1.Children.Add(border);
            border.Margin = new Thickness(100, 100, 0, 0);
            progbar.Margin = new Thickness(20, 40, 0, 0);


            BillRepository billRepository = new BillRepository();
            updateProgressBar updateprogressBar = new updateProgressBar(progbar.SetValue);//实例化updateProgressBar 。


            billRepository.ExcelRowsChanged += (o, i) =>
            {
                progbar.Minimum = 0;
                //progbar.Value = i.CurrentRowIndex;
                progbar.Maximum = (Double)i.counterRows;

//i注意ProgressBar.ValueProperty,它并非使用的实例。
                Dispatcher.Invoke(updateprogressBar, System.Windows.Threading.DispatcherPriority.Background, new object[] { ProgressBar.ValueProperty, i.CurrentRowIndex });
            };
            billRepository.getTaobaoBill(filePath);
            StaPan1.Children.Remove(border);
        }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值