java原生Excel单元格合并自定义导出

1.简单案例

        //初始化
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFCellStyle style = workbook.createCellStyle();
        style.setAlignment(HorizontalAlignment.CENTER);
        style.setVerticalAlignment(VerticalAlignment.CENTER);

        //创建sheet
        HSSFSheet sheet = workbook.createSheet("sheet");

        /** 创建第0行 **/
        HSSFRow row0 = sheet.createRow(0);

        //第一行抬头第1列
        HSSFCell cell_00 = row0.createCell(0);
        cell_00.setCellStyle(style);
        cell_00.setCellValue("日期");

        //第一行抬头第2列
        HSSFCell cell_01 = row0.createCell(1);
        cell_01.setCellStyle(style);
        cell_01.setCellValue("午别");

        //第一行抬头第3列
        HSSFCell cc = row0.createCell(2);
        cc.setCellStyle(style);
        cc.setCellValue("hhh");

        /** 创建第1行 **/
        HSSFRow row1 = sheet.createRow(1);
        //第二行抬头第1列
        HSSFCell cell_10 = row1.createCell(0);
        cell_10.setCellStyle(style);
        cell_10.setCellValue("20180412");

        //第二行抬头第二列
        HSSFCell cell_11 = row1.createCell(1);
        cell_11.setCellStyle(style);
        cell_11.setCellValue("上午");

        //第二行抬头第三列
        HSSFCell cell_2 = row1.createCell(2);
        cell_2.setCellStyle(style);
        cell_2.setCellValue("上午");


        /** 创建第3行 **/
        HSSFRow row2 = sheet.createRow(2);
        //第三行抬头第二列
        HSSFCell cell_21 = row2.createCell(1);
        cell_21.setCellStyle(style);
        cell_21.setCellValue("下午");

        /** 创建第4行 **/
        HSSFRow row3 = sheet.createRow(3);
        //第四行抬头第二列
        HSSFCell cell_23 = row3.createCell(1);
        cell_23.setCellStyle(style);
        cell_23.setCellValue("测试哦");

        // 合并日期占两行(4个参数,分别为起始行,结束行,起始列,结束列)
        // 行和列都是从0开始计数,且起始结束都会合并
        // 这里是合并excel中日期的两行为一行   合并第一行-第三行
        CellRangeAddress region = new CellRangeAddress(1, 3, 0, 0);
        sheet.addMergedRegion(region);

        File file = new File("E:\\demo.xls");
        FileOutputStream fout = null;
        try {
            fout = new FileOutputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        try {
            workbook.write(fout);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                fout.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

1.1 导出文件路径用户自定义
 


        // 获取输出流(用于文件最后导出自定位置使用)
        OutputStream os = response.getOutputStream();
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-disposition", "attachment; filename=" + 
        System.currentTimeMillis()+".xls");
        response.setHeader("filename",System.currentTimeMillis()+".xls");
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "No-cache");
        response.setDateHeader("Expires", 0);
        workbook.write(os);
        os.close();
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值