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 =