layui实现列表EXCEL导出

layui实现列表EXCEL导出

  1. 前台设置导出按钮。
<a href="javascript:;" class="layui-btn" id="exportExcel"><span><i class="layui-icon">&#xe601;</i>导出</span></a>
  1. 设置其click事件调用后台方法
//导出
$('#exportExcel').on('click',function(){
   window.location.href="${base}/risen/zzgl/trade/exportExcel.do?risentbUuid="+'${risentbUuid}';
  });
  1. service层实现方法
@Override
    public RisenTradeBase exportExcel(RisenTradeBase model, HttpServletResponse response) {
        List<RisenTradeBase> list = this.getRisenTradeBaseDao().findAllList(model);
        model.getObjMap().put("dataList", list);
        ExcelUtil.exportExcelTradeBase(model,response);
        return null;
    }
  1. 后台调用实现方法如下。
public class ExcelUtil {
   private static HSSFWorkbook wb = null;// 工作簿
    private static HSSFSheet sheet = null;// 工作表
    private static HSSFRow row = null;// 行
    private static HSSFCell cell = null;

    public static void init() {
        wb = new HSSFWorkbook();
        sheet = wb.createSheet("Sheet1");
    }

    //科普教育基地列表导出
    public static void exportExcelTradeBase(RisenTradeBase model, HttpServletResponse response) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String type="";
        try {
            List<RisenTradeBase> list = (List<RisenTradeBase>) model
                    .getObjMap().get("dataList");
            String fileName = "科普教育基地列表";
            init();// 初始化
            OutputStream os = response.getOutputStream(); // 取得response输出流
            // 创建Excel工作薄
            WritableWorkbook wwb = Workbook.createWorkbook(os);
            // 取得到文件的输出流
            response.reset(); // 清空输出流
            response.setContentType("application/vnd.ms-excel"); // 定义输出类型
            // 设置字体
            WritableFont wfont = new WritableFont(WritableFont.ARIAL, 18,
                    WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,
                    Colour.BLACK);
            WritableCellFormat font = new WritableCellFormat(wfont);
            font.setAlignment(Alignment.CENTRE);
            WritableSheet sheet = wwb.createSheet("科普教育基地列表", 0);
            // 第一行
            Label label = null;
            sheet.mergeCells(0, 0, 8, 0);// 合并单元格
            label = new Label(0, 0, "科普教育基地列表", font);
            sheet.addCell(label);
            wfont = new WritableFont(WritableFont.createFont("宋体"), 14,
                    WritableFont.BOLD);
            font = new WritableCellFormat(wfont);
            font.setWrap(true);// 自动换行
            font.setAlignment(Alignment.CENTRE);// 居中
            font.setVerticalAlignment(VerticalAlignment.CENTRE);
            label = new Label(0, 1, "基地名称", font);
            sheet.setColumnView(0, 30);
            sheet.addCell(label);
            label = new Label(1, 1, "上级组织", font);
            sheet.setColumnView(1, 30);
            sheet.addCell(label);
            label = new Label(2, 1, "基地类型", font);
            sheet.setColumnView(2, 30);
            sheet.addCell(label);
            label = new Label(3, 1, "通讯地址", font);
            sheet.setColumnView(3, 30);
            sheet.addCell(label);
            // 数据
            wfont = new WritableFont(WritableFont.createFont("宋体"), 12);
            font = new WritableCellFormat(wfont);
            font.setWrap(true);// 自动换行
            font.setAlignment(Alignment.CENTRE);// 居中
            font.setVerticalAlignment(VerticalAlignment.CENTRE);
            for (int i = 0; i < list.size(); i++) {
                RisenTradeBase tradeBase = list.get(i);

                label = new Label(0, (i + 2), tradeBase.getRisentbName(), font);
                sheet.addCell(label);

                label = new Label(1, (i + 2), tradeBase.getRisentbParentName(), font);
                sheet.addCell(label);

                if(!StringUtils.isEmpty(tradeBase.getRisentbType())){
                    if(tradeBase.getRisentbType().equals("1")){
                        type="普通类";
                    }else if(tradeBase.getRisentbType().equals("2")){
                        type="传媒类";
                    }
                }

                label = new Label(2, (i + 2),type, font);
                sheet.addCell(label);

                label = new Label(3, (i + 2), tradeBase.getRisentbAddr(), font);
                sheet.addCell(label);

            }

            // 设置打印区域
            sheet.getSettings().setOrientation(PageOrientation.PORTRAIT);// 设置为竖向打印
            sheet.getSettings().setPaperSize(PaperSize.A4); // 设置纸张
            sheet.getSettings().setFitHeight(297); // 打印区高度
            // 设置边距
            sheet.getSettings().setTopMargin(0.5);
            sheet.getSettings().setBottomMargin(0.3);
            sheet.getSettings().setLeftMargin(0.8);
            sheet.getSettings().setRightMargin(0.1);
            // 设置页脚
            sheet.getSettings().setFooterMargin(0.07); // 设置页脚边距(下)
            sheet.getSettings().setScaleFactor(85); // 打印缩放比例
            // 导出表格至Response流
            fileName = java.net.URLEncoder.encode(fileName, "UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename="
                    + fileName + ".xls"); // 设定输出文件
            wwb.write();
            // 关闭文件
            wwb.close();
            os.flush();
            os.close();
            response.flushBuffer();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
  1. 关于前台的部分相关可以参考layui的官网。
发布了33 篇原创文章 · 获赞 2 · 访问量 4737

猜你喜欢

转载自blog.csdn.net/qq_36778310/article/details/103116695