第一步:先重数据库查询出所需要的数据字段内容
@RequestMapping("/xsgzExport")
public void testdownImport(HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException{
String fileName= PubFunc.toString(request.getParameter("file"));
String zjkssj= PubFunc.toString(request.getParameter("zjkssj"));//开始时间
String zjjssj= PubFunc.toString(request.getParameter("zjjssj"));//结束时间
String shzt=PubFunc.toString(request.getParameter("shzt"));
String path1=System.getProperty("web.root");
String tempPath=path1+"upload";
System.out.println(tempPath);
String sql="SELECT c.term_acdeyear AS 学年, "
+" c.term_acdeterm AS 学期, "
+" tbl_agencyinfo.`agencyinfo_name` AS 开设部门, "
+"a.`stuwork_name` AS 岗位名称, "
+"a.`stuwork_salary` AS 岗位待遇, "
+"tbl_classinfo.`classinfo_name` AS "
+"班级名称,tbl_student.`student_sn` AS 学号, "
+"tbl_student.`student_stuname`AS 姓名, "
+"tbl_summary.`summary_date` AS 总结日期, "
+"tbl_summary.`summary_contents` AS 总结内容, "
+"tbl_shenhetab.`shenhetab_result` AS 评阅状态, "
+"tbl_shenhetab.`shenhetab_yijian` AS 评阅内容 "
+"FROM tbl_stuwork a "
+"LEFT JOIN tbl_term c ON a.term_id = c.term_id "
+"LEFT JOIN tbl_stuclassop ON a.`student_id`=tbl_stuclassop.`stuclassop_id` "
+"LEFT JOIN tbl_classinfo ON tbl_stuclassop.`classinfo_id`=tbl_classinfo.`classinfo_id` "
+"LEFT JOIN tbl_student ON tbl_student.`student_id`=a.`student_id` "
+"LEFT JOIN tbl_shenhetab ON tbl_shenhetab.`stuwork_id`=a.`stuwork_id` "
+"LEFT JOIN tbl_summary ON tbl_shenhetab.`summary_id`=tbl_summary.`summary_id` "
+"LEFT JOIN tbl_staff ON tbl_staff.`staff_id`=a.`staff_id` "
+"LEFT JOIN tbl_agencyinfo ON tbl_staff.`agencyinfo_id`=tbl_staff.`agencyinfo_id` "
+"WHERE 1=1 ";
sql +=" and stuwork_stadate >"+"'"+zjkssj+"'";
sql +=" and stuwork_enddate <"+"'"+zjjssj+"'";
if(!shzt.equals("全部")){
sql +=" and stuwork_xuegong>"+"'"+shzt+"'";
}
System.out.println("我是数据库语句:"+sql);
List<Map<String, Object>> list =utilQueryService.getBySql(sql);//查询数据库信息
System.out.println("woshi:"+list);
SimpleDateFormat startDate = new SimpleDateFormat();
startDate.format(new Date());
String url = tempPath +"\\"+ startDate + ".xls";
int re = new ExportExcel().outExcel(url,
"学年,学期,开设部门,岗位名称,岗位待遇,班级,学号,姓名,总结日期,总结内容,评阅状态,评阅内容", list);
System.out.println("re:"+re);
response.setCharacterEncoding("utf-8");
response.setContentType("application/x-download");
response.setHeader("Content-Disposition", "attachment; filename="+URLEncoder.encode(fileName, "UTF-8"));
try {
InputStream inputStream = new FileInputStream(new File(url));
OutputStream os = response.getOutputStream();
byte[] b = new byte[2048];
int length;
while ((length = inputStream.read(b)) > 0) {
os.write(b, 0, length);
}
os.close();
inputStream.close();
} catch (Exception e) {
}
}
第二步:上面那个方法调用了excel导出工具方法
public int outExcel(String path,String headerTitle,List<Map<String,Object>> dataList){
// 第一步,创建一个workbook,对应一个Excel文件
Workbook wb = null;
if(path.endsWith(".xls")){
wb = new HSSFWorkbook();
}else if(path.endsWith(".xlsx")){
wb = new XSSFWorkbook();
}else{
System.out.println("您的文档格式不正确!");
}
// 第二步,在workbook中添加一个sheet,对应Excel文件中的sheet
Sheet sheet = wb.createSheet("sheet1");
// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
Row row = sheet.createRow((int) 0);
// 第四步,创建单元格,并设置值表头 设置表头居中
CellStyle style = wb.createCellStyle();
style.setAlignment(CellStyle.ALIGN_CENTER); // 创建一个居中格式
style.setFillForegroundColor(HSSFColor.SEA_GREEN.index);
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
style.setBorderBottom(CellStyle.BORDER_THIN);
style.setBorderLeft(CellStyle.BORDER_THIN);
style.setBorderRight(CellStyle.BORDER_THIN);
style.setBorderTop(CellStyle.BORDER_THIN);
// 生成一个字体
Font font = wb.createFont();
font.setColor(HSSFColor.BLACK.index);
font.setFontHeightInPoints((short) 12);
font.setBoldweight(Font.BOLDWEIGHT_BOLD);
// 把字体应用到当前的样式
style.setFont(font);
CellStyle style1 = wb.createCellStyle();
style1.setAlignment(CellStyle.ALIGN_CENTER); // 创建一个居中格式
style1.setFillForegroundColor(HSSFColor.BLACK.index);
style1.setFillPattern(CellStyle.SOLID_FOREGROUND);
style1.setBorderBottom(CellStyle.BORDER_THIN);
style1.setBorderLeft(CellStyle.BORDER_THIN);
style1.setBorderRight(CellStyle.BORDER_THIN);
style1.setBorderTop(CellStyle.BORDER_THIN);
// 生成一个字体
Font font1 = wb.createFont();
font1.setFontHeightInPoints((short) 12);
//font1.setBoldweight(Font.BOLDWEIGHT_BOLD);
// 把字体应用到当前的样式
style1.setFont(font1);
Cell cell = null;
int count=0;
String[] header_title = headerTitle.split(",");
for (int i=0;i<header_title.length;i++){
cell = row.createCell((short) count);
cell.setCellValue(header_title[i]);
cell.setCellStyle(style);
count++;
}
// 第五步,写入实体数据 实际应用中这些数据从数据库得到,
for (int i = 0; i < dataList.size(); i++){
row = sheet.createRow((int) i+1);
Map<String,Object> dataMap = dataList.get(i);
for(int j=0;j<header_title.length;j++){
cell = row.createCell((short) j);
cell.setCellValue(filterDangerString(PubFunc.toString(dataMap.get(header_title[j].toLowerCase()))));
}
}
for (int i=0;i<header_title.length;i++){
sheet.setColumnWidth(i,header_title[i].getBytes().length*2*256);
}
// 第六步,将文件存到指定位置
try{
FileOutputStream fout = new FileOutputStream(path);
wb.write(fout);
fout.close();
}
catch (Exception e){ }
return count;
}
第三步:第二步用到第三步的方法
public String filterDangerString(String value){
if(value==null){
return null;
}
value=value.replaceAll("<","<");
value=value.replaceAll(">",">");
value=value.replaceAll("'","\'");
return value;
}