using ClosedXML.Excel;
using DevExpress.Export;
using DevExpress.Web;
using DevExpress.XtraPrinting;
using MySqlConnector;
using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace OlympiaIntranet
{
    public partial class MSReset : System.Web.UI.Page
    {
        DataTable dtOutputTrades = new DataTable();

        protected void Page_Init(object sender, EventArgs e)
        {
            CultureInfo ci = (CultureInfo)Thread.CurrentThread.CurrentCulture.Clone();
            ci.NumberFormat.NumberDecimalSeparator = ",";
            Thread.CurrentThread.CurrentCulture = ci;
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (HttpContext.Current.User.Identity.IsAuthenticated == false)
            {
                Response.Redirect("~/SignIn.aspx");
            }

            //Bind the grid only once
            if (!IsPostBack)
                gvInputCsv.DataBind();

        }

        public DataTable ResetFileToDataTable(UploadedFile uploadedFile)
        {
            // ref link: https://www.c-sharpcorner.com/blogs/read-csv-file-into-data-table1

            DataTable dtCsv = new DataTable();
            string Fulltext;
            if (uploadedFile.IsValid) //if (FileUpload.HasFile && IsPostBack)
            {
                using (StreamReader sr = new StreamReader(uploadedFile.FileContent))
                {
                    while (!sr.EndOfStream)
                    {
                        Fulltext = sr.ReadToEnd().ToString(); //read full file text  
                        string[] rows = Fulltext.Split('\n'); //split full file text into rows  
                        for (int i = 1; i < rows.Count() - 2; i++) // la prima riga la ignora!
                        {
                            string[] rowValues = rows[i].Split(','); //split each row with , to get individual values  
                            {
                                if (i == 1)
                                {
                                    for (int j = 0; j < rowValues.Count(); j++)
                                    {
                                        if (rowValues[j] == "Security Description") dtCsv.Columns.Add("Stock description"); // se il file è giornaliero differisce solo per il nome colonna Security Description che deve diventare Stock description
                                        else dtCsv.Columns.Add(rowValues[j]); //add headers  
                                    }
                                }
                                else
                                {
                                    DataRow dr = dtCsv.NewRow();
                                    for (int k = 0; k < rowValues.Count(); k++)
                                    {
                                        dr[k] = rowValues[k].ToString().Replace(",", ".").Replace("\"",string.Empty);
                                    }
                                    dtCsv.Rows.Add(dr); //add other rows  
                                }
                            }
                        }
                    }
                }
            }
            return dtCsv;
        }

        protected void uploadControl_FileUploadComplete(object sender, FileUploadCompleteEventArgs e)
        {
            if (radioButtonList.SelectedIndex == 0) // da fare caso radioButtonList.SelectedIndex == 1 !
            {
                Session["dtResetFile"] = ResetFileToDataTable(e.UploadedFile); 

            }
        }

        protected void gvInputCsv_DataBinding(object sender, EventArgs e)
        {
            if (Session["dtResetFile"] != null)
            {
                gvInputCsv.DataSource = (DataTable)Session["dtResetFile"];
            }
        }

        protected void btnElaboraInput_Click(object sender, EventArgs e)
        {
            // Serve solo per effettuare il postback e bindare gvInputCsv al datatable contenuto in Session["dtResetFile"]

            // Importa struttura colonne file excel in output per Guardian
            dtOutputTrades = ElaboraTemplateGuardian(Server.MapPath("~/App_Data/TEMPLATE/sample_imp_tra.xlsx"), "Foglio1"); // Importa colonne file template in output per Guardian

            //if (radioButtonList.SelectedIndex == 0) ElaboraMSResetFileDaily(); // Elabora reset file NON PIU' USATO IL DAILY dal 30/07/2021

            if (radioButtonList.SelectedIndex == 0) ElaboraMSResetFileWeekly(); // Elabora reset file 
        }
        private void ElaboraMSResetFileWeekly()
        {
            DataTable dtResetFile = (DataTable)Session["dtResetFile"];
            if (dtResetFile.Rows.Count > 0)
            {
                double tempDouble = 0;
                double quantity = 0;
                foreach (DataRow rigaReset in dtResetFile.Rows)
                {
                    if (rigaReset["Event"].ToString() != "RESET"
                        && rigaReset["Event"].ToString() != "C"
                        && rigaReset["Event"].ToString() != "DIV"
                        ) continue; // elabora la riga solo se Event=RESET o C o DIV altrimenti skippa, dr1,dr2 vengono create solo con EVENT=RESET, dr3 viene creata sia con RESET che C, dr4 viene creata solo se EVENT = DIV

                    // Crea due righe in output per ogni riga in input
                    DataRow dr1 = dtOutputTrades.NewRow();
                    DataRow dr2 = dtOutputTrades.NewRow();
                    DataRow dr3 = dtOutputTrades.NewRow();
                    DataRow dr4 = dtOutputTrades.NewRow();

                    // data_ope
                    dr1["data_ope"] = DateTime.ParseExact(rigaReset["End Date"].ToString(), "yyyy-MM-dd", CultureInfo.InvariantCulture).ToString("dd.MM.yyyy");
                    dr2["data_ope"] = dr1["data_ope"];
                    dr3["data_ope"] = dr1["data_ope"];
                    dr4["data_ope"] = dr1["data_ope"];

                    // data_ins
                    dr1["data_ins"] = DateTime.ParseExact(rigaReset["End Date"].ToString(), "yyyy-MM-dd", CultureInfo.InvariantCulture).ToString("dd.MM.yyyy");
                    dr2["data_ins"] = dr1["data_ins"];
                    dr3["data_ins"] = dr1["data_ins"];
                    dr4["data_ins"] = dr1["data_ins"];

                    // data_val
                    dr1["data_val"] = DateTime.ParseExact(rigaReset["Value Date"].ToString(), "yyyy-MM-dd", CultureInfo.InvariantCulture).ToString("dd.MM.yyyy");
                    dr2["data_val"] = dr1["data_val"];
                    dr3["data_val"] = dr1["data_val"];
                    dr4["data_val"] = dr1["data_val"];

                    // prezzo
                    if (Double.TryParse(rigaReset["Mark Price2"].ToString().Replace(".", ","), out tempDouble)) dr1["prezzo"] = tempDouble;
                    else dr1["prezzo"] = DBNull.Value;
                    dr2["prezzo"] = dr1["prezzo"];

                    // cod_div_tit
                    dr1["cod_div_tit"] = rigaReset["Listing Ccy"];
                    dr2["cod_div_tit"] = dr1["cod_div_tit"];
                    dr3["cod_div_tit"] = dr1["cod_div_tit"];
                    dr4["cod_div_tit"] = dr1["cod_div_tit"];

                    // qta e cod_ope (dr1 e dr2)
                    if (Double.TryParse(rigaReset["Quantity"].ToString().Replace(".", ","), out quantity)) { dr1["qta"] = quantity; dr2["qta"] = -quantity; }
                    else { dr1["qta"] = DBNull.Value; dr2["qta"] = DBNull.Value; }
                    if (quantity > 0) { dr1["cod_ope"] = "B"; dr2["cod_ope"] = "S"; }
                    else { dr1["cod_ope"] = "S"; dr2["cod_ope"] = "B"; }

                    // qta,cod_ope,ctv_tit_dn,ctv_tot_dn,ctv_tit_dr,ctv_tot_dr (dr3)
                    if (Double.TryParse(rigaReset["Total"].ToString().Replace(".", ","), out tempDouble))
                    {
                        dr3["qta"] = tempDouble;
                        if (tempDouble > 0) { dr3["cod_ope"] = "CREDMAR"; } else { dr3["cod_ope"] = "DEBTMAR"; }
                        dr3["ctv_tit_dn"] = tempDouble;
                        dr3["ctv_tot_dn"] = tempDouble;
                        dr3["ctv_tit_dr"] = tempDouble;
                        dr3["ctv_tot_dr"] = tempDouble;

                        dr4["qta"] = tempDouble;
                        if (tempDouble > 0) { dr4["cod_ope"] = "CREDMAR"; } else { dr4["cod_ope"] = "DEBTMAR"; }
                        dr4["ctv_tit_dn"] = tempDouble;
                        dr4["ctv_tot_dn"] = tempDouble;
                        dr4["ctv_tit_dr"] = tempDouble;
                        dr4["ctv_tot_dr"] = tempDouble;
                    }
                    else
                    {
                        dr3["qta"] = DBNull.Value;
                        dr3["ctv_tit_dn"] = DBNull.Value;
                        dr3["ctv_tot_dn"] = DBNull.Value;
                        dr3["ctv_tit_dr"] = DBNull.Value;
                        dr3["ctv_tot_dr"] = DBNull.Value;

                        dr4["qta"] = DBNull.Value;
                        dr4["ctv_tit_dn"] = DBNull.Value;
                        dr4["ctv_tot_dn"] = DBNull.Value;
                        dr4["ctv_tit_dr"] = DBNull.Value;
                        dr4["ctv_tot_dr"] = DBNull.Value;
                    }

                    // cod_tit
                    string cod_tit = execSP_Scalar("SELECT t.cod_tit FROM tit t WHERE t.cod_isin_sot='" + rigaReset["ISIN"].ToString() + "' and t.cfd='S';");
                    dr1["cod_tit"] = cod_tit;
                    dr2["cod_tit"] = cod_tit;
                    dr3["cod_tit"] = cod_tit;
                    dr4["cod_tit"] = cod_tit;

                    // cod_rap
                    if (rigaReset["Account Number"].ToString() == "061787OC") { dr1["cod_rap"] = "DB6289"; dr2["cod_rap"] = "DB6289"; dr3["cod_rap"] = "DB6289"; dr4["cod_rap"] = "DB6289"; }   // Fondo Plurima
                    if (rigaReset["Account Number"].ToString() == "061787VN") { dr1["cod_rap"] = "GP11950"; dr2["cod_rap"] = "GP11950"; dr3["cod_rap"] = "GP11950"; dr4["cod_rap"] = "GP11950"; } // Fondo Hector

                    // cod_dep_liq
                    dr1["cod_dep_liq"] = "LIQCFD" + rigaReset["Swap Settlement Currency"].ToString();
                    dr2["cod_dep_liq"] = dr1["cod_dep_liq"];

                    // cod_div_reg
                    dr1["cod_div_reg"] = rigaReset["Swap Settlement Currency"];
                    dr2["cod_div_reg"] = dr1["cod_div_reg"];
                    dr3["cod_div_reg"] = dr1["cod_div_reg"];
                    dr4["cod_div_reg"] = dr1["cod_div_reg"];

                    // cod_dep_tit
                    dr1["cod_dep_tit"] = "DEPTIT";
                    dr2["cod_dep_tit"] = "DEPTIT";

                    // cod_ctp
                    dr1["cod_ctp"] = "MSP";
                    dr2["cod_ctp"] = "MSP";
                    dr3["cod_ctp"] = "MSP";
                    dr4["cod_ctp"] = "MSP";

                    // des_mov
                    dr1["des_mov"] = string.Format("Reset open {0}", rigaReset["Stock description"]);
                    dr2["des_mov"] = string.Format("Reset close {0} ", rigaReset["Stock description"]);
                    dr3["des_mov"] = string.Format("Reset payment {0}", rigaReset["Stock description"]);
                    dr4["des_mov"] = string.Format("Dividend payment {0}", rigaReset["Stock description"]);

                    if (rigaReset["Event"].ToString() == "RESET") dtOutputTrades.Rows.Add(dr1); // creata solo se Event=RESET
                    if (rigaReset["Event"].ToString() == "RESET") dtOutputTrades.Rows.Add(dr2); // creata solo se Event=RESET
                    dtOutputTrades.Rows.Add(dr3); // creata sempre quindi Event=RESET o Event=C
                    if (rigaReset["Event"].ToString() == "DIV") dtOutputTrades.Rows.Add(dr4); // creata solo se Event=DIV
                }
                if (dtOutputTrades.Rows.Count > 0)
                {
                    DataView dv = dtOutputTrades.DefaultView;
                    dv.Sort = "des_mov";
                    DataTable sortedDT = dv.ToTable();

                    //Session["dtOutputTrades"] = (DataTable)dtOutputTrades;
                    Session["dtOutputTrades"] = (DataTable)sortedDT;
                    gvOutput.DataSource = Session["dtOutputTrades"];
                    gvOutput.DataBind();
                }
            }
        }

        private void ElaboraMSResetFileDaily()
        {
            DataTable dtResetFile = (DataTable)Session["dtResetFile"];
            if (dtResetFile.Rows.Count > 0)
            {
                double tempDouble = 0;
                double quantity = 0;
                foreach (DataRow rigaReset in dtResetFile.Rows)
                {
                    if (rigaReset["Event"].ToString() != "RESET" 
                        && rigaReset["Event"].ToString() != "C"
                        && rigaReset["Event"].ToString() != "DIV"
                        ) continue; // elabora la riga solo se Event=RESET o C o DIV altrimenti skippa, dr1,dr2 vengono create solo con EVENT=RESET, dr3 viene creata sia con RESET che C, dr4 viene creata solo se EVENT = DIV

                    // Crea due righe in output per ogni riga in input
                    DataRow dr1 = dtOutputTrades.NewRow();
                    DataRow dr2 = dtOutputTrades.NewRow();
                    DataRow dr3 = dtOutputTrades.NewRow();
                    DataRow dr4 = dtOutputTrades.NewRow();

                    // data_ope
                    dr1["data_ope"] = DateTime.ParseExact(rigaReset["End Date"].ToString(), "yyyy-MM-dd", CultureInfo.InvariantCulture).ToString("dd.MM.yyyy");
                    dr2["data_ope"] = dr1["data_ope"];
                    dr3["data_ope"] = dr1["data_ope"];
                    dr4["data_ope"] = dr1["data_ope"];

                    // data_ins
                    dr1["data_ins"] = DateTime.ParseExact(rigaReset["End Date"].ToString(), "yyyy-MM-dd", CultureInfo.InvariantCulture).ToString("dd.MM.yyyy");
                    dr2["data_ins"] = dr1["data_ins"];
                    dr3["data_ins"] = dr1["data_ins"];
                    dr4["data_ins"] = dr1["data_ins"];

                    // data_val
                    dr1["data_val"] = DateTime.ParseExact(rigaReset["Value Date"].ToString(), "yyyy-MM-dd", CultureInfo.InvariantCulture).ToString("dd.MM.yyyy");
                    dr2["data_val"] = dr1["data_val"];
                    dr3["data_val"] = dr1["data_val"];
                    dr4["data_val"] = dr1["data_val"];

                    // prezzo
                    if (Double.TryParse(rigaReset["Mark Price2"].ToString().Replace(".", ","), out tempDouble)) dr1["prezzo"] = tempDouble;
                    else dr1["prezzo"] = DBNull.Value;
                    dr2["prezzo"] = dr1["prezzo"];

                    // cod_div_tit
                    dr1["cod_div_tit"] = rigaReset["Listing Ccy"];
                    dr2["cod_div_tit"] = dr1["cod_div_tit"];
                    dr3["cod_div_tit"] = dr1["cod_div_tit"];
                    dr4["cod_div_tit"] = dr1["cod_div_tit"];

                    // qta e cod_ope (dr1 e dr2)
                    if (Double.TryParse(rigaReset["Quantity"].ToString().Replace(".", ","), out quantity)) { dr1["qta"] = quantity; dr2["qta"] = -quantity; }
                    else { dr1["qta"] = DBNull.Value; dr2["qta"] = DBNull.Value; }
                    if (quantity > 0) { dr1["cod_ope"] = "B"; dr2["cod_ope"] = "S"; }
                    else { dr1["cod_ope"] = "S"; dr2["cod_ope"] = "B"; }

                    // qta,cod_ope,ctv_tit_dn,ctv_tot_dn,ctv_tit_dr,ctv_tot_dr (dr3)
                    if (Double.TryParse(rigaReset["Total"].ToString().Replace(".", ","), out tempDouble))
                    {
                        dr3["qta"] = tempDouble;
                        if (tempDouble > 0) { dr3["cod_ope"] = "CREDMAR"; } else { dr3["cod_ope"] = "DEBTMAR"; }
                        dr3["ctv_tit_dn"] = tempDouble;
                        dr3["ctv_tot_dn"] = tempDouble;
                        dr3["ctv_tit_dr"] = tempDouble;
                        dr3["ctv_tot_dr"] = tempDouble;

                        dr4["qta"] = tempDouble;
                        if (tempDouble > 0) { dr4["cod_ope"] = "CREDMAR"; } else { dr4["cod_ope"] = "DEBTMAR"; }
                        dr4["ctv_tit_dn"] = tempDouble;
                        dr4["ctv_tot_dn"] = tempDouble;
                        dr4["ctv_tit_dr"] = tempDouble;
                        dr4["ctv_tot_dr"] = tempDouble;
                    }
                    else
                    {
                        dr3["qta"]        = DBNull.Value;
                        dr3["ctv_tit_dn"] = DBNull.Value;
                        dr3["ctv_tot_dn"] = DBNull.Value;
                        dr3["ctv_tit_dr"] = DBNull.Value;
                        dr3["ctv_tot_dr"] = DBNull.Value;

                        dr4["qta"] = DBNull.Value;
                        dr4["ctv_tit_dn"] = DBNull.Value;
                        dr4["ctv_tot_dn"] = DBNull.Value;
                        dr4["ctv_tit_dr"] = DBNull.Value;
                        dr4["ctv_tot_dr"] = DBNull.Value;
                    }

                    // cod_tit
                    string cod_tit = execSP_Scalar("SELECT t.cod_tit FROM tit t WHERE t.cod_isin_sot='" + rigaReset["ISIN"].ToString() + "' and t.cfd='S';");
                    dr1["cod_tit"] = cod_tit;
                    dr2["cod_tit"] = cod_tit;
                    dr3["cod_tit"] = cod_tit;
                    dr4["cod_tit"] = cod_tit;

                    // cod_rap
                    if (rigaReset["Account Number"].ToString() == "061787OC") { dr1["cod_rap"] = "DB6289"; dr2["cod_rap"] = "DB6289"; dr3["cod_rap"] = "DB6289"; dr4["cod_rap"] = "DB6289"; }   // Fondo Plurima
                    if (rigaReset["Account Number"].ToString() == "061787VN") { dr1["cod_rap"] = "GP11950"; dr2["cod_rap"] = "GP11950"; dr3["cod_rap"] = "GP11950"; dr4["cod_rap"] = "GP11950"; } // Fondo Hector

                    // cod_dep_liq
                    dr1["cod_dep_liq"] = "LIQCFD" + rigaReset["Swap Settlement Currency"].ToString();
                    dr2["cod_dep_liq"] = dr1["cod_dep_liq"];

                    // cod_div_reg
                    dr1["cod_div_reg"] = rigaReset["Swap Settlement Currency"];
                    dr2["cod_div_reg"] = dr1["cod_div_reg"];
                    dr3["cod_div_reg"] = dr1["cod_div_reg"];
                    dr4["cod_div_reg"] = dr1["cod_div_reg"];

                    // cod_dep_tit
                    dr1["cod_dep_tit"] = "DEPTIT";
                    dr2["cod_dep_tit"] = "DEPTIT";

                    // cod_ctp
                    dr1["cod_ctp"] = "MSP";
                    dr2["cod_ctp"] = "MSP";
                    dr3["cod_ctp"] = "MSP";
                    dr4["cod_ctp"] = "MSP";

                    // des_mov
                    dr1["des_mov"] = string.Format("Reset open {0}", rigaReset["Security Description"]);
                    dr2["des_mov"] = string.Format("Reset close {0} ", rigaReset["Security Description"]);
                    dr3["des_mov"] = string.Format("Reset payment {0}", rigaReset["Security Description"]);
                    dr4["des_mov"] = string.Format("Dividend payment {0}", rigaReset["Security Description"]);

                    if (rigaReset["Event"].ToString() == "RESET") dtOutputTrades.Rows.Add(dr1); // creata solo se Event=RESET
                    if (rigaReset["Event"].ToString() == "RESET") dtOutputTrades.Rows.Add(dr2); // creata solo se Event=RESET
                    dtOutputTrades.Rows.Add(dr3); // creata sempre quindi Event=RESET o Event=C
                    if (rigaReset["Event"].ToString() == "DIV") dtOutputTrades.Rows.Add(dr4); // creata solo se Event=DIV
                }
                if (dtOutputTrades.Rows.Count > 0)
                {
                    DataView dv = dtOutputTrades.DefaultView;
                    dv.Sort = "des_mov";
                    DataTable sortedDT = dv.ToTable();

                    //Session["dtOutputTrades"] = (DataTable)dtOutputTrades;
                    Session["dtOutputTrades"] = (DataTable)sortedDT;
                    gvOutput.DataSource = Session["dtOutputTrades"];
                    gvOutput.DataBind();
                }
            }
        }

        private static string execSP_Scalar(string query)
        {
            object objResult;
            string result = String.Empty;
            using (var con = new MySqlConnection(Properties.Settings.Default.mySqlConnection))
            {
                var cmd = new MySqlCommand
                {
                    CommandText = query,
                    CommandType = CommandType.Text,
                    Connection = con
                };
                cmd.CommandTimeout = 0;
                con.Open(); // Open the SqlConnection.
                objResult = cmd.ExecuteScalar();

                if (objResult != null) result = objResult.ToString();
            }
            return (result);
        }

        public static DataTable ElaboraTemplateGuardian(string file, string sheetName)
        {
            // Open the Excel file using ClosedXML.
            // Keep in mind the Excel file cannot be open when trying to read it
            using (XLWorkbook workBook = new XLWorkbook(file))
            {
                //Read the first Sheet from Excel file.
                IXLWorksheet workSheet = workBook.Worksheet(1);

                //Create a new DataTable.
                DataTable dt = new DataTable();

                //Loop through the Worksheet rows.
                bool firstRow = true;
                foreach (IXLRow row in workSheet.Rows())
                {
                    //Use the first row to add columns to DataTable.
                    if (firstRow)
                    {
                        foreach (IXLCell cell in row.Cells())
                        {
                            // Definisco tipo di colonna in tabella dt a seconda delle colonne di partenza del template
                            switch (cell.Value.ToString())
                            {
                                //case "qta":
                                //    dt.Columns.Add(cell.Value.ToString(),typeof(Int64));
                                //    break;

                                case "qta":
                                case "prezzo":
                                case "cambiom":
                                case "ctv_tit_dr":
                                case "coma_dep_dr":
                                case "spese_dr":
                                case "ctv_tot_dr":
                                case "imposte_dr":
                                case "ctv_tit_dn":
                                case "imposte_dn":
                                case "ctv_tot_dn":
                                    dt.Columns.Add(cell.Value.ToString(), typeof(double));
                                    break;
                                default:
                                    dt.Columns.Add(cell.Value.ToString());
                                    break;
                            }
                            // dt.Columns.Add(cell.Value.ToString()); 
                        }
                        firstRow = false;
                    }
                    else
                    {
                        //Add rows to DataTable.
                        dt.Rows.Add();
                        int i = 0;

                        foreach (IXLCell cell in row.Cells(row.FirstCellUsed().Address.ColumnNumber, row.LastCellUsed().Address.ColumnNumber))
                        {
                            dt.Rows[dt.Rows.Count - 1][i] = cell.Value.ToString();
                            i++;
                        }
                    }
                }

                return dt;
            }
        }

        protected void btnOutputGuardian_Click(object sender, EventArgs e)
        {
            // Metodo 1) - Crea file di output in formato .xlsx per guardian relativo a MS Trades
            // Prepare the response
            string fileType = string.Empty;
            if (radioButtonList.SelectedIndex == 0) fileType = "msreset";
            else fileType = "normaltrade";
            string excelTrades = string.Format("{0}_trades_{1:yyyyMMdd}.xlsx", fileType, DateTime.Today.Date);
            HttpResponse httpResponse = Response;
            httpResponse.Clear();
            httpResponse.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
            httpResponse.AddHeader("content-disposition", string.Format("attachment;filename={0}", excelTrades));

            // Flush the workbook to the Response.OutputStream
            DataTable dt = (DataTable)Session["dtOutputTrades"];
            using (var workbook = new XLWorkbook())
            {
                using (MemoryStream memoryStream = new MemoryStream())
                {
                    workbook.Worksheets.Add(dt, "Foglio1");
                    workbook.SaveAs(memoryStream);
                    memoryStream.WriteTo(httpResponse.OutputStream);
                    memoryStream.Close();
                }
            }
            httpResponse.End();

            // Metodo 2) Utilizzo AspxGridviewExporter per salvare xlsx in ouutput
            //if (Session["dtOutputTrades"] != null)
            //{
            //    gvOutput.DataSource = (DataTable)Session["dtOutputTrades"];
            //    gvOutput.DataBind();

            //    string fileType = string.Empty;
            //    if (radioButtonList.SelectedIndex == 0) fileType = "msreset";
            //    else fileType = "normaltrade";
            //    ASPxGridViewExporter1.FileName = String.Format("{0}_trades_{1:yyyyMMdd}.xlsx", fileType, DateTime.Today.Date);
            //    XlsxExportOptionsEx option = new XlsxExportOptionsEx { ExportType = ExportType.DataAware, TextExportMode = TextExportMode.Value };
            //    ASPxGridViewExporter1.WriteXlsxToResponse(option);
            //}

        }
    }
}