SpringBoot添加freemarker+jxl下载数据库记录

来源:互联网 发布:android自带数据库 编辑:程序博客网 时间:2024/06/05 05:31

1、pom中添加jxl依赖

<dependency><groupId>net.sourceforge.jexcelapi</groupId><artifactId>jxl</artifactId><version>2.6.10</version></dependency>


2、修改之前的freemark模板,添加点击下载按钮,这里我引用了bootstarp的一些css

<!DOCTYPE html><#assign base=request.contextPath /><html><head><script language="javascript" type="text/javascript">function myclick(){location.href="/doexport";}</script><link rel="stylesheet" type="text/css"href="bootstrap/css/bootstrap.css" /><script src="jquery.js"></script><script src="bootstrap/js/bootstrap.js"></script></head><body><div class="col-md-8 col-sm-offset-2 text-center"><h1 class="text-info">查看卡卷使用情况<a class="btn btn-success"  href='javascript:myclick()'>下载记录</a></h1></div><div><table class="table table-bordered"  height="100" border="0" cellspacing="1" bgcolor="#000000"><tr bgcolor="#FFFFFF"><td>id</td><td>open_id</td><td>nickname</td><td>groupid</td><td>gameid</td><td>cardid</td><td>code</td><td>updatetime</td></tr><#list card as item><tr bgcolor="#FFFFFF"><td>${item.id}</td><td>${item.open_id}</td><td>${item.nickname}</td><td>${item.group_id?c}</td><td>${item.game_id?c}</td><td>${item.card_id}</td><td>${item.code}</td><td>${item.update_time}</td></tr></#list></table></div></body></html>

3、controller层添加导出功能

@RequestMapping("/doexport")public void doExport(HttpServletRequest req,HttpServletResponse res) throws IOException, RowsExceededException, WriteException{OutputStream os = res.getOutputStream();// 取得输出流             res.reset();// 清空输出流             res.setHeader("Content-disposition", "attachment; filename="+"WechatCard.xls");// 设定输出文件头             res.setContentType("application/msexcel");// 定义输出类型           WritableWorkbook wbook = Workbook.createWorkbook(os);                WritableSheet wsheet = wbook.createSheet("核销记录", 0);        WritableFont Titlefont = new WritableFont(WritableFont.ARIAL, 12,WritableFont.NO_BOLD,                   false,UnderlineStyle.NO_UNDERLINE,Colour.BLUE);         WritableCellFormat tilefotmat=new WritableCellFormat(Titlefont);                wsheet.addCell(new Label(0,0,"序号",tilefotmat));        wsheet.addCell(new Label(1,0,"用户Id",tilefotmat));        wsheet.addCell(new Label(2,0,"用户昵称",tilefotmat));        wsheet.addCell(new Label(3,0,"设备号",tilefotmat));        wsheet.addCell(new Label(4,0,"游戏Id",tilefotmat));        wsheet.addCell(new Label(5,0,"卡卷Id",tilefotmat));        wsheet.addCell(new Label(6,0,"卡卷码",tilefotmat));        wsheet.addCell(new Label(7,0,"核销时间",tilefotmat));                wsheet.setColumnView(0, 10);        wsheet.setColumnView(1, 30);        wsheet.setColumnView(2, 30);        wsheet.setColumnView(3, 10);        wsheet.setColumnView(4, 10);        wsheet.setColumnView(5, 30);        wsheet.setColumnView(6, 20);        wsheet.setColumnView(7, 20);                List<RecordConsumeCard> cards = consumeCardMapper.getConsumeCardInfo();                int i=1;        for(RecordConsumeCard card:cards){        wsheet.addCell(new Label(0,i,String.valueOf(card.getId())));            wsheet.addCell(new Label(1,i,card.getOpen_id()));            wsheet.addCell(new Label(2,i,card.getNickname()));            wsheet.addCell(new Label(3,i,String.valueOf(card.getGroup_id())));            wsheet.addCell(new Label(4,i,String.valueOf(card.getGame_id())));            wsheet.addCell(new Label(5,i,String.valueOf(card.getCard_id())));            wsheet.addCell(new Label(6,i,String.valueOf(card.getCode())));            wsheet.addCell(new Label(7,i,String.valueOf(card.getUpdate_time())));            i++;        }        wbook.write(); // 写入文件             wbook.close();            os.close(); // 关闭流  }


原创粉丝点击