/**
* 导出为excel文件
*/
@RequestMapping(value = "toExcel/{formId}", method = RequestMethod.GET)
public void getExcelFile(HttpServletRequest request,
HttpServletResponse response, @PathVariable String formId) {
Map<String, Object> filters = WebUtils.getParametersStartingWith(
request, "filter__");
String result = service.getExcelData(filters);
String resultName = new File(result).getName();
this.response(response, resultName, result);
}
private void response(HttpServletResponse response, String fileName,
String filePath) {
OutputStream os = null;
try {
os = response.getOutputStream();
response.reset();
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Content-Disposition", "attachment; filename="
+ fileName);
response.setContentType("application/octet-stream; charset=utf-8");
os.write(FileUtils.readFileToByteArray(new File(filePath)));
os.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private String file_separator = SystemUtils.FILE_SEPARATOR;
private String path = SystemUtils.USER_HOME + file_separator + "***"
+ file_separator + "×××" + file_separator + "###"
+ file_separator;
private SimpleDateFormat format = new SimpleDateFormat(
"yyyy_MM_dd_HH_mm_ss");
public String getPath() {
String time = format.format(new Date());
return path + time + "form.xls";
}
public String getWardExcelData(Map<String, Object> filters) {
List<WardExcel> excelData = dao.getWardExcelData(filters);
// 创建excel
HSSFWorkbook wb = new HSSFWorkbook();
// 创建sheet
HSSFSheet sheet = wb.createSheet("*********");
// 创建一行
HSSFRow rowTitle = sheet.createRow(0);
// 创建标题栏样式
HSSFCellStyle styleTitle = wb.createCellStyle();
styleTitle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 居中
HSSFFont fontTitle = wb.createFont();
// 宋体加粗
fontTitle.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
fontTitle.setFontName("宋体");
fontTitle.setFontHeight((short) 200);
styleTitle.setFont(fontTitle);
//除了标题从第6行开始合并单元格,合并单元格从0列到3列
for(int i = 6;i<=15;i++){
sheet.addMergedRegion(new CellRangeAddress(i, i, 0, 3));
}
// 在行上创建1列
HSSFCell cellTitle = rowTitle.createCell(0);
// 列标题及样式
cellTitle.setCellValue("标题1");
cellTitle.setCellStyle(styleTitle);
// 在行上创建2列
cellTitle = rowTitle.createCell(1);
cellTitle.setCellValue("标题2");
cellTitle.setCellStyle(styleTitle);
cellTitle = rowTitle.createCell(2);
cellTitle.setCellValue("标题3");
cellTitle.setCellStyle(styleTitle);
cellTitle = rowTitle.createCell(3);
cellTitle.setCellValue("标题4");
cellTitle.setCellStyle(styleTitle);
HSSFCellStyle styleCenter = wb.createCellStyle();
styleCenter.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 居中
for (int i = 0; i < excelData.size(); i++) {
WardExcel item = excelData.get(i);
HSSFRow row = sheet.createRow(i + 1);
HSSFCell cell = row.createCell(0);
cell.setCellValue(item.getCountItem());
if(i>4){
HSSFCellStyle styleLeft = wb.createCellStyle();
styleLeft.setAlignment(HSSFCellStyle.ALIGN_LEFT);// 居左
cell.setCellStyle(styleLeft);
}else{
cell.setCellStyle(styleCenter);
}
cell = row.createCell(1);
cell.setCellValue(item.getPlace());
cell.setCellStyle(styleCenter);
cell = row.createCell(2);
cell.setCellValue(item.getSoldier());
cell.setCellStyle(styleCenter);
cell = row.createCell(3);
cell.setCellValue(item.getTotal());
cell.setCellStyle(styleCenter);
}
FileOutputStream fout = null;
String filePath = getPath();
try {
FileUtils.createFile(getPath());
fout = new FileOutputStream(filePath);
wb.write(fout);
fout.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return filePath;
}