/**
*
* 定时任务
* @author yanwang
*/
@Component
@Lazy(value=false)
public class AutoTask {
/**
* 课程管理service
*/
@Autowired
private CourseService courseService;
/**
*
* 修改模板级联框信息.
*/
@Scheduled(cron = "* 0/30 * * * ? ")
public void updateXls(){
System.out.println("new Date().getTime()-----"+new Date().getTime());
//存级联信息
List<String> subjectList = getXlsDropDownbox();
//级联信息模板文件路径
WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();
String filePath = webApplicationContext.getServletContext().getRealPath(SysCode.BDFH.XG)
+ SysCode.TEMPLATE_SUBJECT_PATH;
//将从数据库取出的级联信息对模板下拉框进行更新并修改下拉框引用
updateXlsDropDownbox(filePath, subjectList);
}
/**
*
* 得到级联信息导入模板下拉框内容.
*/
public List<String> getXlsDropDownbox(){
//存级联信息
List<String> subjectList = new ArrayList<String>();
//得到年级字典
List<PublicDict> gradeList = PublicZdCacheLoadImpl.getListByLxjp("GRADE");
//按年级排序
Collections.sort(gradeList, new Comparator<PublicDict>(){
@Override
public int compare(PublicDict o1, PublicDict o2){
int i = Integer.parseInt(o1.getDm()) - Integer.parseInt(o2.getDm());
if(i == 0){
return Integer.parseInt(o1.getDm()) - Integer.parseInt(o2.getDm());
}
return i;
}
});
for(int i=0;i<gradeList.size();i++) {
String gradeName = gradeList.get(i).getMc();
String grade = gradeList.get(i).getDm();
List<AppCourse> courseList = courseService.queryCourseToGrade(grade);
//通过年级查询课程
for(int j=0;j<courseList.size();j++){
String courseName = courseList.get(j).getName();
subjectList.add(gradeName+"-"+courseName);
}
}
return subjectList;
}
/**
*
* 更新下拉框内容并进行引用.
*/
@SuppressWarnings("resource")
public void updateXlsDropDownbox(String filePath,List<String> subjectList){
try{
InputStream fis = new FileInputStream(new File(filePath));
HSSFWorkbook wb = new HSSFWorkbook(fis);
//找到表中的第一个sheet
HSSFSheet sheet1 = wb.getSheetAt(0);
HSSFSheet sheet = wb.getSheetAt(1);
//将获取的数据写入模板中
int z;
for(z=0;z<subjectList. size();z++){
if(null != sheet.getRow(z)){
if(null != sheet.getRow(z).getCell(0)){
sheet.getRow(z).getCell(0).setCellValue(subjectList.get(z));
}else{
// 在当前行的6列上创建单元格
Cell cell = sheet.getRow(z).createCell(0);
// 添加内容至单元格
cell.setCellValue(subjectList.get(z));
}
}else{
// 在索引z的位置创建行
Row row = sheet.createRow(z);
// 在当前行的6列上创建单元格
Cell cell = row.createCell(0);
// 添加内容至单元格
cell.setCellValue(subjectList.get(z));
}
}
//如果更新的数据没有之前数据多,将将之前没有被更新的数据设为""
while(null != sheet.getRow(z) && null != sheet.getRow(z).getCell(0)){
sheet.getRow(z).getCell(0).setCellValue("");
z++;
}
//下拉框引用另一列
int size = subjectList.size();
String strFormula = sheet.getSheetName()+"!$A$1:$A$"+size ;
//设置第一列的1-1000行为下拉列表
CellRangeAddressList regions = new CellRangeAddressList(1, 1000, 4, 4);
//创建下拉列表数据
DVConstraint constraint = DVConstraint.createFormulaListConstraint(strFormula);//add
//绑定
HSSFDataValidation dataValidation = new HSSFDataValidation(regions,constraint);//add
dataValidation.createErrorBox("Error", "Error");
dataValidation.createPromptBox("", null);
sheet1.addValidationData(dataValidation);
OutputStream fos = new FileOutputStream(filePath);
wb.write(fos);
fis.close();
fos.close();
}catch (Exception e){
e.printStackTrace();
}
}
*
* 定时任务
* @author yanwang
*/
@Component
@Lazy(value=false)
public class AutoTask {
/**
* 课程管理service
*/
@Autowired
private CourseService courseService;
/**
*
* 修改模板级联框信息.
*/
@Scheduled(cron = "* 0/30 * * * ? ")
public void updateXls(){
System.out.println("new Date().getTime()-----"+new Date().getTime());
//存级联信息
List<String> subjectList = getXlsDropDownbox();
//级联信息模板文件路径
WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();
String filePath = webApplicationContext.getServletContext().getRealPath(SysCode.BDFH.XG)
+ SysCode.TEMPLATE_SUBJECT_PATH;
//将从数据库取出的级联信息对模板下拉框进行更新并修改下拉框引用
updateXlsDropDownbox(filePath, subjectList);
}
/**
*
* 得到级联信息导入模板下拉框内容.
*/
public List<String> getXlsDropDownbox(){
//存级联信息
List<String> subjectList = new ArrayList<String>();
//得到年级字典
List<PublicDict> gradeList = PublicZdCacheLoadImpl.getListByLxjp("GRADE");
//按年级排序
Collections.sort(gradeList, new Comparator<PublicDict>(){
@Override
public int compare(PublicDict o1, PublicDict o2){
int i = Integer.parseInt(o1.getDm()) - Integer.parseInt(o2.getDm());
if(i == 0){
return Integer.parseInt(o1.getDm()) - Integer.parseInt(o2.getDm());
}
return i;
}
});
for(int i=0;i<gradeList.size();i++) {
String gradeName = gradeList.get(i).getMc();
String grade = gradeList.get(i).getDm();
List<AppCourse> courseList = courseService.queryCourseToGrade(grade);
//通过年级查询课程
for(int j=0;j<courseList.size();j++){
String courseName = courseList.get(j).getName();
subjectList.add(gradeName+"-"+courseName);
}
}
return subjectList;
}
/**
*
* 更新下拉框内容并进行引用.
*/
@SuppressWarnings("resource")
public void updateXlsDropDownbox(String filePath,List<String> subjectList){
try{
InputStream fis = new FileInputStream(new File(filePath));
HSSFWorkbook wb = new HSSFWorkbook(fis);
//找到表中的第一个sheet
HSSFSheet sheet1 = wb.getSheetAt(0);
HSSFSheet sheet = wb.getSheetAt(1);
//将获取的数据写入模板中
int z;
for(z=0;z<subjectList. size();z++){
if(null != sheet.getRow(z)){
if(null != sheet.getRow(z).getCell(0)){
sheet.getRow(z).getCell(0).setCellValue(subjectList.get(z));
}else{
// 在当前行的6列上创建单元格
Cell cell = sheet.getRow(z).createCell(0);
// 添加内容至单元格
cell.setCellValue(subjectList.get(z));
}
}else{
// 在索引z的位置创建行
Row row = sheet.createRow(z);
// 在当前行的6列上创建单元格
Cell cell = row.createCell(0);
// 添加内容至单元格
cell.setCellValue(subjectList.get(z));
}
}
//如果更新的数据没有之前数据多,将将之前没有被更新的数据设为""
while(null != sheet.getRow(z) && null != sheet.getRow(z).getCell(0)){
sheet.getRow(z).getCell(0).setCellValue("");
z++;
}
//下拉框引用另一列
int size = subjectList.size();
String strFormula = sheet.getSheetName()+"!$A$1:$A$"+size ;
//设置第一列的1-1000行为下拉列表
CellRangeAddressList regions = new CellRangeAddressList(1, 1000, 4, 4);
//创建下拉列表数据
DVConstraint constraint = DVConstraint.createFormulaListConstraint(strFormula);//add
//绑定
HSSFDataValidation dataValidation = new HSSFDataValidation(regions,constraint);//add
dataValidation.createErrorBox("Error", "Error");
dataValidation.createPromptBox("", null);
sheet1.addValidationData(dataValidation);
OutputStream fos = new FileOutputStream(filePath);
wb.write(fos);
fis.close();
fos.close();
}catch (Exception e){
e.printStackTrace();
}
}
}
//springmvc.xml中定时任务配置
<beans xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd">
<context:component-scan base-package="com.iflytek.autotask" />
<task:annotation-driven/>