Пример #1
0
 public bool AddFillOut(List <FillOut> fillOuts)
 {
     if (fillOuts.Count > 0)
     {
         foreach (var item in fillOuts)
         {
             FillOut fillOut = new FillOut
             {
                 GameID        = item.GameID,
                 QuestionaryID = item.QuestionaryID,
                 QuestionID    = item.QuestionID,
                 SelectID      = item.SelectID,
                 Value         = item.Value
             };
             gameClubEntities.FillOut.Add(fillOut);
         }
         gameClubEntities.SaveChanges();
         return(true);
     }
     return(false);
 }
 public ActionResult AddFillOut(string QuestionaryID, FormCollection formCollection)
 {
     if (!string.IsNullOrEmpty(QuestionaryID))
     {
         List <FillOut> fillOuts = new List <FillOut>();
         EFQuestionary.DelFillOut(QuestionaryID, UserID);
         IEnumerable <Question> questions = EFQuestionary.Questions.Where(q => q.QuestionaryID == QuestionaryID);
         foreach (var item in questions)
         {
             foreach (var items in EFQuestionary.Selects.Where(q => q.QuestionID == item.QuestionID))
             {
                 if (item.Type)
                 {
                     if (formCollection[items.QuestionID + items.SelectID] != null)
                     {
                         FillOut fillOut = new FillOut
                         {
                             GameID        = UserID,
                             QuestionaryID = QuestionaryID,
                             QuestionID    = items.QuestionID,
                             SelectID      = items.SelectID,
                         };
                         if (items.Type)
                         {
                             if (!string.IsNullOrEmpty(formCollection[items.QuestionID + items.SelectID + "input"].ToString()))
                             {
                                 fillOut.Value = formCollection[items.QuestionID + items.SelectID + "input"].ToString();
                             }
                             else
                             {
                                 fillOut.Value = "未填写";
                             }
                         }
                         else
                         {
                             fillOut.Value = items.Value;
                         }
                         fillOuts.Add(fillOut);
                     }
                 }
                 else
                 {
                     string SelectID;
                     if (formCollection[items.QuestionID] != null)
                     {
                         SelectID = formCollection[items.QuestionID].ToString();
                     }
                     else
                     {
                         SelectID = null;
                     }
                     if (SelectID != null && SelectID.Equals(items.SelectID))
                     {
                         FillOut fillOut = new FillOut
                         {
                             GameID        = UserID,
                             QuestionaryID = QuestionaryID,
                             QuestionID    = items.QuestionID,
                             SelectID      = formCollection[items.QuestionID].ToString(),
                         };
                         if (items.Type)
                         {
                             if (!string.IsNullOrEmpty(formCollection[items.QuestionID + items.SelectID + "input"].ToString()))
                             {
                                 fillOut.Value = formCollection[items.QuestionID + items.SelectID + "input"].ToString();
                             }
                             else
                             {
                                 fillOut.Value = "未填写";
                             }
                         }
                         else
                         {
                             fillOut.Value = items.Value;
                         }
                         fillOuts.Add(fillOut);
                     }
                 }
             }
         }
         EFQuestionary.AddFillOut(fillOuts);
         QuestionViewModel = GetQuestionViewModel(QuestionaryID);
         if (Request.IsAjaxRequest())
         {
             return(PartialView("_Questions", QuestionViewModel));
         }
         return(View(QuestionViewModel));
     }
     return(View(false));
 }
Пример #3
0
        public FileResult ExportQuestionary(string QuestionaryID)
        {
            CellRangeAddress cellRange;
            //创建Excel文件的对象
            HSSFWorkbook xSSFWorkbook = new HSSFWorkbook();
            //添加一个sheet
            ISheet sheet1 = xSSFWorkbook.CreateSheet("Sheet1");
            //获取list数据
            List <Question>   questions   = EFQuestionary.Questions.Where(q => q.QuestionaryID == QuestionaryID).OrderBy(q => q.QuestionID).ToList();
            List <Select>     selects     = EFQuestionary.Selects.ToList();
            List <FillOut>    fillOuts    = EFQuestionary.FillOuts.Where(f => f.QuestionaryID == QuestionaryID).OrderBy(f => f.GameID).ToList();
            List <GameMember> gameMembers = EFGameMember.gameMembers.ToList();
            //给sheet1添加第一行的头部标题
            IRow row1 = sheet1.CreateRow(0);

            row1.CreateCell(0).SetCellValue("游戏ID");
            row1.CreateCell(1).SetCellValue("游戏昵称");
            int count = 2;

            foreach (var item in questions)
            {
                row1.CreateCell(count).SetCellValue(item.QuestionContext);
                cellRange = new CellRangeAddress(0, 0, count, count + selects.Where(s => s.QuestionID == item.QuestionID).Count() - 1);
                sheet1.AddMergedRegion(cellRange);
                count += selects.Where(s => s.QuestionID == item.QuestionID).Count();
            }
            //将数据逐步写入sheet1各个行
            IRow row2 = sheet1.CreateRow(1);

            row2.CreateCell(0).SetCellValue("选项值");
            cellRange = new CellRangeAddress(1, 1, 0, 1);
            sheet1.AddMergedRegion(cellRange);
            count = 2;
            foreach (var item in questions)
            {
                foreach (var items in selects.Where(s => s.QuestionID == item.QuestionID).OrderBy(s => s.SelectID))
                {
                    if (items.Type)
                    {
                        row2.CreateCell(count++).SetCellValue("其他");
                    }
                    else
                    {
                        row2.CreateCell(count++).SetCellValue(items.Value);
                    }
                }
            }
            count = 2;
            int counts;

            foreach (var GameID in fillOuts.Select(f => f.GameID).Distinct())
            {
                counts = 0;
                IRow row = sheet1.CreateRow(count++);
                row.CreateCell(counts++).SetCellValue(GameID);
                row.CreateCell(counts++).SetCellValue(gameMembers.Where(g => g.GameID == GameID).FirstOrDefault().GameName.ToString());
                foreach (var item in questions)
                {
                    foreach (var items in selects.Where(s => s.QuestionID == item.QuestionID).OrderBy(s => s.SelectID))
                    {
                        FillOut fillOut = fillOuts.Where(f => f.GameID == GameID && f.SelectID == items.SelectID).FirstOrDefault();
                        if (fillOut != null)
                        {
                            if (items.Type)
                            {
                                row.CreateCell(counts++).SetCellValue(fillOut.Value);
                            }
                            else
                            {
                                row.CreateCell(counts++).SetCellValue("√");
                            }
                        }
                        else
                        {
                            row.CreateCell(counts++).SetCellValue("×");
                        }
                    }
                }
            }

            // 写入到客户端
            MemoryStream ms = new MemoryStream();

            xSSFWorkbook.Write(ms);
            ms.Seek(0, SeekOrigin.Begin);
            return(File(ms, "application/vnd.ms-excel", "编号" + QuestionaryID + "调查问卷结果.xls"));
        }