Excel导出实列

1、html调用

<td><button type="button" onclick="exportlist()"><span class="icon_export">导出</span></button></td>

function exportlist() {
window.location.href=basePath+"Security/Risk/export_all?unit_id="+encodeURI(unit_id)+"&flag="+encodeURI($("#flag").val())+"&personnel_name="+encodeURI($("#personnel_name").val());
}

2、Controller

@ResponseBody
@RequestMapping("export_all")
public String export_all(HttpServletRequest request, HttpServletResponse response, RiskPersonnelEntity entity) {
    return riskService.export(request,response,entity);
}

3、server处理

单行表头

@Override
    public String export(HttpServletRequest request, HttpServletResponse response, RiskPersonnelEntity entity) {
        // 创建excel
        HSSFWorkbook hssf = new HSSFWorkbook();
        // 样式表
        HSSFCellStyle style = hssf.createCellStyle();
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
        style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直居中
        style.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 下边框
        style.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左边框
        style.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上边框
        style.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右边框
        style.setWrapText(true);// 回绕文本

        // 表头样式
        HSSFCellStyle style3 = hssf.createCellStyle();
        style3.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
        style3.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直居中
        style3.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 下边框
        style3.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左边框
        style3.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上边框
        style3.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右边框
        style3.setWrapText(true);// 回绕文本
        HSSFFont font1 = hssf.createFont();
        font1.setFontName("黑体");
        font1.setFontHeightInPoints((short) 11);
        font1.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 粗体显示
        style3.setFont(font1);

        // 按日期创建工作表
        HSSFSheet sheet = hssf.createSheet("数据列");
        sheet.setColumnWidth(0, 1800);
        sheet.setColumnWidth(1, 3500);
        sheet.setColumnWidth(2, 3500);
        sheet.setColumnWidth(3, 3500);
        sheet.setColumnWidth(4, 3500);
        sheet.setColumnWidth(5, 3500);

        HSSFRow row = sheet.createRow(0);
        HSSFRow row1 = sheet.createRow(1);
        HSSFCell cell = row.createCell(0);
        row.setHeight((short) (15.625 * 30));
        row1.setHeight((short) (15.625 * 30));

        setCell(hssf, sheet, row, cell, style3, 0, 0, 0, 0, "序号");
        setCell(hssf, sheet, row, cell, style3, 0, 0, 1, 1, "单位");
        setCell(hssf, sheet, row, cell, style3, 0, 0, 2, 2, "姓名");

        List<RiskPersonnelEntity> list=riskPersonnelDao.selectByParam(entity);
        int i=1;
        for(RiskPersonnelEntity obj : list){
            HSSFRow rows = sheet.createRow(i);
            rows.setHeight((short) (15.625 * 25));
            setCell(hssf, sheet, rows, cell, style, i, i, 0, 0, i + "");
            setCell(hssf, sheet, rows, cell, style, i, i, 1, 1, obj.getUnit_name());
            setCell(hssf, sheet, rows, cell, style, i, i, 2, 2, obj.getPersonnel_name());
            i++;
        }
// 初始化封装的excel实体
        workbook = new WorkbookEntity();
        workbook.setFileName("人员信息");
        workbook.setWorkbook(hssf);
        OutputStream out=null;
        String excelName=null;
        try {
            out = response.getOutputStream();
            response.setContentType("application/msexcel;charset=utf-8");
            excelName = new String(workbook.getFileName().getBytes(),"iso8859-1");
            response.setHeader("Content-disposition", "attachment;filename="+excelName+".xls");
            workbook.getWorkbook().write(out);
            out.flush();
            out.close();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        return "workbook";
    }
private void setCell(HSSFWorkbook hssf, HSSFSheet sheet, HSSFRow row, HSSFCell cell, int a, int b, int c, int d, String string) {
    cell = row.createCell(c);
    cell.setCellValue(string);
    if (a != b || c != d) {
        CellRangeAddress cra = new CellRangeAddress(a, b, c, d);
        sheet.addMergedRegion(cra);
    }
}

private void setCell(HSSFWorkbook hssf, HSSFSheet sheet, HSSFRow row, HSSFCell cell, HSSFCellStyle style, int a, int b, int c, int d, HSSFRichTextString hr) {
    cell = row.createCell(c);
    cell.setCellValue(hr);
    cell.setCellStyle(style);
    if (a != b || c != d) {
        CellRangeAddress cra = new CellRangeAddress(a, b, c, d);
        sheet.addMergedRegion(cra);
        RegionUtil.setBorderBottom(1, cra, sheet, hssf);
        RegionUtil.setBorderLeft(1, cra, sheet, hssf);
        RegionUtil.setBorderRight(1, cra, sheet, hssf);
        RegionUtil.setBorderTop(1, cra, sheet, hssf);
    }
}

public void setCell(HSSFWorkbook hssf, HSSFSheet sheet, HSSFRow row, HSSFCell cell, HSSFCellStyle style, int a, int b, int c, int d, String val) {
    cell = row.createCell(c);
    cell.setCellValue(val);
    cell.setCellStyle(style);
    if (a != b || c != d) {
        CellRangeAddress cra = new CellRangeAddress(a, b, c, d);
        sheet.addMergedRegion(cra);
        RegionUtil.setBorderBottom(1, cra, sheet, hssf);
        RegionUtil.setBorderLeft(1, cra, sheet, hssf);
        RegionUtil.setBorderRight(1, cra, sheet, hssf);
        RegionUtil.setBorderTop(1, cra, sheet, hssf);
    }
}

public void setCell(HSSFWorkbook hssf, HSSFSheet sheet, HSSFRow row, HSSFCell cell, HSSFCellStyle style, int a, int b, int c, int d, double val) {
    cell = row.createCell(c);
    cell.setCellValue(val);
    cell.setCellStyle(style);
    if (a != b || c != d) {
        CellRangeAddress cra = new CellRangeAddress(a, b, c, d);
        sheet.addMergedRegion(cra);
        RegionUtil.setBorderBottom(1, cra, sheet, hssf);
        RegionUtil.setBorderLeft(1, cra, sheet, hssf);
        RegionUtil.setBorderRight(1, cra, sheet, hssf);
        RegionUtil.setBorderTop(1, cra, sheet, hssf);
    }
}

多行表头

@Override
    public String Export(HttpServletRequest request, HttpServletResponse response, Equipment_WzdbEntity entity) {
        //查询主计划数据
        String dbid=null,db_date=null,out_unit=null,enter_unit=null,examine1=null,examine2=null,examine4=null;
        HttpSession session=request.getSession();
        String oilfield = (String) session.getAttribute("oilfield");
        entity.setOilfield(oilfield);
        Equipment_WzdbEntity main = SbdbDao.Get_main(entity).get(0);//list
        dbid = main.getDbid();
        db_date = main.getDb_date();
        out_unit = main.getOut_unit();
        enter_unit = main.getEnter_unit();
        examine1 = main.getExamine1();
        examine2 = main.getExamine2();
        examine4 = main.getExamine4();
        /** 第一步,创建一个Workbook,对应一个Excel文件  */
        HSSFWorkbook hssf = new HSSFWorkbook();
        /** 第二步,在Workbook中添加一个sheet,对应Excel文件中的sheet  */
        HSSFSheet sheet = hssf.createSheet("调拨单导出");
        /** 第三步,设置样式以及字体样式*/
        //标题样式1
        HSSFCellStyle titleStyle1 = hssf.createCellStyle();
        titleStyle1.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
        titleStyle1.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直居中
        HSSFFont headerFont1 = (HSSFFont) hssf.createFont(); // 创建字体样式
        headerFont1.setFontName("黑体"); // 设置字体类型
        headerFont1.setFontHeightInPoints((short) 14); // 设置字体大小
        titleStyle1.setFont(headerFont1); // 为标题样式设置字体样式
        //标题样式2
        HSSFCellStyle titleStyle2 = hssf.createCellStyle();
        titleStyle2.setAlignment(titleStyle2.ALIGN_CENTER);//水平居中
        titleStyle2.setVerticalAlignment(titleStyle2.VERTICAL_CENTER);//垂直对齐
        HSSFFont headerFont2 = (HSSFFont) hssf.createFont(); // 创建字体样式
        headerFont2.setBold(true); //字体加粗
        headerFont2.setFontName("黑体"); // 设置字体类型
        headerFont2.setFontHeightInPoints((short) 17); // 设置字体大小
        titleStyle2.setFont(headerFont2); // 为标题样式设置字体样式
        //标题样式3
        HSSFCellStyle titleStyle3 = hssf.createCellStyle();
        HSSFFont headerFont3 = (HSSFFont) hssf.createFont(); // 创建字体样式
        headerFont3.setFontName("黑体"); // 设置字体类型
        headerFont3.setFontHeightInPoints((short) 11); // 设置字体大小
        titleStyle3.setFont(headerFont3); // 为标题样式设置字体样式
        // 表头样式
        HSSFCellStyle headerStyle = hssf.createCellStyle();
        headerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
        headerStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直居中
        headerStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 下边框
        headerStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左边框
        headerStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上边框
        headerStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右边框
        headerStyle.setWrapText(true);// 回绕文本
        HSSFFont font1 = hssf.createFont();
        font1.setFontName("黑体");
        font1.setFontHeightInPoints((short) 11);
        font1.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 粗体显示
        headerStyle.setFont(font1);
        //内容样式
        HSSFCellStyle contentStyle = hssf.createCellStyle();
        contentStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
        contentStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直居中
        contentStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 下边框
        contentStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左边框
        contentStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上边框
        contentStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右边框
        contentStyle.setWrapText(true);// 回绕文本

        //底部样式
        HSSFCellStyle footStyle = hssf.createCellStyle();
        footStyle.setAlignment(HSSFCellStyle.ALIGN_LEFT); // 创建一个居中格式
        footStyle.setVerticalAlignment(HSSFCellStyle.ALIGN_LEFT);// 垂直居中
        footStyle.setBorderBottom(HSSFCellStyle.BORDER_NONE); // 下边框
        footStyle.setBorderLeft(HSSFCellStyle.BORDER_NONE);// 左边框
        footStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上边框
        footStyle.setBorderRight(HSSFCellStyle.BORDER_NONE);// 右边框
        footStyle.setWrapText(true);// 回绕文本

        sheet.setColumnWidth(0, 1800);
        sheet.setColumnWidth(1, 4000);
        sheet.setColumnWidth(2, 4000);
        sheet.setColumnWidth(3, 3500);
        sheet.setColumnWidth(4, 3500);
        sheet.setColumnWidth(5, 3500);
        sheet.setColumnWidth(6, 3500);
        sheet.setColumnWidth(7, 3500);
        sheet.setColumnWidth(8, 3500);
        sheet.setColumnWidth(9, 3500);

        // 行号
        int rowNum = 0;
        int sb_number=0;
        //标题行1
        HSSFRow title1 = sheet.createRow(rowNum++);
        title1.setHeight((short) 500);// 设置行高
        HSSFCell c00 = title1.createCell(0);
        c00.setCellValue("中国石油冀东油田");
        c00.setCellStyle(titleStyle1);
        // 合并单元格,参数依次为起始行,结束行,起始列,结束列 (索引0开始)
        sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 9));//标题合并单元格操作,6为总列数
        //标题行2
        HSSFRow title2 = sheet.createRow(rowNum++);
        title2.setHeight((short) 600);// 设置行高
        HSSFCell c01 = title2.createCell(0);
        c01.setCellValue("固定资产内部调拨单");
        c01.setCellStyle(titleStyle2);
        // 合并单元格,参数依次为起始行,结束行,起始列,结束列 (索引0开始)
        sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, 9));//标题合并单元格操作,6为总列数
        //标题行3
        HSSFRow title3 = sheet.createRow(rowNum++);
        title3.setHeight((short) 300);// 设置行高
        String[] row_title = {"","","","","","","","","",""};
        for (int ii = 0; ii < row_title.length; ii++) {
            HSSFCell c02 = title3.createCell(ii);
            c02.setCellStyle(titleStyle3);
            if (ii == 0) {
                c02.setCellValue("调出单位:"+out_unit);
            } else if (ii == 3) {
                c02.setCellValue( "调字第("+dbid+")号");
            } else if (ii == 6) {
                c02.setCellValue(" 调入单位:"+enter_unit);
            } else if (ii == 9) {
                c02.setCellValue("调拨日期:"+db_date);
            }
        }
        sheet.addMergedRegion(new CellRangeAddress(2, 2, 0, 2));
        sheet.addMergedRegion(new CellRangeAddress(2, 2, 3, 5));
        sheet.addMergedRegion(new CellRangeAddress(2, 2, 6, 8));
        sheet.addMergedRegion(new CellRangeAddress(2, 2, 9, 9));
        //表头行
        HSSFRow row1 = sheet.createRow(rowNum++);
        row1.setHeight((short) 500);
        String[] row_first = {"序号", "资产编码", "资产名称", "规格型号", " 单位 ", "数量","原值","净值","调拨原因","备注"};
        for (int i = 0; i < row_first.length; i++) {
            HSSFCell tempCell = row1.createCell(i);
            tempCell.setCellStyle(headerStyle);
            tempCell.setCellValue(row_first[i]);
        }

        List<Equipment_WzdbEntity> list=SbdbDao.Get_detailed(entity);
        int i=1;
        for(Equipment_WzdbEntity obj : list){
            sb_number+=obj.getSb_number();
            HSSFRow rows = sheet.createRow(rowNum++);
            HSSFCell cell = rows.createCell(0);
            rows.setHeight((short) (15.625 * 25));
            setCell(hssf, sheet, rows, cell, contentStyle, i, i, 0, 0, i);
            setCell(hssf, sheet, rows, cell, contentStyle, i, i, 1, 1, obj.getAsset_number());
            setCell(hssf, sheet, rows, cell, contentStyle, i, i, 2, 2, obj.getSbmc());
            setCell(hssf, sheet, rows, cell, contentStyle, i, i, 3, 3, obj.getGgxh());
            setCell(hssf, sheet, rows, cell, contentStyle, i, i, 4, 4, obj.getUnit());
            setCell(hssf, sheet, rows, cell, contentStyle, i, i, 5, 5, obj.getSb_number());
            setCell(hssf, sheet, rows, cell, contentStyle, i, i, 6, 6, obj.getYz());
            setCell(hssf, sheet, rows, cell, contentStyle, i, i, 7, 7, obj.getJz());
            setCell(hssf, sheet, rows, cell, contentStyle, i, i, 8, 8, obj.getDbyy());
            setCell(hssf, sheet, rows, cell, contentStyle, i, i, 9, 9, obj.getRemark());
            i++;
        }
        //合计行
        HSSFRow sum = sheet.createRow(rowNum++);
        sum.setHeight((short) 500);
        String[] row_sum = {"","","","","","","","","",""};
        for (int ii = 0; ii < row_sum.length; ii++) {
            HSSFCell tempCell = sum.createCell(ii);
            tempCell.setCellStyle(contentStyle);
            if (ii == 1) {
                tempCell.setCellValue("合计:");
            } else if (ii == 5) {
                tempCell.setCellValue(sb_number);
            } else {
                tempCell.setCellValue(row_sum[ii]);
            }
        }
        // 尾行
        HSSFRow foot = sheet.createRow(rowNum++);
        foot.setHeight((short) 500);
        String[] row_foot = {"公司资产管理部门签章:","","","调入单位签章:","","调出单位签章:","",""," 调出单位制单:",""};
        for (int ii = 0; ii < row_foot.length; ii++) {
            HSSFCell tempCell = foot.createCell(ii);
            tempCell.setCellStyle(footStyle);
            if (ii == 0) {
                tempCell.setCellValue(row_foot[ii] + isnull(examine1));
            } else if (ii == 3) {
                tempCell.setCellValue(row_foot[ii] + isnull(examine4));
            } else if (ii == 5) {
                tempCell.setCellValue(row_foot[ii] + isnull(examine2));
            } else if (ii == 8) {
                tempCell.setCellValue(row_foot[ii]);
            } else {
                tempCell.setCellValue(row_foot[ii]);
            }
        }
        int footRowNum=list.size()+5;
        sheet.addMergedRegion(new CellRangeAddress(footRowNum, footRowNum, 0, 2));
        sheet.addMergedRegion(new CellRangeAddress(footRowNum, footRowNum, 3, 4));
        sheet.addMergedRegion(new CellRangeAddress(footRowNum, footRowNum, 5, 7));
        sheet.addMergedRegion(new CellRangeAddress(footRowNum, footRowNum, 8, 9));
// 初始化封装的excel实体
        workbook = new WorkbookEntity();
        workbook.setFileName("设备调拨单");
        workbook.setWorkbook(hssf);
        OutputStream out=null;
        String excelName=null;
        try {
            out = response.getOutputStream();
            response.setContentType("application/msexcel;charset=utf-8");
            excelName = new String(workbook.getFileName().getBytes(),"iso8859-1");
            response.setHeader("Content-disposition", "attachment;filename="+excelName+".xls");
            workbook.getWorkbook().write(out);
            out.flush();
            out.close();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        return "workbook";
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值