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

1、pom中添加jxl依赖


	net.sourceforge.jexcelapi
	jxl
	2.6.10


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


<#assign base=request.contextPath />












查看卡卷使用情况下载记录

<#list card as item>
id open_id nickname groupid gameid cardid code updatetime
${item.id} ${item.open_id} ${item.nickname} ${item.group_id?c} ${item.game_id?c} ${item.card_id} ${item.code} ${item.update_time}

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 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(); // 关闭流  
	}


你可能感兴趣的:(java,Springboot)