java生成Excel并下载(业务需求:前台点击导出按钮,后台生成Excel并下载)
导入所需要的jar包
<dependency>
<groupId>net.sourceforge.jexcelapi</groupId>
<artifactId>jxl</artifactId>
<version>2.6.12</version>
</dependency>
编写所需要的类
ServletRequestAttributes requestAttributes =
(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletResponse response = requestAttributes.getResponse();
HttpServletRequest request = requestAttributes.getRequest();
String filename = "房间列表.xls";
try {
String path = request.getSession().getServletContext().getRealPath("") + "/" + filename;
File name = new File(path);
WritableWorkbook workbook = Workbook.createWorkbook(name);
WritableSheet sheet = workbook.createSheet("房间列表", 0);
WritableFont font = new WritableFont(WritableFont.ARIAL,
14, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK);
WritableCellFormat cellFormat = new WritableCellFormat(font);
cellFormat.setBackground(Colour.WHITE);
cellFormat.setBorder(Border.ALL, BorderLineStyle.DASH_DOT);
cellFormat.setAlignment(Alignment.CENTRE);
cellFormat.setVerticalAlignment(VerticalAlignment.CENTRE);
sheet.getSettings().setDefaultColumnWidth(20);
cellFormat.setWrap(true);
Label label0 = new Label(0, 0, "房间ID", cellFormat);
Label label1 = new Label(1, 0, "房间名称", cellFormat);
Label label2 = new Label(2, 0, "房间状态", cellFormat);
Label label3 = new Label(3, 0, "房间地址", cellFormat);
sheet.addCell(label0);
sheet.addCell(label1);
sheet.addCell(label2);
sheet.addCell(label3);
WritableFont font2 = new WritableFont(WritableFont.ARIAL,
14, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK);
WritableCellFormat cellFormat2 = new WritableCellFormat(font2);
cellFormat2.setAlignment(Alignment.CENTRE);
cellFormat2.setVerticalAlignment(VerticalAlignment.CENTRE);
cellFormat2.setBackground(Colour.WHITE);
cellFormat2.setBorder(Border.ALL, BorderLineStyle.THIN);
cellFormat2.setWrap(true);
int n = 1;
List<kfzt> kfztList = kfztDao.queryByJms(userId);
if (kfztList != null && kfztList.size() > 0) {
for (kfzt kf : kfztList) {
Label lt0 = new Label(0, n, kf.getKefangID()+"", cellFormat2);
Label lt1 = new Label(1, n, kf.getMingcheng(), cellFormat2);
Label lt2 = new Label(2, n, kf.getZhuangTai(), cellFormat2);
Label lt3 = new Label(3, n, kf.getDizhi(), cellFormat2);
sheet.addCell(lt0);
sheet.addCell(lt1);
sheet.addCell(lt2);
sheet.addCell(lt3);
n++;
}
}
workbook.write();
workbook.close();
} catch (Exception e) {
e.printStackTrace();
}
OutputStream out = null;
try {
String userAgent=request.getHeader("User-Agent");
if (userAgent.contains("MSIE") || userAgent.contains("Trident") || userAgent.contains("Edge")) {
response.addHeader("content-disposition", "attachment;filename=" +
java.net.URLEncoder.encode(filename, "utf-8"));
} else {
response.addHeader("content-disposition", "attachment;filename=" +
new String(filename.getBytes("UTF-8"), "ISO8859-1"));
}
out = response.getOutputStream();
String path3 = request.getSession().getServletContext().getRealPath("") + "/" + filename;
InputStream is = new FileInputStream(path3);
byte[] b = new byte[4096];
int size = is.read(b);
while (size > 0) {
out.write(b, 0, size);
size = is.read(b);
}
out.close();
is.close();
} catch (Exception e) {
e.printStackTrace();
}