Пример #1
0
        public void UploadDateTemplateFile(List<IFormFile> files)
        {
            List<Question> questions = new List<Question>();
            long size = 0;
            #region
            string fileSaveRootPath = Path.Combine(_webHostEnvironment.WebRootPath, "UploadFiles_"+ System.DateTime.Now.ToString("yyyyMMddHHmmss"));//文件保存路径
            if (!Directory.Exists(fileSaveRootPath))
            {
                Directory.CreateDirectory(fileSaveRootPath);
            }
            #endregion
            foreach (var file in files)//遍历上传的Excel文件列表
            {
                #region 获取到对应的Excel文件
                var fileName = file.FileName.Trim('"');//获取文件名

                fileName = Path.Combine(fileSaveRootPath, fileName);//文件上传的路径
                size += file.Length;
                using (FileStream fs = System.IO.File.Create(fileName))//创建文件流
                {
                    file.CopyTo(fs);//将上载文件的内容复制到目标流
                    fs.Flush();//清除此流的缓冲区并导致将任何缓冲数据写入
                }
                #endregion
                #region 读取Excel文件
                using (ExcelPackage package = new ExcelPackage(new FileInfo(fileName)))
                {
                    ExcelWorksheet worksheet = package.Workbook.Worksheets[1];
                    int rowCount = worksheet.Dimension.Rows;
                    int colCount = worksheet.Dimension.Columns;
                    for (int row = 4; row < rowCount+1; row++)
                    {
                        if (worksheet.Cells[row, 1].Value != null && worksheet.Cells[row, 2].Value != null && worksheet.Cells[row, 3].Value != null)
                        {
                            questions.Add(new Question()
                            {
                                QuestionTypeId = _exerciseTypeService.GetIdByType(worksheet.Cells[row, 1].Value.ToString()).Data,
                                Content = worksheet.Cells[row, 2].Value.ToString(),
                                Answer = worksheet.Cells[row, 3].Value.ToString(),
                                AnswerNote = worksheet.Cells[row, 4].Value == null ? string.Empty : worksheet.Cells[row, 4].Value.ToString(),
                                QuestionTypeName = worksheet.Cells[row, 1].Value.ToString()//添加这个字段主要是为了管理界面便于取值
                            }) ;
                        }
                        else
                        {

                        }

                    }
                }
                #endregion
            }
            if (_exerciseService.AddQuestions(questions).IsSuccess)
            {
                ViewBag.Message = $"{files.Count}个文件 /{size}字节上传成功!";
            }
            else
            {
                ViewBag.Message = "上传失败!";
            }
            #region
            if (Directory.Exists(fileSaveRootPath))
            {
                Directory.Delete(fileSaveRootPath, true);
            }

            #endregion
        }