using System;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.HSSF.Util;
using NPOI.SS.Util;
using System.Data;
using System.Resources;
using System.Globalization;
using System.IO;

public partial class Masters_ExportData : System.Web.UI.Page
{
    #region "Variable Declaration"
    ResourceManager rm;
    CultureInfo ci;
    cls_Common objCommon = new cls_Common();
    Cls_CheckUser objCheckUser = new Cls_CheckUser();
    SMSNotification objsms = new SMSNotification();
    DataSet ds_Checkuser = new DataSet();
    DataSet ds_Templates = new DataSet();
    string gstrUserGroupName = string.Empty;
    int EmployeeID = 0;
    string source_Language;
    string strScript = string.Empty;
    string strFromdate, gstrDateFormat, strTodate;
    DataSet dsSMSLogDetails = new DataSet();
    #endregion
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["SMSLog"] == "SMSLogDetails")
        {
            //ExportExcel(Convert.ToInt32(Session["parentLevelId"]), Request.QueryString["TemplateName"], Request.QueryString["FromDate"], Request.QueryString["ToDate"], Request.QueryString[""], Request.QueryString["Status"], Convert.ToString(Session["UserGroupName"]), Convert.ToString(Session["GradeLevel"]));
        }
    }
    protected void ExportExcel(int parentId, string TemplateName, string receivers, string fromdate, string todate, int status, string userGroup, string grade)
    {
        dsSMSLogDetails = objsms.SelectLogDetails(parentId, Convert.ToString(TemplateName), Convert.ToString(receivers),
            Convert.ToString(fromdate), Convert.ToString(todate), Convert.ToInt16(status),
            userGroup, grade);
        DataTable dtLevelName = new DataTable();
        HSSFWorkbook workbook = new HSSFWorkbook();
        ISheet sheet = workbook.CreateSheet("SMS Log Details");
        int rowIndex = 0;
        NPOI.SS.UserModel.IRow row = sheet.CreateRow(rowIndex);
        HSSFFont hFont = (HSSFFont)workbook.CreateFont();
        hFont.Boldweight = (short)FontBoldWeight.Bold;
        hFont.Color = HSSFColor.Maroon.Index;
        hFont.FontHeightInPoints = 10;
        HSSFCellStyle hStyle = (HSSFCellStyle)workbook.CreateCellStyle();
        hStyle.SetFont(hFont);
        hStyle.FillForegroundColor = IndexedColors.White.Index;
        hStyle.FillPattern = FillPattern.SolidForeground;
        hStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
        hStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
        hStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
        hStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
        HSSFCellStyle MainHStyle = (HSSFCellStyle)workbook.CreateCellStyle();
        MainHStyle.SetFont(hFont);
        MainHStyle.Alignment = HorizontalAlignment.Center;
        HSSFCellStyle IStyle = (HSSFCellStyle)workbook.CreateCellStyle();
        IStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
        IStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
        IStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
        IStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
        row.CreateCell(0).SetCellValue("SMS Log Details");
        row.GetCell(0).CellStyle = MainHStyle;
        rowIndex++;
        row = sheet.CreateRow(rowIndex);
        row.CreateCell(0).SetCellValue("Sl.No");
        row.CreateCell(1).SetCellValue("Template Name");
        row.CreateCell(2).SetCellValue("Message");
        row.CreateCell(3).SetCellValue("Triggered Time");
        row.CreateCell(4).SetCellValue("Receivers");
        row.CreateCell(5).SetCellValue("Status");
        for (int i = 0; i <= 9; i++)
        {
            row.GetCell(i).CellStyle = hStyle;
        }
        rowIndex++;
        Int32 SlNo = 0;
        foreach (DataRow DR in dsSMSLogDetails.Tables[0].Rows)
        {
            row = sheet.CreateRow(rowIndex);
            SlNo++;
            row.CreateCell(0).SetCellValue(SlNo);
            row.CreateCell(1).SetCellValue(Convert.ToString(DR["TemplateName"]));
            row.CreateCell(2).SetCellValue(Convert.ToString(DR["Message"]));
            row.CreateCell(3).SetCellValue(Convert.ToString(DR["TriggeredTimings"]));
            row.CreateCell(4).SetCellValue(Convert.ToString(DR["Receivers"]));
            row.CreateCell(5).SetCellValue(Convert.ToString(DR["Status"]));
            //Style Added the created Headers.
            for (int i = 0; i <= 9; i++)
            {
                row.GetCell(i).CellStyle = IStyle;
                sheet.AutoSizeColumn(i);
            }
            rowIndex++;
        }
        using (MemoryStream exportData = new MemoryStream())
        {
            workbook.Write(exportData);
            string saveAsFileName = string.Format("SMSLogDetails-{0:d}.xls", DateTime.Now).Replace("/", "-");
            Response.ContentType = "application/vnd.ms-excel";
            Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", saveAsFileName));
            Response.Clear();
            Response.BinaryWrite(exportData.GetBuffer());
            Response.End();
        }
    }
}