예제 #1
0
        private ResultObject ImportExcel(string _path, string _tempUploadFolder, string _storeFolder)
        {
            try
            {
                var result = new ResultObject {
                    Success = -1
                };

                List <Question> listFromFiles = new List <Question>();

                string extension = Path.GetExtension(_path);

                IWorkbook workbook = null;
                using (FileStream fs = new FileStream(_path, FileMode.Open, FileAccess.Read))
                {
                    if (extension == ".xlsx")
                    {
                        workbook = new XSSFWorkbook(fs);
                    }
                    else if (extension == ".xls")
                    {
                        workbook = new HSSFWorkbook(fs);
                    }
                }
                ISheet   sheet       = workbook.GetSheetAt(0);
                Question ques        = null;
                string   FILEERROR   = "";
                int      questionRow = 0;

                for (int rowIndex = 1; rowIndex <= sheet.LastRowNum; rowIndex++)
                {
                    var    currentRow = sheet.GetRow(rowIndex);
                    string flag       = GetValueCell(currentRow.GetCell(0));
                    if ("".Equals(flag) || "END".Equals(flag.ToUpper()))
                    {
                        if (ValidateQuestion(ques, questionRow, ref FILEERROR))
                        {
                            listFromFiles.Add(ques);
                        }
                        break;
                    }
                    if ("1".Equals(flag))
                    {
                        if (ValidateQuestion(ques, questionRow, ref FILEERROR))
                        {
                            listFromFiles.Add(ques);
                        }
                        ques = GetQuestionFromRow(currentRow, rowIndex, ref FILEERROR);
                    }
                    if ("2".Equals(flag))
                    {
                        var ans = GetAnswerFromRow(currentRow, rowIndex, ref FILEERROR);
                        if (ans != null)
                        {
                            ques.Answers.Add(ans);
                        }
                    }
                }
                if ("".Equals(FILEERROR))
                {
                    result.Success = service.Import(listFromFiles);
                    if (result.Success >= 1)
                    {
                        foreach (var question in listFromFiles)
                        {
                            AcceptFile(question.Content, _tempUploadFolder, _storeFolder);
                        }
                    }
                    ClearFile(_path);
                    return(result);
                }
                else
                {
                    ClearFile(_path);
                    result.Message = FILEERROR;
                    return(result);
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }