自己记录下来的东西,防止自己忘记!
导出excel
List<ILoginLogVoModel> list = loginLogService.exportAll(model);
Map<String, List<Map<String, Object>>> excelMap = new HashMap<String, List<Map<String, Object>>>();
// sheet
List<Map<String, Object>> sheetList = new ArrayList<Map<String, Object>>();
// row 行数
for (int j = 0; j < list.size(); j++) {
Map<String, Object> hashMap = new HashMap<String, Object>();
ILoginLogVoModel vo = list.get(j);
// (导出的每个字段没有对应)cell 每行中每列的值 注意列字不能少
hashMap.put("列" + 0, ActionHelper.toStr(j+1)); //序号
hashMap.put("列" + 1, vo.getOpt_no());
hashMap.put("列" + 2, vo.getOpt_name());
hashMap.put("列" + 3, vo.getLogin_ip());
hashMap.put("列" + 4, vo.getBrowser());
hashMap.put("列" + 5, vo.getOs());
hashMap.put("列" + 6, vo.getLogin_mac());
hashMap.put("列" + 7, vo.getLogin_time());
hashMap.put("列" + 8, vo.getOut_time());
hashMap.put("列" + 9, vo.getLogin_statu());
sheetList.add(hashMap);
}
excelMap.put("登录日志管理导出", sheetList);
// 正常表头 注意列字不能少
LinkedHashMap<String, String> head = new LinkedHashMap<String, String>();
head.put("列" + 0, "序号");
head.put("列" + 1, "登录账号");
head.put("列" + 2, "用户名");
head.put("列" + 3, "IP地址");
head.put("列" + 4, "浏览器");
head.put("列" + 5, "操作系统");
head.put("列" + 6, "主机名");
head.put("列" + 7, "登录时间");
head.put("列" + 8, "退出时间");
head.put("列" + 9, "登录状态");
// 设置每个sheet页的表头
List<LinkedHashMap<String, String>> sheetHead = new ArrayList<LinkedHashMap<String, String>>();
sheetHead.add(head);
// 设置多sheet页
Map<String, List<LinkedHashMap<String, String>>> map = new HashMap<String, List<LinkedHashMap<String, String>>>();
map.put("登录日志管理导出", sheetHead); //对应上面excelMap put的情况报表
ExcelWrite write = new ExcelWrite();
//设置response 这样就可以前台弹出框进行下载了
res.setContentType("application/msexcel");
res.setHeader("Content-disposition", "attachment; filename=登录日志"+new Date().toString()+".xls");
write.setResponse(res);
write.setSheetHead(map);
write.setMergeCell(true);
write.writeExcel(excelMap);
return null;
js:
function importInfo(){
$('#queryForm').attr("action","<%=basePath%>/SYS/LoginLog/exportPage.do"); //设置action指向导出
$('#queryForm').submit();
$('#queryForm').attr("action","<%=basePath%>/SYS/LoginLog/queryLoginLog.do"); //设置action执向查询
}
导出word
String realpath = request.getRealPath("")+"/download/商业说明会邀请函模版.doc";
//String realpath = "C:\\Users\\newtouch\\Desktop\\商业说明会邀请函模版.doc";
try {
FileInputStream fis = new FileInputStream(new File(realpath));
System.out.println(fis.available());
HWPFDocument hdt = new HWPFDocument(fis);
// 替换读取到的word模板内容的指定字段
Range range = hdt.getRange();
for (Map.Entry<String, String> entry : map.entrySet()) {
range.replaceText(entry.getKey(), entry.getValue());
}
//保存
HttpServletResponse hresponse = (HttpServletResponse) response;
hresponse.setContentType("application/octet-stream");
hresponse.setHeader("Content-Disposition", "attachment;filename="
+ new String(
("商业说明会邀请函"+DateTools.getJoinedSysDateTime() + ".doc")
.getBytes(), "ISO8859-1"));
OutputStream os = hresponse.getOutputStream();
hdt.write(os);
os.flush();
os.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
}
导出pdf
IEA_PrepareDeptVOModel ea_PrepareDeptVOModel=new EA_PrepareDeptVOModel();
ea_PrepareDeptVOModel.setCand_code(req.getParameter("cand_code"));
ea_PrepareDeptVOModel=deplicenseDao.queryDeppdf(ea_PrepareDeptVOModel);
List<IEA_PrepareDeptVOModel> list =new ArrayList<IEA_PrepareDeptVOModel>();
list.add(ea_PrepareDeptVOModel);
ExportPDF pdf = new ExportPDF();
HttpServletResponse hresponse = (HttpServletResponse) res;
hresponse.setContentType("application/mspdf");
hresponse.setHeader("Content-disposition", "attachment; filename="
+ DateTools.getJoinedSysDateTime() + "export.pdf");
OutputStream os = hresponse.getOutputStream();
pdf.doExportPdf(os, list);
os.flush();
os.close();
os = null;
下载
//设置请求编码格式
request.setCharacterEncoding("UTF-8");
//获得请求文件名
String demodownloadPath = StaticProperties.getProperty("demodownloadPath"); //模板保存路径
String downloadPath=new String(demodownloadPath.getBytes("ISO-8859-1"),"UTF-8");//转码
String filename = downloadPath.trim();
filename=filename.substring(filename.lastIndexOf("/")+1);
String downloadPath2=StaticProperties.getProperty("downloadPath");
String downloadPath3=new String(downloadPath2.getBytes("ISO-8859-1"),"UTF-8");
//设置文件MIME类型
response.setContentType(servletContext.getMimeType(filename));
//设置Content-Disposition
response.setHeader("Content-Disposition", "attachment;filename="+ java.net.URLEncoder.encode(filename, "UTF-8"));
//读取目标文件,通过response将目标文件写到客户端
//读取文件
InputStream in=null;
OutputStream out=null;
try {
in = new FileInputStream(downloadPath3+filename);
out = response.getOutputStream();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//写文件
int b;
while((b=in.read())!= -1)
{
out.write(b);
}
in.close();
out.close();