一、报表导出时候部门和年份都是不确定的,也就是说行列数不确定,所以我之前写的工具类不太适合这种表格的导出,下面我写了一个比较灵活的表格导出方法,具体到每一个单元格,比较灵活,有不足之处还望批评指正。
二、JAVA代码
@Override
public void entryYearsDistribution3(String exportType,String end, HttpServletResponse response) throws IOException {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("入职年限分布表");
CellStyle cellStyle=wb.createCellStyle();
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
DepartmentInfo departmentInfo = new DepartmentInfo();
List<DepartmentInfo> departmentInfos = departmentInfoDAO.selectDepartmentList(departmentInfo);
HSSFRow row = sheet.createRow(0);
Short s = 540;
row.setHeight(s);
HSSFCell cell = row.createCell(0);
cell.setCellValue("行标签");
cell.setCellStyle(cellStyle);
for (int i = 0; i < departmentInfos.size(); i++) {
row = sheet.createRow(i+1);
row.setHeight(s);
cell = row.createCell(0);
cell.setCellValue(departmentInfos.get(i).getDepartmentName());
cell.setCellStyle(cellStyle);
}
HSSFRow lastRow = sheet.createRow(departmentInfos.size());
cell = row.createCell(0);
cell.setCellValue("合计");
cell.setCellStyle(cellStyle);
List<String> yearList = calaulateYear(end);
int departmentCount=0;
for (int i = 1; i <yearList.size()+1; i++) {
row = sheet.getRow(0);
cell = row.createCell(i);
cell.setCellValue(yearList.get(i-1));
cell.setCellStyle(cellStyle);
List<DepartmentInfo> departmentInfoByMonth = null;
String year = yearList.get(i - 1);
String realYear = year.substring(0, 4);
String lastYear = end.substring(0, 4);
if(exportType.equals("1")) {
if(realYear.equals(2013)){
departmentInfoByMonth = departmentInfoDAO.countNumberByYearBefore("2013");
}if(realYear.equals(lastYear)){
departmentInfoByMonth = departmentInfoDAO.countNumberByLastYear(end);
} else {
departmentInfoByMonth = departmentInfoDAO.countNumberByYear(realYear);
}
}else {
if(realYear.equals(2013)) {
departmentInfoByMonth = departmentInfoDAO.countNumberByYearBefore2("2013");
}if(realYear.equals(lastYear)){
departmentInfoByMonth = departmentInfoDAO.countNumberByLastYear2(end);
}else {
departmentInfoByMonth = departmentInfoDAO.countNumberByYear2(realYear);
}
}
departmentCount = departmentInfoByMonth.size();
BigDecimal total = new BigDecimal(0);
for (int j = 0; j < departmentInfoByMonth.size(); j++) {
row = sheet.getRow(j + 1);
if ( row == null) {
row = sheet.createRow(j+1);
}
else {
row = sheet.getRow(j+1);
}
cell = row.createCell(i);
String numbers = departmentInfoByMonth.get(j).getNumbers();
cell.setCellValue(numbers);
cell.setCellStyle(cellStyle);
total = total.add(new BigDecimal(numbers));
}
cell = lastRow.createCell(i);
cell.setCellValue(total.toString());
cell.setCellStyle(cellStyle);
}
row = sheet.getRow(0);
cell = row.createCell(yearList.size()+1);
cell.setCellValue("合计");
cell.setCellStyle(cellStyle);
for (int j = 1; j <= departmentCount+1; j++) {
BigDecimal total = new BigDecimal(0);
for (int i = 1; i < yearList.size()+1; i++) {
row = sheet.getRow(j);
total = total.add(new BigDecimal(row.getCell(i).getStringCellValue()));
}
cell = sheet.getRow(j).createCell(yearList.size()+1);
cell.setCellValue(total.toString());
cell.setCellStyle(cellStyle);
}
OutputStream output = response.getOutputStream();
response.reset();
String fileName = URLEncoder.encode("入职年限分布表");
response.reset();
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("UTF-8");
response.addHeader("Content-Disposition",
"attachment; filename=\"" + fileName + ".xls\"");
wb.write(output);
output.close();
}