excel 表头合并 和拆分的例子

自己整理了一个excel 表头合并 和拆分的例子

import java.io.File;
import java.io.IOException;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

public class Test {

    public static void main(String[] args) throws Exception {
        String path = "e:/test.xlsx";
        writeExcel(path);
    }

    public static void writeExcel(String fileName) throws IOException,
            RowsExceededException, WriteException {
        // 构建一个工作薄
        WritableWorkbook workbook = Workbook.createWorkbook(new File(fileName));
        if (workbook == null) {
            return;
        }
        // 获取第一个sheet
        WritableSheet sheet = workbook.createSheet("sheet1", 0);
        if (sheet == null) {
            return;
        }
        WritableFont bold = new WritableFont(WritableFont.createFont("微软雅黑"),
                12, WritableFont.NO_BOLD);
        WritableCellFormat titleFormate = new WritableCellFormat(bold);// 生成一个单元格样式控制对象
        titleFormate.setBorder(jxl.format.Border.ALL,
                jxl.format.BorderLineStyle.THIN);// 设置边框
        titleFormate.setAlignment(jxl.format.Alignment.CENTRE);// 单元格中的内容水平方向居中
        titleFormate.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);// 单元格的内容垂直方向居中
        // 创建表头
        sheet.mergeCells(0, 0, 0, 1);// 单元格合并
        sheet.addCell(new Label(0, 0, "时间", titleFormate));

        sheet.mergeCells(1, 0, 4, 0);
        sheet.addCell(new Label(1, 0, "新增积分总数", titleFormate));

        sheet.addCell(new Label(1, 1, "交易", titleFormate));
        sheet.addCell(new Label(2, 1, "扫码", titleFormate));
        sheet.addCell(new Label(3, 1, "爱互动", titleFormate));
        sheet.addCell(new Label(4, 1, "合计", titleFormate));

        sheet.mergeCells(5, 0, 5, 1);
        sheet.addCell(new Label(5, 0, "消费积分总数", titleFormate));

        sheet.mergeCells(6, 0, 11, 0);
        sheet.addCell(new Label(6, 0, "剩余有效积分总数", titleFormate));

        sheet.addCell(new Label(6, 1, "店铺会员", titleFormate));
        sheet.addCell(new Label(7, 1, "中级会员", titleFormate));
        sheet.addCell(new Label(8, 1, "高级会员", titleFormate));
        sheet.addCell(new Label(9, 1, "VIP会员", titleFormate));
        sheet.addCell(new Label(10, 1, "SVIP会员", titleFormate));
        sheet.addCell(new Label(11, 1, "合计", titleFormate));

        sheet.mergeCells(12, 0, 12, 1);
        sheet.addCell(new Label(12, 0, "过期积分数", titleFormate));

        WritableCellFormat bodyFormate = new WritableCellFormat();// 生成一个单元格样式控制对象
        bodyFormate.setBorder(jxl.format.Border.ALL,
                jxl.format.BorderLineStyle.THIN);// 设置边框

        for(int i = 2; i <= 12; i++){
            sheet.addCell(new Label(0, i, "2017-04-08", bodyFormate));
            sheet.addCell(new Label(0, i, "0", bodyFormate));
            sheet.addCell(new Label(0, i, "0", bodyFormate));
            sheet.addCell(new Label(0, i, "0", bodyFormate));
            sheet.addCell(new Label(0, i, "0", bodyFormate));
            sheet.addCell(new Label(0, i, "0", bodyFormate));
            sheet.addCell(new Label(0, i, "0", bodyFormate));
            sheet.addCell(new Label(0, i, "0", bodyFormate));
            sheet.addCell(new Label(0, i, "0", bodyFormate));
            sheet.addCell(new Label(0, i, "0", bodyFormate));
            sheet.addCell(new Label(0, i, "0", bodyFormate));
            sheet.addCell(new Label(0, i, "0", bodyFormate));
        }

        // 设置单元格的宽度
        sheet.setColumnView(5, 20);
        sheet.setColumnView(6, 11);
        sheet.setColumnView(7, 11);
        sheet.setColumnView(8, 11);
        sheet.setColumnView(9, 11);
        sheet.setColumnView(10, 11);
        sheet.setColumnView(11, 11);
        sheet.setColumnView(12, 20);

        // 从内存中写入文件中
        workbook.write();
        // 关闭资源,释放内存
        workbook.close();
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值