博 之 文

以 拼 搏 设 计 梦 想 , 以 恒 心 编 程 明 天
  首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

poi 导出excel(下拉框形式)

Posted on 2015-07-03 10:50  IsNull_Soft  阅读(422)  评论(0)    收藏  举报
        
 [HttpGet]
        [Route("api/JobDescription/ExportJobActionModul")]
        public HttpResponseMessage ExportTargetModul(int userId, string jobdescriptionIds)
        {
            string pathExcel = "";
            jobdescriptionService.ExportTargetModul(userId, jobdescriptionIds, out pathExcel);

            var result = Request.CreateResponse(HttpStatusCode.OK);
            result.Content = new StreamContent(new FileStream(pathExcel, FileMode.Open, FileAccess.Read));
            result.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue(DispositionTypeNames.Attachment);
            result.Content.Headers.ContentDisposition.FileName = System.Web.HttpUtility.UrlEncode("岗位职责导入模板.xls");
            result.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/vnd.ms-excel");

            return result;
        }


#region 导出岗位说明书模板
        public void ExportTargetModul(int userId, string jobdescriptionIds, out string pathExcel)
        {
            string path = EVE.Library.Config.WebConfig.UploadPath + "/InputModul/";
            var userModel = new UserService().Get(userId);

            var jobDutys = jobDutyDAL.Entities.Where(p => p.CompanyID == userModel.CompanyID);
            string[] jobDutyArray = new string[jobDutys.Count()];
            int itemIndex = 0;
            jobDutys.ForEach(item =>
            {
                jobDutyArray[itemIndex] = item.Name;
                itemIndex++;
            });

            DirectoryInfo dirInfo = new DirectoryInfo(path + @"/Temp/");
            FileInfo[] files = dirInfo.GetFiles();
            foreach (FileInfo file in files) { file.Delete(); }

            //要操作的excel文件路径
            FileStream fsStream = File.OpenRead(path + @"InputJobDescription.xls");
            HSSFWorkbook workbook = new HSSFWorkbook(fsStream);

            var userInputSheet = workbook.GetSheet(workbook.GetSheetName(0));
            CellRangeAddressList jRegions = new CellRangeAddressList(1, 8888, 0, 0);
            DVConstraint jConstraint = DVConstraint.CreateExplicitListConstraint(jobDutyArray);
            HSSFDataValidation jDataValidate = new HSSFDataValidation(jRegions, jConstraint);
            userInputSheet.AddValidationData(jDataValidate);

            var sheetList = jobdescriptionIds.Split(',').Select(p => { return p.ToInt(); });
            var sheetNameList = jobdescriptionDAL.Entities.Where(p => sheetList.Any(r => r == p.ID)).Select(p => p.Name);

            var sheetTemp = workbook.GetSheet(workbook.GetSheetName(0));
            workbook.Remove(sheetTemp);

            sheetNameList.ForEach(item =>
            {
                sheetTemp.CopySheet(item, true);
            });

            pathExcel = path + @"/Temp/InputJobDescription_" + Guid.NewGuid().ToStr() + ".xls";
            //把编辑过后的工作薄重新保存为excel文件
            FileStream fs2 = File.Create(pathExcel);

            workbook.Write(fs2);
            fs2.Close();

        }