项目实训涉及的获取数据库数据,导出生成Excel表格(使用Nutz框架)

前提要连接自己的数据库,java bean 就不写出来了

Service层:

/**
     * 导出数据到Excel
     * @param ids
     * @return
     */
    public boolean exportMeeting(Integer[] ids) {
        List list = new ArrayList();
        for (Integer id : ids) {
            list.add(dao().fetch(MeetingMain.class, id));
        }

        HSSFWorkbook  wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet("sheet");
        HSSFRow row = sheet.createRow(0);

        HSSFCell cell = row.createCell(0);
        cell.setCellValue("会议主题");
        cell = row.createCell(1);
        cell.setCellValue("负责人");
        cell = row.createCell(2);
        cell.setCellValue("会议类型");
        cell = row.createCell(3);
        cell.setCellValue("会议开始时间");
        cell = row.createCell(4);
        cell.setCellValue("会议结束时间");

        for (int i = 0; i < list.size(); i++) {
            row = sheet.createRow(i + 1);
            MeetingMain quz = (MeetingMain) list.get(i);
            row.createCell(0).setCellValue(quz.getName());
            row.createCell(1).setCellValue(quz.getCharger());
            row.createCell(2).setCellValue(quz.getType() == 0? "教研会" : (quz.getType() == 1? "班会" : "小组会议"));
            row.createCell(3).setCellValue(new SimpleDateFormat("yyyy-mm-dd HH:mm:ss").format(quz.getStartTime()));
            row.createCell(4).setCellValue(new SimpleDateFormat("yyyy-mm-dd HH:mm:ss").format(quz.getEndTime()));
        }

        try {
            FileOutputStream fos = new FileOutputStream("D:/meeting.xls");
            wb.write(fos);
            fos.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        } 
        return false;
    }

Module层:

/**
     * 从数据库获取数据生成excel
     * @param id
     * @return
     */
    @At("/teach/meeting-exportMeeting")
    public Object exportMeeting(Integer[] id) {
        return meetingService.exportMeeting(id);
    }

JS调用:

/**
 * 会议基本信息导出事件
 */
$("#grid-main-exp").click(function(){
    var id = gridMain.jqGrid('getGridParam', 'selarrrow');
    if (!checkNotNull(id)) {
        alert("请选择数据行进行操作");
        return;
    }

    $.asyn({
        url : ctx + "/teach/meeting-exportMeeting.do?id=" + id,
        success : function(result) {
            if (result) {
                alert("导出成功!");
            } else {
                alert("导出失败!");
            }

        }
    });
});

JSP页面:

<button id="grid-main-exp" class="btn btn-default btn-sm">
    <i class="fa fa-trash-o">i> 导出
button>

你可能感兴趣的:(课堂笔记)