PgCurve.xaml.cs 22.8 KB
Newer Older
潘栩锋's avatar
潘栩锋 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using System.Net;
using FLY.Thick.Base.Common;
using FLY.Thick.Base.Client;
潘栩锋's avatar
潘栩锋 committed
18
using FLY.Thick.Base.UI.Converter;
19 20
using Unity;
using FLY.Thick.Base.IService;
潘栩锋's avatar
潘栩锋 committed
21 22 23
using System.ComponentModel;
using System.Collections.ObjectModel;
using GalaSoft.MvvmLight.Command;
24 25 26 27 28 29
using OfficeOpenXml;
using OfficeOpenXml.Table;
using System.Data;
using System.IO;
using Microsoft.Win32;
using System.Threading.Tasks;
潘栩锋's avatar
潘栩锋 committed
30

潘栩锋's avatar
潘栩锋 committed
31
namespace FLY.Thick.Base.UI
潘栩锋's avatar
潘栩锋 committed
32 33 34 35
{
    /// <summary>
    /// Page_Curve.xaml 的交互逻辑
    /// </summary>
潘栩锋's avatar
潘栩锋 committed
36
    public partial class PgCurve : Page
潘栩锋's avatar
潘栩锋 committed
37
    {
潘栩锋's avatar
潘栩锋 committed
38 39
        PgCurveVm viewModel;
        public PgCurve()
潘栩锋's avatar
潘栩锋 committed
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
        {
            InitializeComponent();
            InitializeComponent2();
        }

        void InitializeComponent2()
        {
            System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea1 = new System.Windows.Forms.DataVisualization.Charting.ChartArea();
            System.Windows.Forms.DataVisualization.Charting.Legend legend1 = new System.Windows.Forms.DataVisualization.Charting.Legend();
            System.Windows.Forms.DataVisualization.Charting.Series series1 = new System.Windows.Forms.DataVisualization.Charting.Series();
            
            chart1.Name = "chart1";
            // 
            // chart1
            // 
            chartArea1.Name = "Default";
            chartArea1.AxisX.LabelStyle.Font = new System.Drawing.Font("Trebuchet MS", 8.25F, System.Drawing.FontStyle.Bold);
            chartArea1.AxisX.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
            chartArea1.AxisX.MajorGrid.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
59 60 61
            //chartArea1.AxisX.LabelStyle.Format = "%F2";


潘栩锋's avatar
潘栩锋 committed
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
            chartArea1.AxisY.LabelStyle.Font = new System.Drawing.Font("Trebuchet MS", 8.25F, System.Drawing.FontStyle.Bold);
            chartArea1.AxisY.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
            chartArea1.AxisY.MajorGrid.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
            chartArea1.BackColor = System.Drawing.Color.OldLace;
            //chartArea1.BackGradientStyle = System.Windows.Forms.DataVisualization.Charting.GradientStyle.TopBottom;
            //chartArea1.BackSecondaryColor = System.Drawing.Color.White;
            chartArea1.BorderColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
            chartArea1.BorderDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Solid;
            chartArea1.ShadowColor = System.Drawing.Color.Transparent;
            //对数坐标
            //chartArea1.AxisY.IsLogarithmic = true;
            //chartArea1.AxisY.LogarithmBase = Math.E;
            this.chart1.ChartAreas.Add(chartArea1);


            legend1.Name = "Default";
            legend1.BackColor = System.Drawing.Color.Transparent;
            legend1.Enabled = false;
            legend1.Font = new System.Drawing.Font("Trebuchet MS", 8.25F, System.Drawing.FontStyle.Bold);
            legend1.IsTextAutoFit = false;
            
            this.chart1.Legends.Add(legend1);
            //this.chart1.Location = new System.Drawing.Point(16, 32);

            series1.Name = "Series 1";
            series1.BorderColor = System.Drawing.Color.FromArgb(((int)(((byte)(180)))), ((int)(((byte)(26)))), ((int)(((byte)(59)))), ((int)(((byte)(105)))));
            series1.BorderWidth = 3;
            series1.ChartArea = "Default";
            series1.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
            series1.Color = System.Drawing.Color.FromArgb(((int)(((byte)(220)))), ((int)(((byte)(65)))), ((int)(((byte)(140)))), ((int)(((byte)(240)))));
            series1.Legend = "Default";
            series1.MarkerSize = 8;
            series1.MarkerStyle = System.Windows.Forms.DataVisualization.Charting.MarkerStyle.Circle;
            
            series1.ShadowColor = System.Drawing.Color.Black;
            series1.ShadowOffset = 2;
            series1.XValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Double;
            series1.YValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Int32;
100
            
潘栩锋's avatar
潘栩锋 committed
101 102 103 104 105 106 107


            this.chart1.Series.Add(series1);

            this.chart1.BackColor = System.Drawing.Color.Transparent;
        }

潘栩锋's avatar
潘栩锋 committed
108

109 110
        [InjectionMethod]
        public void Init(ICurveService curveService) 
潘栩锋's avatar
潘栩锋 committed
111
        {
潘栩锋's avatar
潘栩锋 committed
112 113
            viewModel = new PgCurveVm();
            viewModel.Init(curveService, chart1, this);
潘栩锋's avatar
潘栩锋 committed
114

潘栩锋's avatar
潘栩锋 committed
115 116 117 118 119
            this.DataContext = viewModel;
        }
    }
    public class CurveTypeConverter : EnumToIsCheckedConverter<CurveType>
    {
潘栩锋's avatar
潘栩锋 committed
120

潘栩锋's avatar
潘栩锋 committed
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
    }
    public class CurveCorrectWayConverter : EnumToIsCheckedConverter<CurveCorrectWay>
    {

    }

    public class PgCurveVm : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        
        #region 参数
        /// <summary>
        /// AD曲线校正方式
        /// </summary>
        public CurveCorrectWay CorrectWay { get; set; }
        /// <summary>
        /// 标记,用于告诉 客户端,当前的曲线类型, 自然对数 / 直线拟合 / 二次拟合
        /// </summary>
        public CurveType Flag { get; set; }
潘栩锋's avatar
潘栩锋 committed
141

142
        public ObservableCollection<CurveCell2> Curves { get; } = new ObservableCollection<CurveCell2>();
潘栩锋's avatar
潘栩锋 committed
143 144 145 146 147

        #endregion

        #region Command
        public RelayCommand ApplyCmd { get;}
148 149 150 151 152
        public RelayCommand AdScaleCmd { get; }
        public RelayCommand ValueScaleCmd { get; }
        public RelayCommand SetRevisedCmd { get; }
        public RelayCommand SaveXlsxCmd { get; }
        public RelayCommand LoadXlsxCmd { get; }
潘栩锋's avatar
潘栩锋 committed
153 154 155 156 157 158 159 160 161
        #endregion


        ICurveService mCurveService;
        System.Windows.Forms.DataVisualization.Charting.Chart chart1;
        Page page;
        public PgCurveVm() 
        {
            ApplyCmd = new RelayCommand(Apply);
162 163 164 165 166
            AdScaleCmd = new RelayCommand(AdScale);
            ValueScaleCmd = new RelayCommand(ValueScale);
            SetRevisedCmd = new RelayCommand(SetRevised);
            SaveXlsxCmd = new RelayCommand(SaveXlsx);
            LoadXlsxCmd = new RelayCommand(LoadXlsx);
潘栩锋's avatar
潘栩锋 committed
167
        }
潘栩锋's avatar
潘栩锋 committed
168

169 170


潘栩锋's avatar
潘栩锋 committed
171 172 173 174
        public void Init(
            ICurveService curveService,
            System.Windows.Forms.DataVisualization.Charting.Chart chart,
            Page page)
潘栩锋's avatar
潘栩锋 committed
175
        {
潘栩锋's avatar
潘栩锋 committed
176 177 178 179
            mCurveService = curveService;
            this.chart1 = chart;
            this.page = page;

180 181 182 183 184
            Misc.BindingOperations.SetBinding(mCurveService, nameof(mCurveService.CorrectWay), this, nameof(CorrectWay));
            Misc.BindingOperations.SetBinding(mCurveService, nameof(mCurveService.Flag), this, nameof(Flag));
            Misc.BindingOperations.SetBinding(mCurveService, nameof(mCurveService.Curves), ()=> {
                update_curves(); 
            });
潘栩锋's avatar
潘栩锋 committed
185

潘栩锋's avatar
潘栩锋 committed
186
            this.PropertyChanged += PgCurveVm_PropertyChanged;
潘栩锋's avatar
潘栩锋 committed
187 188
        }

潘栩锋's avatar
潘栩锋 committed
189
        private void PgCurveVm_PropertyChanged(object sender, PropertyChangedEventArgs e)
潘栩锋's avatar
潘栩锋 committed
190
        {
191
            if (e.PropertyName == nameof(Flag)) 
潘栩锋's avatar
潘栩锋 committed
192 193 194
            {
                DataBindAll();
            }
195
            
潘栩锋's avatar
潘栩锋 committed
196 197
        }

潘栩锋's avatar
潘栩锋 committed
198
        void update_curves() 
潘栩锋's avatar
潘栩锋 committed
199
        {
潘栩锋's avatar
潘栩锋 committed
200 201 202 203 204 205
            Curves.CollectionChanged -= Curves_CollectionChanged;
            Curves.Clear();
            if (mCurveService.Curves != null)
            {
                foreach (var cell in mCurveService.Curves)
                {
206 207 208
                    var cell2 = new CurveCell2() { AD = cell.AD, Value = cell.Value };
                    cell2.PropertyChanged += Cell_PropertyChanged;
                    Curves.Add(cell2);
潘栩锋's avatar
潘栩锋 committed
209 210 211
                }
            }
            Curves.CollectionChanged += Curves_CollectionChanged;
潘栩锋's avatar
潘栩锋 committed
212 213
            DataBindAll();
        }
214 215

        private void Cell_PropertyChanged(object sender, PropertyChangedEventArgs e)
潘栩锋's avatar
潘栩锋 committed
216
        {
217
            DataBindAll();
潘栩锋's avatar
潘栩锋 committed
218
        }
潘栩锋's avatar
潘栩锋 committed
219 220 221 222 223

        void Curves_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
            {
224
                foreach (CurveCell2 c in e.NewItems)
潘栩锋's avatar
潘栩锋 committed
225
                {
潘栩锋's avatar
潘栩锋 committed
226
                    c.PropertyChanged += (_s, _e) => { DataBindAll(); };
潘栩锋's avatar
潘栩锋 committed
227 228 229 230 231
                }
                DataBindAll();
            }
            else if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Remove)
            {
潘栩锋's avatar
潘栩锋 committed
232
                DataBindAll();
潘栩锋's avatar
潘栩锋 committed
233 234 235 236 237 238
            }
        }

        void DataBindAll()
        {

潘栩锋's avatar
潘栩锋 committed
239 240
            CurveCore CurveCore = new CurveCore();
            CurveCore.Flag = Flag;
241 242

            CurveCore.Curves = Curves.Select(c2 => new CurveCell() { AD = c2.AD, Value = c2.Value }).ToList();
潘栩锋's avatar
潘栩锋 committed
243 244 245

            var curves = CurveCore.Curves;

潘栩锋's avatar
潘栩锋 committed
246
            chart1.Series["Series 1"].Points.Clear();
潘栩锋's avatar
潘栩锋 committed
247
            for (int i = 0; i < curves.Count(); i++)
潘栩锋's avatar
潘栩锋 committed
248
            {
潘栩锋's avatar
潘栩锋 committed
249
                int ad = curves[i].AD;
250
                double thick = curves[i].Value;
潘栩锋's avatar
潘栩锋 committed
251

252
                chart1.Series["Series 1"].Points.AddXY(thick, ad);
潘栩锋's avatar
潘栩锋 committed
253

潘栩锋's avatar
潘栩锋 committed
254
                if ((i + 1) <= (curves.Count() - 1))
潘栩锋's avatar
潘栩锋 committed
255 256 257 258
                {
                    //补齐点, 以thick=100 步进
                    int step = 100;
                    bool isDescending = true;//降序排列
潘栩锋's avatar
潘栩锋 committed
259
                    if (curves[i].AD < curves[i + 1].AD)
潘栩锋's avatar
潘栩锋 committed
260 261
                        isDescending = false;

潘栩锋's avatar
潘栩锋 committed
262
                    while (true)
潘栩锋's avatar
潘栩锋 committed
263 264 265 266
                    {
                        if (isDescending)
                        {
                            ad -= step;
潘栩锋's avatar
潘栩锋 committed
267
                            if (ad <= curves[i + 1].AD)
潘栩锋's avatar
潘栩锋 committed
268 269 270 271 272
                                break;
                        }
                        else
                        {
                            ad += step;
潘栩锋's avatar
潘栩锋 committed
273
                            if (ad >= curves[i + 1].AD)
潘栩锋's avatar
潘栩锋 committed
274 275 276
                                break;
                        }

潘栩锋's avatar
潘栩锋 committed
277
                        thick = CurveCore.AD2Value(ad, AD2ValueFlag.NoRevised);
278
                        thick = Math.Round(thick, 2);
279
                        chart1.Series["Series 1"].Points.AddXY(thick, ad);
潘栩锋's avatar
潘栩锋 committed
280
                        chart1.Series["Series 1"].Points[chart1.Series["Series 1"].Points.Count() - 1].MarkerStyle =
潘栩锋's avatar
潘栩锋 committed
281 282 283 284 285
                            System.Windows.Forms.DataVisualization.Charting.MarkerStyle.None;
                    }
                }
            }
        }
潘栩锋's avatar
潘栩锋 committed
286 287 288 289
        private void Apply()
        {
            if (!WdPassword.Authorize("Curve"))
                return;
潘栩锋's avatar
潘栩锋 committed
290

291
            mCurveService.Apply(this.CorrectWay, this.Flag, this.Curves.ToArray());
潘栩锋's avatar
潘栩锋 committed
292 293 294 295 296
            FLY.ControlLibrary.Window_Tip.Show("应用成功",
                null,
                TimeSpan.FromSeconds(2));
            
        }
297
        private void AdScale()
潘栩锋's avatar
潘栩锋 committed
298
        {
潘栩锋's avatar
潘栩锋 committed
299 300
            var curves = Curves;
            if (curves.Count > 1)
潘栩锋's avatar
潘栩锋 committed
301
            {
潘栩锋's avatar
潘栩锋 committed
302 303 304 305 306
                WdAdScale w = new WdAdScale();
                w.Old = curves[0].AD;
                w.New = curves[0].AD;
                w.Owner = FLY.ControlLibrary.COMMON.GetWindow(page);
                if (w.ShowDialog() == true)
潘栩锋's avatar
潘栩锋 committed
307
                {
潘栩锋's avatar
潘栩锋 committed
308
                    if ((w.Old != w.New) && (w.New != 0) && (w.Old != 0))
潘栩锋's avatar
潘栩锋 committed
309 310
                    {
                        double scale = (double)w.New / w.Old;
潘栩锋's avatar
潘栩锋 committed
311
                        for (int i = 0; i < curves.Count(); i++)
潘栩锋's avatar
潘栩锋 committed
312
                        {
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
                            curves[i].AD = (int)Math.Round(scale * curves[i].AD);
                        }
                    }
                    FLY.ControlLibrary.Window_Tip.Show("成功",
                        null,
                        TimeSpan.FromSeconds(2));
                }
            }
        }

        private async void LoadXlsx()
        {
            //加载数据
            string strDesktopPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);

            OpenFileDialog sfd = new OpenFileDialog();
            sfd.Filter = "xlsx files (*.xlsx)|*.xlsx";
            sfd.InitialDirectory = strDesktopPath;

            if (sfd.ShowDialog() == true)
            {
                string filename = sfd.FileName;
                Exception error = null;
                var ret = await Task.Factory.StartNew(() =>
                {
                    try
                    {
                        LoadXlsx(filename);
                    }
                    catch (Exception e)
                    {
                        error = e;
                        return false;
                    }
                    return true;
                });
                if (ret)
                {
                    DataBindAll();
                    FLY.ControlLibrary.Window_Tip.Show("成功", $"加载{filename}", TimeSpan.FromSeconds(2));
                }
                else
                {
                    MessageBox.Show($"{error}", $"加载{filename}失败", MessageBoxButton.OK, MessageBoxImage.Error);
                }

            }
        }
        private void LoadXlsx(string filepath)
        {
            //检测标题

            DataTable dataTable_curve = new DataTable("table_curve");
            dataTable_curve.Columns.Add(new DataColumn() { ColumnName = "AD", DataType = typeof(double), Caption = "0" });
            dataTable_curve.Columns.Add(new DataColumn() { ColumnName = "VALUE", DataType = typeof(double), Caption = "0.00" });

            DataTable dataTable_info = new DataTable("table_info");
            dataTable_info.Columns.Add(new DataColumn() { ColumnName = "曲线类型", DataType = typeof(string) });
            dataTable_info.Columns.Add(new DataColumn() { ColumnName = "校正方式", DataType = typeof(string) });

            using (ExcelPackage p = new ExcelPackage(new FileInfo(filepath)))
            {
                ExcelWorksheet sheet = p.Workbook.Worksheets["AD曲线"];
                FromSheet(sheet, dataTable_curve);

                sheet = p.Workbook.Worksheets["辅助信息"];
                FromSheet(sheet, dataTable_info);
            }


383 384
            
            page.Dispatcher.Invoke(() =>
385
            {
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
                Curves.CollectionChanged -= Curves_CollectionChanged;
                this.PropertyChanged -= PgCurveVm_PropertyChanged;

                Curves.Clear();
                for (int i = 0; i < dataTable_curve.Rows.Count; i++)
                {
                    var dataRow = dataTable_curve.Rows[i];
                    int ad = System.Convert.ToInt32(dataRow["AD"]);
                    double value = System.Convert.ToDouble(dataRow["VALUE"]);
                    Curves.Add(new CurveCell2() { AD = ad, Value = value });
                }

                if (dataTable_info.Rows.Count > 0)
                {
                    var dataRow = dataTable_info.Rows[0];
                    Flag = (CurveType)Enum.Parse(typeof(CurveType), (string)dataRow["曲线类型"]);
                    CorrectWay = (CurveCorrectWay)Enum.Parse(typeof(CurveCorrectWay), (string)dataRow["校正方式"]);
                }


                this.PropertyChanged += PgCurveVm_PropertyChanged;
                Curves.CollectionChanged += Curves_CollectionChanged;
            });

410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611
        }

        void FromSheet(ExcelWorksheet sheet, DataTable dataTable)
        {

            int from_row = 1;
            int row = from_row;


            for (int i = 0; i < dataTable.Columns.Count; i++)
            {
                int col = i + 1;
                if ((string)(sheet.Cells[row, col].Value) != dataTable.Columns[i].ColumnName)
                {
                    throw new Exception($"格式出错, 第{i}列 不是{dataTable.Columns[i].ColumnName}");
                }
            }

            row++;
            while (true)
            {
                var newRow = dataTable.NewRow();
                //当一行都是空的,认为没有数据
                int nullCnt = 0;
                for (int j = 0; j < dataTable.Columns.Count; j++)
                {
                    int col = j + 1;
                    object value = sheet.Cells[row, col].Value;

                    if (value is string || value == null)
                    {
                        if (string.IsNullOrEmpty((string)(value)))
                        {
                            //空的
                            nullCnt++;
                            //continue;
                            return;
                        }

                    }
                    try
                    {
                        newRow[j] = sheet.Cells[row, col].Value;
                    }
                    catch (Exception e)
                    {
                        throw e;
                    }
                }
                if (nullCnt == dataTable.Columns.Count)
                {
                    //没有数据了
                    break;
                }
                dataTable.Rows.Add(newRow);
                row++;
            }

        }


        private async void SaveXlsx()
        {
            //下载数据
            string strDesktopPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);

            string filename = $"AD曲线_{DateTime.Now:yyyyMMddHHmmss}.xlsx";
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "xlsx files (*.xlsx)|*.xlsx";
            sfd.InitialDirectory = strDesktopPath;
            sfd.FileName = filename;
            if (sfd.ShowDialog() == true)
            {
                filename = sfd.FileName;

                await Task.Factory.StartNew(() =>
                {
                    SaveToXlsx(filename);
                });
                
                FLY.ControlLibrary.Window_Tip.Show("成功", $"导出到{filename}", TimeSpan.FromSeconds(2));
            }
        }

        private void SaveToXlsx(string filepath)
        {
            if (File.Exists(filepath))
            {
                File.Delete(filepath);
            }
            using (ExcelPackage p = new ExcelPackage(new FileInfo(filepath)))
            {
                ExcelWorksheet sheet = p.Workbook.Worksheets.Add("AD曲线");
                DataTable dataTable = new DataTable("table_curve");
                dataTable.Columns.Add(new DataColumn() { ColumnName = "AD", DataType = typeof(double), Caption = "0" });
                dataTable.Columns.Add(new DataColumn() { ColumnName = "VALUE", DataType = typeof(double), Caption = "0.00" });

                for (int i = 0; i < Curves.Count(); i++)
                {
                    try
                    {
                        var dataRow = dataTable.NewRow();
                        dataRow["AD"] = Curves[i].AD;
                        dataRow["VALUE"] = Curves[i].Value;
                        dataTable.Rows.Add(dataRow);
                    }
                    catch
                    {
                        break;
                    }
                }

                ToSheet(sheet, dataTable);


                sheet = p.Workbook.Worksheets.Add("辅助信息");
                dataTable = new DataTable("table_info");
                dataTable.Columns.Add(new DataColumn() { ColumnName = "曲线类型", DataType = typeof(string)});
                dataTable.Columns.Add(new DataColumn() { ColumnName = "校正方式", DataType = typeof(string) });
                {
                    var dataRow = dataTable.NewRow();
                    dataRow["曲线类型"] = Flag.ToString();
                    dataRow["校正方式"] = CorrectWay.ToString();
                    dataTable.Rows.Add(dataRow);
                }
                ToSheet(sheet, dataTable);

                p.Save();

            }

        }
        void ToSheet(ExcelWorksheet sheet, DataTable dataTable)
        {
            int from_row = 1;
            int row = from_row;
            //添加标题
            for (int i = 0; i < dataTable.Columns.Count; i++)
            {
                int col = i + 1;
                sheet.Cells[row, col].Value = dataTable.Columns[i].ColumnName;
                //格式
                sheet.Column(col).Style.Numberformat.Format = dataTable.Columns[i].Caption;
            }

            row++;
            for (int i = 0; i < dataTable.Rows.Count; i++)
            {
                for (int j = 0; j < dataTable.Columns.Count; j++)
                {
                    int col = j + 1;
                    sheet.Cells[row, col].Value = dataTable.Rows[i][j];
                }
                row++;
            }

            int colcnt = dataTable.Columns.Count;
            int rowcnt = dataTable.Rows.Count;
            var range = sheet.Cells[from_row, 1, from_row + rowcnt, colcnt];

            var tbl = sheet.Tables.Add(range, dataTable.TableName);
            tbl.TableStyle = TableStyles.Medium9;
            sheet.Cells[sheet.Dimension.Address].AutoFitColumns();
        }


        private void SetRevised()
        {
            if (mCurveService.Curves != null) {
                Curves.CollectionChanged -= Curves_CollectionChanged;
                Curves.Clear();

                foreach (var cell in mCurveService.Curves)
                {
                    Curves.Add(new CurveCell2() { AD = cell.RevisedAD, Value = cell.Value });
                    cell.PropertyChanged += (s, e) => { DataBindAll(); };
                }
                Curves.CollectionChanged += Curves_CollectionChanged;
                DataBindAll();
                FLY.ControlLibrary.Window_Tip.Show("成功",
                    null,
                    TimeSpan.FromSeconds(2));
            }
        }

        private void ValueScale()
        {
            var curves = Curves;
            if (curves.Count > 1)
            {
                WdAdScale w = new WdAdScale();
                w.Old = curves[0].Value;
                w.New = curves[0].Value;
                w.Owner = FLY.ControlLibrary.COMMON.GetWindow(page);
                if (w.ShowDialog() == true)
                {
                    if ((w.Old != w.New) && (w.New != 0) && (w.Old != 0))
                    {
                        double scale = w.New / w.Old;
                        for (int i = 0; i < curves.Count(); i++)
                        {
                            curves[i].Value = Math.Round(scale * curves[i].Value, 2);
潘栩锋's avatar
潘栩锋 committed
612 613
                        }
                    }
614 615 616
                    FLY.ControlLibrary.Window_Tip.Show("成功",
                        null,
                        TimeSpan.FromSeconds(2));
潘栩锋's avatar
潘栩锋 committed
617 618 619 620 621
                }
            }
        }
    }
}