/**
* 导出Excel的操作方式
* @author liuyy
* @throws Exception
* @date 2017-1-12
*/
@Override
public Object export() throws Exception {
List<BeApproveBill> beApproveBillList = getEntityManager().namedQuery("getBeApproveBill");
List<BeApproveBill> list = new ArrayList<BeApproveBill>();
FileOutputStream out = null;
String path = WebContextHolder.getServletContext().getRealPath("/");
path = path + "\\接待清单.xls";
try {
out = new FileOutputStream(path);
} catch (FileNotFoundException e) {
String info = "导出Excel文件失败";
logger.error(info, e);
throw new RuntimeException(info, e);
}
exportData(beApproveBillList, list, out);
downloadFile(request, response, path, "接待清单.xls");
return super.export();
}
/**
* 对审批状态进行判断并且导出Excel的操作
* @param beApproveBillList
* @param list
* @param out
* @author liuyy
*/
public void exportData(List<BeApproveBill> beApproveBillList, List<BeApproveBill> list, FileOutputStream out) {
if(beApproveBillList.size() > 0) {
for(BeApproveBill beApproveBill : beApproveBillList) {
String approveStatus = beApproveBill.getApproveStatus();
if("1".equals(approveStatus) || "3".equals(approveStatus)) {
list.add(beApproveBill);
}
}
ExcelUtil<BeApproveBill> util = new ExcelUtil<BeApproveBill>(BeApproveBill.class);// 创建工具类.
util.exportExcel(list, "接待清单", 65536,out);// 导出
}
}
/**
* 下载文件的操作
* @param request
* @param response
* @param filePath 文件全路径
* @param fileName 指定客户端下载时显示的文件名
* @throws IOException
* @author liuyy
* @date 2017-1-12
*/
public void downloadFile(HttpServletRequest request,
HttpServletResponse response, String filePath, String fileName)
throws IOException {
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
bis = new BufferedInputStream(new FileInputStream(filePath));
bos = new BufferedOutputStream(response.getOutputStream());
long fileLength = new File(filePath).length();
solveGarbled(request, response, fileName, bis, bos, fileLength);
bis.close();
bos.close();
}
/**
* 解决下载的时候针对不同浏览器乱码的操作
* @param request
* @param response
* @param fileName
* @param bis
* @param bos
* @param fileLength
* @throws UnsupportedEncodingException
* @throws IOException
* @author liuyy
* @date 2017-1-12
*/
public void solveGarbled(HttpServletRequest request, HttpServletResponse response, String fileName, BufferedInputStream bis,
BufferedOutputStream bos, long fileLength) throws UnsupportedEncodingException, IOException {
response.setCharacterEncoding("UTF-8");
response.setContentType("multipart/form-data");
//解决各浏览器的中文乱码问题
String userAgent = request.getHeader("User-Agent");
byte[] bytes = userAgent.contains("MSIE") ? fileName.getBytes()
: fileName.getBytes("UTF-8"); // fileName.getBytes("UTF-8")处理safari的乱码问题
fileName = new String(bytes, "ISO-8859-1"); // 各浏览器基本都支持ISO编码
response.setHeader("Content-disposition",
String.format("attachment; filename=\"%s\"", fileName));
response.setHeader("Content-Length", String.valueOf(fileLength));
byte[] buff = new byte[2048];
int bytesRead;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
}
下面是工具类ExcelUtil和ExcelVOAttribute
package com.jtv.oaquery.module.b
利用java导出Excel文件
最新推荐文章于 2024-07-23 14:56:34 发布