spring+strust2+mybatis框架下的导出功能的实现

来源:互联网 发布:手机淘宝浏览单软件 编辑:程序博客网 时间:2024/06/05 02:46
1.html中:<button style="float: right" type="button" id="btnDownload"  class="btn btn-primary">导出</button>2.js中://下载按钮绑定事件$("#btnDownload").bind("click", function() {            var iframe = document.createElement("iframe");    iframe.src = "/CaiYiJia/detailPaymentOP_excelDataExport.do?";    iframe.style.display = "none";    document.body.appendChild(iframe);});3.Action:/**     * excel导出加计扣除     *     * @return     * @throws     */    public String excelDataExport() {        log.info("方法excelDataExport开始");        selectPagingAddDeduction();        // 输入流定义        InputStream in = null;        try {            // 获取对象一览的流定义            in = this.getClass().getResourceAsStream(                    "/template/additional_deduction_list.xls");            // 读入模版到工作簿中            HSSFWorkbook work = new HSSFWorkbook(in);            // 表格定义            HSSFSheet sheet = null;            // 克隆sheet            sheet = work.cloneSheet(0);            // 设置该sheet的sheetName            work.setSheetName(1, "加计扣除表");            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");            // 判断是否含有数据            for (int i = 1; i < aaData.size() + 1; i++) {                // 如果行数>8行                if (i >= 2) {                    // 从备份的表格中复制一行空行给当前表格                    copyRow(sheet, work.getSheetAt(0), i, 1);                }                if (aaData.get(i - 1).getOrgVouDate() != null) {                    sheet.getRow(i).getCell(0).setCellValue(sdf.format(aaData.get(i - 1).getOrgVouDate()));                }                sheet.getRow(i).getCell(1).setCellValue(aaData.get(i - 1).getOrgVouNo());                sheet.getRow(i).getCell(2).setCellValue(aaData.get(i - 1).getOrgVouSubject());                sheet.getRow(i).getCell(3).setCellValue(aaData.get(i - 1).getOrgVouAmount().toString());                if (aaData.get(i - 1).getWeightedDeductionFlag() == 1) {                    sheet.getRow(i).getCell(4).setCellValue("是");                }                if (aaData.get(i - 1).getWeightedDeductionFlag() == 0) {                    sheet.getRow(i).getCell(4).setCellValue("否");                }            }            // 删除第一个表格            work.removeSheetAt(0);            // 获取页面的响应信息            HttpServletResponse response = ServletActionContext.getResponse();            // 取得输出流            OutputStream os = response.getOutputStream();            response.setContentType("application/vnd.ms-excel");            response.setHeader("Content-disposition","attachment;filename="+ java.net.URLEncoder.encode("additional_deduction_list", "UTF-8")+ ".xls");            work.write(os);            os.close();            work.close();            in.close();            return null;        } catch (FileNotFoundException ex) {            ex.printStackTrace();        } catch (IOException ex) {            ex.printStackTrace();        }        log.info("方法excelDataExport结束");        return SUCCESS;    }    /*     * 复制sheet的指定行到操作表格的指定行     *     * @param sheet     *     * @param copySheet     *     * @param rowNum     *     * @param copyRowNum     */    private void copyRow(HSSFSheet sheet, HSSFSheet copySheet, int rowNum,            int copyRowNum) {        HSSFRow copyRow = copySheet.getRow(copyRowNum);        HSSFRow row = sheet.createRow(rowNum);        Iterator<Cell> iter = copyRow.cellIterator();        while (iter.hasNext()) {            HSSFCell copyCell = (HSSFCell) iter.next();            int index = copyCell.getColumnIndex();            HSSFCell cell = row.createCell(index);            cell.setCellStyle(copyCell.getCellStyle());            cell.setCellType(copyCell.getCellType());            cell.setCellValue(copyCell.getStringCellValue());        }    }4.项目中建一个文件夹放Excel模板


0 0
原创粉丝点击