工作中遇到 分頁導出(週五)數據表格,導出一個時段的週五的數據,分頁sheet表單和對應的列表。一段時間內每個週五在Excel中生成列表中的一條數據和一個表格。
這裏主要的記錄一下分頁sheet表單功能。
public static void main(String[] args) throws Exception {
/*
* 分sheet導出查詢記錄
*/
List<TimeDay> foolist=getStudentInfo();
String templateDir = "src/main/java/templates/formInfoTemplate_outout.xls";
String targetDir="e:/excel/export/testMultipleSheets.xls";
List sheetNames = new ArrayList();
for(int i=0;i<foolist.size();i++){
TimeDay sa = (TimeDay)foolist.get(i);
sheetNames.add(sa.getEveryFriday());
}
InputStream is = new BufferedInputStream(new FileInputStream(templateDir));
XLSTransformer transformer = new XLSTransformer();
HSSFWorkbook resultWorkBook = null;
try {
resultWorkBook = (HSSFWorkbook) transformer.transformMultipleSheetsList(is, foolist, sheetNames, "TimeDay", new HashMap(), 0);
} catch (InvalidFormatException e) {
e.printStackTrace();
}
OutputStream os = new BufferedOutputStream(new FileOutputStream(targetDir));
try {
resultWorkBook.write(os);
} catch (IOException e) {
e.printStackTrace();
}
}
獲取當年1月1號~當前時間的數據
private static List<TimeDay> getStudentInfo() throws Exception {
List<TimeDay> TimeDays = new ArrayList<TimeDay>();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String startDate = getCurrYearFirst()+"-01-01";
String currenDate = sdf.format(new Date());
List<String> allWeekly2 = getAllWeekly2(startDate, currenDate);
if(allWeekly2 != null && allWeekly2.size() > 0){
for (int aa = 0;aa < allWeekly2.size();aa++){
TimeDays.add(new TimeDay(allWeekly2.get(aa)));
}
}
return TimeDays;
}
獲取當年的第一天日期
public static String getCurrYearFirst(){
Calendar currCal=Calendar.getInstance();
int currentYear = currCal.get(Calendar.YEAR);
return String.valueOf(currentYear);
}
獲取起始時間startDate 至 結束時間endDate 時間段中所有的爲星期五的數據(沒有排除週末和節假日爲週五的情況)
public static List<String> getAllWeekly2(String startDate, String endDate) throws Exception {
// 獲取兩個日期之間的所有日期
DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Calendar calBegin = Calendar.getInstance();
calBegin.setTime(format.parse(startDate));
Calendar calEnd = Calendar.getInstance();
calEnd.setTime(format.parse(endDate));
List<String> Datelist = new ArrayList<String>();
while (format.parse(endDate).after(calBegin.getTime())) {
calBegin.add(Calendar.DAY_OF_MONTH, 1);
Date date = calBegin.getTime();
SimpleDateFormat dateFm = new SimpleDateFormat("EEEE");
String currSun = dateFm.format(date);
if (currSun.equals("星期五")) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String format1 = sdf.format(date);
Datelist.add(format1);
}
}
return Datelist;
}
這裏記錄一下主要的方法,這裏就不介紹導出Excel的模板。