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

namespace Windows_Exports
{
    public partial class Form1 : Form
    {
        public Form1()
        {

            InitializeComponent();
            this.viewer1.Sidebar.Visible = true;
            GrapeCity.ActiveReports.PageReport rpt1 = new GrapeCity.ActiveReports.PageReport(new FileInfo(@"RdlReport1.rdlx"));
            rpt1.Document.LocateDataSource += new LocateDataSourceEventHandler(LocateData);
            viewer1.LoadDocument(rpt1.Document);

        }
        public  void LocateData(object sender, LocateDataSourceEventArgs args)
        {
            #region
            if (args.DataSourceName == "DataSource1")
            {
                if (args.DataSetName == "DataSet1")
                {
                    List<FormData> data = new List<FormData>();
                    for (int i = 1; i <= 3; i++)
                    {
                        for (int j = 1; j <= 5; j++)
                        {
                            for (int k = 1; k <= 2; k++)
                            {
                                data.Add(new FormData()
                                {
                                    Main = i + "CM主控制器",
                                    Sub = "T-80" + i + j + "子控制器",
                                    Variable = "T-80" + i + j + k + ".PV",
                                    Type = "CV投用情况",
                                    Active = 0 + (i + j + k) % 2,
                                    Value = (95 - i / 10.0 + j / 10.0 + k / 100.0) / 100.0
                                });
                            }
                        }
                    }
                    args.Data = data;
                }
            }
            #endregion
            #region
            if (args.DataSourceName == "DataSource2")
            {
                if (args.DataSetName == "DataSet2")
                {
                    List<TimeInfo> info = new List<TimeInfo>();
                    info.Add(new TimeInfo() { Type = "开始时间", Value = Convert.ToDateTime("22/08/2017 09:00:00") });
                    info.Add(new TimeInfo() { Type = "结束时间", Value = Convert.ToDateTime("22/08/2017 10:00:00") });
                    info.Add(new TimeInfo() { Type = "CV投用指标", Value = 0.95 });
                    args.Data = info;
                }
            }
            #endregion
            #region
            if (args.DataSourceName == "DataSource3")
            {
                if (args.DataSetName == "DataSet3")
                {
                    List<Operate> operate = new List<Operate>();
                    operate.Add(new Operate() { Number = 1, Type = "SVCTL61", Count = 838 });
                    operate.Add(new Operate() { Number = 2, Type = "SVCTL64", Count = 226 });
                    operate.Add(new Operate() { Number = 3, Type = "SVCTL23", Count = 222 });
                    operate.Add(new Operate() { Number = 4, Type = "SVCTL55", Count = 37 });
                    operate.Add(new Operate() { Number = 5, Type = "SVCTL63", Count = 35 });
                    operate.Add(new Operate() { Number = 6, Type = "SVCTL01", Count = 30 });
                    operate.Add(new Operate() { Number = 7, Type = "SVCTL14", Count = 29 });
                    operate.Add(new Operate() { Number = 8, Type = "SVCTL25", Count = 27 });
                    operate.Add(new Operate() { Number = 9, Type = "SVCTL02", Count = 26 });
                    operate.Add(new Operate() { Number = 10, Type = "SVCTL54", Count = 20 });
                    operate.Add(new Operate() { Number = 11, Type = "其他", Count = 969 });
                    args.Data = operate;
                }
            }
            #endregion
          
        }
        public  void tsbExcel_Click(object sender, EventArgs e)
        {

            GrapeCity.ActiveReports.PageReport rpt1 = new GrapeCity.ActiveReports.PageReport(new FileInfo(@"RdlReport1.rdlx"));
            rpt1.Document.LocateDataSource += new LocateDataSourceEventHandler(LocateData);
            // Create an output directory.
            System.IO.DirectoryInfo outputDirectory = new System.IO.DirectoryInfo(@"D:\MyExcel");
            outputDirectory.Create();

            // Provide settings for your rendering output.
            GrapeCity.ActiveReports.Export.Excel.Page.ExcelRenderingExtensionSettings excelSetting = new GrapeCity.ActiveReports.Export.Excel.Page.ExcelRenderingExtensionSettings();
            excelSetting.FileFormat = GrapeCity.ActiveReports.Export.Excel.Page.FileFormat.Xls;
            GrapeCity.ActiveReports.Extensibility.Rendering.ISettings setting = excelSetting;

            // Set the rendering extension and render the report.
            GrapeCity.ActiveReports.Export.Excel.Page.ExcelRenderingExtension excelRenderingExtension = new GrapeCity.ActiveReports.Export.Excel.Page.ExcelRenderingExtension();
            GrapeCity.ActiveReports.Rendering.IO.FileStreamProvider outputProvider = new GrapeCity.ActiveReports.Rendering.IO.FileStreamProvider(outputDirectory, System.IO.Path.GetFileNameWithoutExtension(outputDirectory.Name));

            // Overwrite output file if it already exists.
            outputProvider.OverwriteOutputFile = true;

            rpt1.Document.Render(excelRenderingExtension, outputProvider, excelSetting.GetSettings());

        
        }

        private void tsbPrint_Click(object sender, EventArgs e)
        {
            string file_name = @"..\..\RdlReport1.rdlx";
            GrapeCity.ActiveReports.PageReport pageReport = new GrapeCity.ActiveReports.PageReport(new System.IO.FileInfo(file_name));
            pageReport.Document.LocateDataSource += new LocateDataSourceEventHandler(LocateData);
            pageReport.Document.Printer.DefaultPageSettings.Margins.Bottom = 0;
            pageReport.Document.Printer.DefaultPageSettings.Margins.Left = 0;
            pageReport.Document.Printer.DefaultPageSettings.Margins.Right = 0;
            pageReport.Document.Printer.DefaultPageSettings.Margins.Top = 0;
            pageReport.Document.Printer.PaperSize.RawKind = 0;
            pageReport.Document.Printer.PaperSize.Width = 14;
            pageReport.Document.Printer.PaperSize.Height = 20;
            GrapeCity.ActiveReports.PrintExtension.Print(pageReport.Document, true, true);

        }
        public class FormData
        {
            public string Main { get; set; }
            public string Sub { get; set; }
            public string Variable { get; set; }
            public string Type { get; set; }
            public double Active { get; set; }
            public double Value { get; set; }
        }

        public class TimeInfo
        {
            public string Type { get; set; }
            public object Value { get; set; }
        }

        public class Operate
        {
            public int Number { get; set; }
            public string Type { get; set; }
            public int Count { get; set; }
        }
    }
}
