using FarPoint.Excel;
using FarPoint.Win.Spread;
using FarPoint.Win.Spread.Model;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using System.Xml.Serialization;
using static WindowsFormsApp5.TemplateLocation;

namespace WindowsFormsApp5
{
    public partial class Form1 : Form
    {
        List<string> sheets;
        public Form1()
        {
            InitializeComponent();
            fpSpread1.OpenExcel(System.Windows.Forms.Application.StartupPath + @"/111.xlsx");

            //fpSpread1.OpenExcel(@"C:\Users\Zhang Xuemin\Desktop\合并单元格表.xlsx");//临时位置
            //sheets = new List<string>()
            //{ "波长示值误差与重复性校验", "透射比示值误差与重复性校验", "杂散光校验", "基线平直度检验", "基线暗噪声检验", "光谱带宽与仪器最小光谱带宽", "噪声检验", "漂移检验" };
            /////对templateLocations进行添加
            ////添加检测项信息的绑定关系
            //AddTestItemTempLoc();
            ////添加各表的绑定关系
            //AddResultTemLoc();


            //StringWriter stringWriter = new StringWriter(new StringBuilder());
            //XmlSerializer xmlSerializer = new XmlSerializer(typeof(List<TemplateLocation>));
            //xmlSerializer.Serialize(stringWriter, templateLocations);
            /////生成的文件路径
            //FileStream fileStream = new FileStream(@"C:\Users\Zhang Xuemin\Desktop\我的.xml", FileMode.OpenOrCreate);
            //StreamWriter streamWriter = new StreamWriter(fileStream);
            //streamWriter.Write(stringWriter.ToString());
            //streamWriter.Close();
            //fileStream.Close();
            ////foreach (FarPoint.Win.Spread.SheetView sheet in fpSpread1.Sheets)
            ////{
            ////    if (!sheets.Contains(sheet.SheetName))
            ////        sheet.Visible = false;
            ////    else
            ////    {
            ////        FarPoint.Win.Spread.CellType.TextCellType tct = new FarPoint.Win.Spread.CellType.TextCellType();
            ////        /*tct.Multiline = true;*/
            ////        tct.WordWrap = true;
            ////        for (int i = 0; i < sheet.NonEmptyColumnCount; i++)
            ////        {
            ////            for (int j = 1; j < sheet.NonEmptyRowCount; j++)
            ////            {
            ////                fpSpread1.ActiveSheet.Cells[i, j].CellType = tct;
            ////            }

            ////        }
            ////    }
            ////}
            ////fpSpread1.Sheets["波长示值误差与重复性校验"].Protect = true;
            ////fpSpread1.Sheets["波长示值误差与重复性校验"].Columns[2].Locked = false ;
            /////部分单元格可编辑
            /////行高自适应，自动换行
            //fpSpread1.SaveExcel(@"C:\Users\Zhang Xuemin\Desktop\设为换行.xlsx", (ExcelSaveFlags.UseOOXMLFormat));
            //this.Refresh();
            //fpSpread1.OpenExcel(@"C:\Users\Zhang Xuemin\Desktop\设为换行.xlsx");
        }
        /// <summary>
        /// xml中加入各表绑定关系
        /// </summary>
        private void AddResultTemLoc()
        {
            foreach (string temp in sheets)
            {
                if (fpSpread1.Sheets.Find(temp) != null)
                {
                    TemplateLocation templateLocation = new TemplateLocation();
                    templateLocation.SheetName = temp;
                    for (int i = 1; i < fpSpread1.Sheets[temp].NonEmptyRowCount; i++)
                    {
                        BindingLocation binding = new BindingLocation();
                        binding.Name = fpSpread1.Sheets[temp].Cells[i, 0].Text;
                        if (temp == "基线平直度检验" && temp == "基线暗噪声")
                        {
                            binding.Field = string.Empty;
                        }
                        else
                        {
                            binding.Field = fpSpread1.Sheets[temp].Cells[i, 1].Text;
                        }
                        binding.RowIndex = i.ToString();

                        if (temp == "透射比示值误差与重复性校验" || temp == "光谱带宽与仪器最小光谱带宽")
                            binding.ColumnIndex = "3";
                        else if (temp == "基线平直度检验" || temp == "基线暗噪声检验")
                            binding.ColumnIndex = "1";
                        else
                            binding.ColumnIndex = "2";

                        if (temp == "波长示值误差与重复性校验")
                            binding.IsCrosswise = "true";
                        templateLocation.BindingLocation.Add(binding);
                    }
                    templateLocations.Add(templateLocation);
                }
            }
        }

        /// <summary>
        /// 绑定关系
        /// </summary>
        private List<TemplateLocation> templateLocations = new List<TemplateLocation>() { };
        /// <summary>
        /// xml中加入检测项信息绑定关系
        /// </summary>
        private void AddTestItemTempLoc()
        {
            TemplateLocation templateLocation = new TemplateLocation();
            templateLocation.SheetName = "检测项信息";

            BindingLocation binding = new BindingLocation()
            {
                Name = "项目名称",
                Field = "Name",
                RowIndex = "0",
                ColumnIndex = "1"
            };
            templateLocation.BindingLocation.Add(binding);
            binding = new BindingLocation()
            {
                Name = "提示",
                Field = "Tips",
                RowIndex = "0",
                ColumnIndex = "2"
            };
            templateLocation.BindingLocation.Add(binding);
            binding = new BindingLocation()
            {
                Name = "方法信息",
                Field = "MethodStr",
                RowIndex = "0",
                ColumnIndex = "3"
            };
            templateLocation.BindingLocation.Add(binding);
            binding = new BindingLocation()
            {
                Name = "测量信息",
                Field = "MeasurePara",
                RowIndex = "0",
                ColumnIndex = "4"
            };
            templateLocation.BindingLocation.Add(binding);
            binding = new BindingLocation()
            {
                Name = "重复次数",
                Field = "RepeatTimes",
                RowIndex = "0",
                ColumnIndex = "5"
            };
            templateLocation.BindingLocation.Add(binding);
            binding = new BindingLocation()
            {
                Name = "重复时间间隔",
                Field = "RepeatInterval",
                RowIndex = "0",
                ColumnIndex = "6"
            };
            templateLocation.BindingLocation.Add(binding);
            binding = new BindingLocation()
            {
                Name = "特征值",
                Field = "FeaturePoints",
                RowIndex = "0",
                ColumnIndex = "7"
            };
            templateLocation.BindingLocation.Add(binding);
            binding = new BindingLocation()
            {
                Name = "分析方法",
                Field = "AnalysisMethod",
                RowIndex = "0",
                ColumnIndex = "8"
            };
            templateLocation.BindingLocation.Add(binding);
            binding = new BindingLocation()
            {
                Name = "子项目编号",
                Field = "ItemID",
                RowIndex = "0",
                ColumnIndex = "26"
            };
            templateLocation.BindingLocation.Add(binding);
            templateLocations.Add(templateLocation);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //var get=fpSpread1.Sheets["漂移检验"].GetConditionalFormatting(1, 4);
            

            /*UnaryComparisonConditionalFormattingRule sRule;

            sRule = new UnaryComparisonConditionalFormattingRule(UnaryComparisonOperator.EqualTo, "S", false);
            sRule.Operator = UnaryComparisonOperator.EqualTo;
            sRule.Value = "500";
            sRule.BackColor = Color.Red;
            sRule.FontStyle = new SpreadFontStyle(RegularBoldItalicFontStyle.Bold);

            ConditionalFormatting cf;
            cf = new ConditionalFormatting(new CellRange(0, 0, fpSpread1.Sheets["漂移检验"].Rows.Count, fpSpread1.Sheets["漂移检验"].Columns.Count), new IConditionalFormattingRule[] { sRule});

            fpSpread1.Sheets["漂移检验"].Models.ConditionalFormatting.Add(cf);
            *//*Cells[1, 2, 10, 2].*//*
            fpSpread1.SaveExcel(@"C:\Users\Zhang Xuemin\Desktop\检验软件\测试条件规则.xlsx",ExcelSaveFlags.UseOOXMLFormat);
            fpSpread1.OpenExcel(@"C:\Users\Zhang Xuemin\Desktop\检验软件\测试条件规则.xlsx");
            fpSpread1.ResumeLayout();*/
/*
            var lowMarksRule = new BetweenValuesConditionalFormattingRule(false, 1, false, 33, false);
            lowMarksRule.BackColor = Color.FromArgb(100, Color.LightPink);
            lowMarksRule.ForeColor = Color.Red;

            var lowMarksRule2 = new TextConditionalFormattingRule(,"不合格");
*/
            //var highMarksRule1 = new BetweenValuesConditionalFormattingRule(false, 76, false, 100, false);
            var AAS = new TextConditionalFormattingRule(TextConditionOperator.Contains,"不合格");

            //var ssss=new HighlightConditionalFormattingRule(TextConditionOperator)

            var ss = new UnaryComparisonConditionalFormattingRule(UnaryComparisonOperator.EqualTo, "不合格", false);


            //var qq1 = new BetweenValuesConditionalFormattingRule(false, "500", true,"",true,true);
            AAS.BackColor = Color.Green;
            //AAS.ForeColor = Color.DarkBlue;
            fpSpread1.Sheets["漂移检验"].SetConditionalFormatting(1,1,10,10 , AAS);
            //fpSpread1_Sheet1.SetConditionalFormatting(1, 2, students.Count, 3, lowMarksRule, avgMarksRule, highMarksRule);
        }

    }
}
