利用jxl进行对excel导出

本文详细介绍了如何使用jxl库将数据导出至Excel文件。通过示例代码展示了如何创建工作簿、工作表,设置表头及样式,以及如何将数据填充到指定的单元格中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.poi和jxl都可以进行对excel的导出,但两者的差别也是很明显的,读者可以细查

以下介绍jxl到处excel的方式

1.1无表头的方式 需要 OutputStream,List集合注意行和列都是从0开始

public static void createExcel(OutputStream os,
            List<Object> object) throws WriteException, IOException {
        String FirstSheet = "人员信息";
        // 创建工作薄
        WritableWorkbook workbook = Workbook.createWorkbook(os);
        // 创建新的一页 命名
        WritableSheet sheet = workbook.createSheet(FirstSheet, 0);
        // 构造表头
        WritableFont color = new WritableFont(WritableFont.createFont("宋体"), 12);// 选择字体
        // color.setColour(Colour.BLACK);// 设置字体颜色为黑色
        WritableCellFormat titleFormate1 = new WritableCellFormat(color);
        titleFormate1.setAlignment(jxl.format.Alignment.LEFT);// 单元格中的内容水平方向居左
        titleFormate1.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);// 单元格的内容垂直方向居中
        titleFormate1.setWrap(true);
        //设置表头
        Label XLH = new Label(0, 0, "编号", titleFormate1);
        //设置工作表指定列的宽度。这导致Excel调整整个列。如果已经指定的列视图相关联的信息,然后取而代之的是新的数据
        //第一个参数是列,第二个参数是宽度
        sheet.setColumnView(0, 9);
          //添加单元格到工作表中
        sheet.addCell(XLH);
        
        Label BH = new Label(1, 0, "姓名", titleFormate1);
        sheet.setColumnView(1, 12);
        sheet.addCell(BH);
        Label KS = new Label(2, 0, "出生年月", titleFormate1);
        sheet.setColumnView(2, 16);
        sheet.addCell(KS);
        Label FSSJ = new Label(3, 0, "初始学校", titleFormate1);
        sheet.setColumnView(3, 25);
        sheet.addCell(FSSJ);

        sheet.setRowView(1, 600, false);// 设置第二行的高度
        for (int i = 0; i < object.size(); i++) {
            Object object1= object.get(i);
            int rowIndex = i + 1;
            sheet.addCell(new Label(0, rowIndex, i + 1 + "", titleFormate1));
            sheet.addCell(new Label(1, rowIndex, object1.Name,titleFormate1));
            sheet.addCell(new Label(2, rowIndex, object1.phoneNumber, titleFormate1));
            sheet.addCell(new Label(3, rowIndex, object1.phoneNumber, titleFormate1));
            
        
        }
        // 把创建的内容写入到输出流中,并关闭输出流
        workbook.write();
        workbook.close();
        os.close();
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值