excel 表头合并 和拆分的例子

来源:互联网 发布:软件开机自启动 编辑:程序博客网 时间:2024/06/18 09:50

自己整理了一个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();    }}
原创粉丝点击