FScada组态软件操作Excel文件例子

Excel演示项目

实现代码:
        public void Button_Click(object sender,RoutedEventArgs e)
        {
            //MessageBox.Show("ok");
            Excel excel = new Excel(); //创建Excel对象
            if (excel.LoadFromResource("realdata.xls")) //从项目中装载Excel文件
            {
                excel.SetCurrentSheet(0);                //设置当前工作表,后续操作在此工作表进行
                for (int i=1;i<28;i++){
                    string tagname =  excel.GetCellStringValue(i,1); //从工资表指定行列读取标签名称
                    if (!string.IsNullOrEmpty(tagname))
                    {
                       IChannel ch = Env.Current.GetChannel(tagname);
                       if (ch!=null)
                       excel.SetCellValue(i,2,ch.Value);                //设置数据
                    }
                    //如果Sheet内有公式 则加下列代码 强制更新计算
                    excel.CurrentSheetForceFormulaRecalculation = true;                
                }
                //excel.SetActiveSheet(0);  //保存时可以设置活动工作表,打开Excel显示的活动工作表
                string filename = System.IO.Path.Combine(Env.Current.Application.BasePath,"Reports","realdata.xls");
                excel.SaveToFile(filename);//Save to file
                //Excel.OpenExcel(filename,"print");  //Print Excel
                Excel.OpenExcel(filename);  //openExcel                
            }
        }