[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(); }
浙公网安备 33010602011771号