public void ExportUser(int userId, out string pathExcel) { string path = EVE.Library.Config.WebConfig.UploadPath + "/InputModul/"; var userModel = new UserService().Get(userId); var departments = departmentDAL.Entities.Where(p => p.CompanyID == userModel.CompanyID); string[] genderArray = new string[] { "男", "女" }; int itemIndex = 0; string[] departArray = new string[departments.Count()]; departments.ForEach(item => { departArray[itemIndex] = item.Name; itemIndex++; }); var roles = roleDAL.Entities.Where(p => p.CompanyID == userModel.CompanyID); string[] roleArray = new string[roles.Count()]; int itemRole = 0; roles.ForEach(item => { roleArray[itemRole] = item.Name; itemRole++; }); DirectoryInfo dirInfo = new DirectoryInfo(path + @"/Temp/"); FileInfo[] files = dirInfo.GetFiles(); foreach (FileInfo file in files) { file.Delete(); } //要操作的excel文件路径 FileStream fsStream = File.OpenRead(path + @"userInputDataModul.xls"); HSSFWorkbook workbook = new HSSFWorkbook(fsStream); var userInputSheet = workbook.GetSheet(workbook.GetSheetName(0)); CellRangeAddressList dRegions = new CellRangeAddressList(1, 8888, 0, 0); DVConstraint dConstraint = DVConstraint.CreateExplicitListConstraint(departArray); HSSFDataValidation dDataValidate = new HSSFDataValidation(dRegions, dConstraint); userInputSheet.AddValidationData(dDataValidate); CellRangeAddressList gRegions = new CellRangeAddressList(1, 8888, 3, 3); DVConstraint gConstraint = DVConstraint.CreateExplicitListConstraint(genderArray); HSSFDataValidation gDataValidate = new HSSFDataValidation(gRegions, gConstraint); userInputSheet.AddValidationData(gDataValidate); CellRangeAddressList rRegions = new CellRangeAddressList(1, 8888, 8, 8); DVConstraint rConstraint = DVConstraint.CreateExplicitListConstraint(roleArray); HSSFDataValidation rDataValidate = new HSSFDataValidation(rRegions, rConstraint); userInputSheet.AddValidationData(rDataValidate); pathExcel = path + @"/Temp/userInputDataModul_" + Guid.NewGuid().ToStr() + ".xls"; //把编辑过后的工作薄重新保存为excel文件 FileStream fs2 = File.Create(pathExcel); workbook.Write(fs2); fs2.Close(); }
浙公网安备 33010602011771号