private List <PrintResponseModel> GetResponseList(string responseId, bool FromFinal)
        {
            List <FormsHierarchyDTO> FormsHierarchy = GetFormsHierarchy(responseId);
            SurveyModel SurveyModel = new SurveyModel();

            // SurveyModel.Form = form;
            SurveyModel.RelateModel = Mapper.ToRelateModel(FormsHierarchy, Session["RootFormId"].ToString());

            //   Common.Message.SurveyAnswerResponse answerResponse = _isurveyFacade.GetSurveyAnswerResponse(responseId);
            //
            List <PrintResponseModel> PrintList = new List <PrintResponseModel>();

            foreach (var form in SurveyModel.RelateModel)
            {
                foreach (var answerResponse in form.ResponseIds)
                {
                    PrintResponseModel PrintResponseModel        = new PrintResponseModel();
                    SurveyInfoModel    surveyInfoModel           = GetSurveyInfo(answerResponse.SurveyId);
                    Common.Message.SurveyControlsRequest Request = new Common.Message.SurveyControlsRequest();
                    Request.SurveyId = answerResponse.SurveyId;
                    Common.Message.SurveyControlsResponse List = _isurveyFacade.GetSurveyControlList(Request);

                    var QuestionAnswerList = SurveyHelper.GetQuestionAnswerList(answerResponse.XML, List);
                    var SourceTables       = _isurveyFacade.GetSourceTables(Session["RootFormId"].ToString());
                    PrintResponseModel.ResponseList  = SurveyHelper.SetCommentLegalValues(QuestionAnswerList, List, surveyInfoModel, SourceTables);
                    PrintResponseModel.NumberOfPages = SurveyHelper.GetNumberOfPags(answerResponse.XML);
                    PrintResponseModel.SurveyName    = surveyInfoModel.SurveyName;
                    PrintResponseModel.CurrentDate   = DateTime.Now.ToString();
                    PrintResponseModel.ResponseId    = responseId;
                    PrintResponseModel.SurveyId      = form.FormId;
                    PrintResponseModel.IsFromFinal   = FromFinal;
                    PrintList.Add(PrintResponseModel);
                }
            }

            return(PrintList);
        }
        private ActionResult DoDownLoad(PublishModel Model)
        {
            string Newfile         = Model.FileName + ".CSV";
            bool   IsAuthenticated = Model.IsAuthenticated = bool.Parse(Session["IsAuthenticated"].ToString());

            if (string.IsNullOrEmpty(Model.SurveyKey))
            {
                ModelState.AddModelError("SurveyKey", "Survey Id is required.");;
            }
            //if (Model.UserPublishKey == Guid.Empty)
            //{
            //    ModelState.AddModelError("UserPublishKey", "Security Token is required."); ;
            //}
            ModelState["EndDate"].Errors.Clear();

            ModelState["Path"].Errors.Clear();
            ModelState["SurveyName"].Errors.Clear();
            if (IsAuthenticated)
            {
                ModelState["UserPublishKey"].Errors.Clear();
            }
            if (ModelState.IsValid)
            {
                Stopwatch stopwatch = new Stopwatch();
                try
                {
                    stopwatch.Start();
                    int  NumberOfPages       = 0;
                    int  InitialSetupCounter = 0;
                    bool IsDraftMode         = false;
                    /////////////////////////////////////////////////
                    // get a list of all controls in the survey
                    PrintResponseModel PrintResponseModel        = new PrintResponseModel();
                    Common.Message.SurveyControlsRequest Request = new Common.Message.SurveyControlsRequest();
                    Request.SurveyId = Model.SurveyKey;
                    Common.Message.SurveyControlsResponse List = _isurveyFacade.GetSurveyControlList(Request);


                    ///////////////////////////////////////////
                    //   string path = Directory.GetParent(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)).FullName;
                    if (Model.SurveyMode.ToString() == "1")
                    {
                        IsDraftMode = false;
                    }
                    else
                    {
                        IsDraftMode = true;
                    }
                    // if (Environment.OSVersion.Version.Major >= 6)
                    // {
                    // path = Directory.GetParent(path).ToString();
                    //}



                    // FullPath = path + @"\Downloads\" + FileName;
                    FullPath = @Model.Path;
                    ///get  Response size
                    if (IsAuthenticated)
                    {
                        Model.UserPublishKey = GetUserPublishKey(Model.SurveyKey.ToString());
                    }
                    SurveyAnswerRequest  SurveyAnswerRequest  = SetMessageObject(true, Model.SurveyKey, new Guid(Model.UserPublishKey), Model.OrganizationKey, IsDraftMode, 0);
                    SurveyAnswerResponse SurveyAnswerResponse = new Common.Message.SurveyAnswerResponse();
                    SurveyAnswerResponse = _isurveyFacade.GetSurveyAnswerResponse(SurveyAnswerRequest);
                    int PageSize = SurveyAnswerResponse.PageSize;

                    int TotalCount = 0;


                    FileInfo newFile = new FileInfo(Newfile);

                    int Row = 1;
                    using (ExcelPackage package = new ExcelPackage(newFile))
                    {
                        if (PageSize > 0)
                        {
                            var Sheet = package.Workbook.Worksheets.Add("Sheet1");
                            for (int i = 1; SurveyAnswerResponse.NumberOfPages > i - 1; i++)
                            {
                                SurveyAnswerRequest = SetMessageObject(false, Model.SurveyKey, new Guid(Model.UserPublishKey), Model.OrganizationKey, IsDraftMode, 0, i, PageSize);
                                SurveyAnswerResponse Response = _isurveyFacade.GetSurveyAnswerResponse(SurveyAnswerRequest);


                                foreach (var item in Response.SurveyResponseList)
                                {
                                    //  var item = Response.SurveyResponseList[0];
                                    var ResponseValueList = SurveyHelper.GetQuestionAnswerList(item.XML, List);
                                    if (InitialSetupCounter == 0)
                                    {
                                        NumberOfPages = SurveyHelper.GetNumberOfPags(item.XML);
                                        InitialSetupCounter++;
                                        int col = 1;
                                        for (int j = 1; NumberOfPages > j - 1; j++)
                                        {
                                            // var ws = package.Workbook.Worksheets.FirstOrDefault(x => x.Name == "Page" + j);
                                            // if (ws == null)
                                            //{
                                            //var sheet = package.Workbook.Worksheets.Add("Page" + j);
                                            // }
                                            var List1 = ResponseValueList.Where(x => x.PageNumber == j).ToList();

                                            // var Sheet = package.Workbook.Worksheets["Page" + j];

                                            foreach (var item1 in List1)
                                            {
                                                Row = 1;
                                                Sheet.Cells[Row, col].Value = item1.ControlName;
                                                switch (item1.ControlType)
                                                {
                                                case "NumericTextBox":
                                                    Sheet.Column(col).Style.Numberformat.Format = "0.00";
                                                    break;

                                                case "Date":
                                                    Sheet.Column(col).Style.Numberformat.Format = "mm-dd-yyyy";
                                                    break;

                                                default:

                                                    break;
                                                }
                                                Sheet.Cells[Row + 1, col].Value = item1.Value;
                                                col++;
                                            }
                                        }
                                        Row++;
                                    }//if
                                    else
                                    {
                                        int col = 1;
                                        for (int j = 1; NumberOfPages > j - 1; j++)
                                        {
                                            var List2 = ResponseValueList.Where(x => x.PageNumber == j).ToList();

                                            //var Sheet = package.Workbook.Worksheets["Page" + j];
                                            // int col = 1;
                                            foreach (var ListItem in List2)
                                            {
                                                Sheet.Cells[Row, col].Value = ListItem.Value;
                                                col++;
                                            }
                                        }
                                    }// else

                                    Row++;
                                }// Outer Foreach
                            }
                            using (var memoryStream = new MemoryStream())
                            {
                                Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                                Response.AddHeader("content-disposition", "attachment;  filename=" + Newfile);
                                package.SaveAs(memoryStream);
                                memoryStream.WriteTo(Response.OutputStream);
                                Response.Flush();
                                Response.End();
                            }
                        }
                        else
                        {
                            Model.SuccessfulPublish = false;
                            ModelState.AddModelError("Error", "No records found for the download criteria entered.");


                            ViewBag.SurveyNameList1 = GetAllSurveysByOrgId(Session["OrgId"].ToString());;
                            return(View("Index", Model));
                        }
                    }//using
                    stopwatch.Stop();
                    Model.Path = @FullPath;
                    Model.SuccessfulPublish = true;
                    Model.TimeElapsed       = stopwatch.Elapsed.ToString();
                    Model.RecordCount       = TotalCount - 1;
                    return(View("Index", Model));
                }
                catch (Exception ex)
                {
                    Model.SuccessfulPublish = false;
                    ModelState.AddModelError("Error", "Please validate the information provided and try downloading again.");
                    ViewBag.SurveyNameList1 = GetAllSurveysByOrgId(Session["OrgId"].ToString());;
                    return(View("Index", Model));
                }
            }
            else
            {
                Model.SuccessfulPublish = false;
                ViewBag.SurveyNameList1 = GetAllSurveysByOrgId(Session["OrgId"].ToString());;
                return(View("Index", Model));
            }
        }