jxl将数据库表内容导出到excel

本文介绍了一种使用Java和POI库将数据库中的“purchaseApplyItem”表内容导出到Excel文件的方法。代码实现了创建Excel文件、设置样式、填充表头及数据的功能。

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

数据库表 purchaseApplyItem:

purchaseApplyItem

导出代码:

public void dump2Excel() {
    String filePathStr = "C:\\a\\b";
    String fileNameStr = "purchaseApplyItems.xls";
    File filePath = new File(filePathStr);
    File file = new File(filePathStr + "\\" + fileNameStr);
    WritableWorkbook wwb = null;
    try {
        if (!filePath.isDirectory()) {
            filePath.mkdirs();
        }
        if (!file.exists()) {
            file.createNewFile();
        } else {
            System.out.println("该文件已存在,请先删除后再生成!");
            return;
        }
        wwb = Workbook.createWorkbook(file);
        WritableSheet ws = wwb.createSheet("采购申请单", 0);
        ws.setRowView(0, 600, false); // 设置行高
        ws.setColumnView(0, 20);// 设置列宽
        // 从数据库获取所有的数据(根据自己的情况写对应的方法)
        List<PurchaseApplyItem> purchaseApplyItems = getEntity(new PurchaseApplyItem());
        // 设置字体
        // true表示是否斜体
        WritableFont wf = new WritableFont(WritableFont.createFont("宋体"), 12, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE, Colour.RED);
        // 设置格式
        WritableCellFormat wcf = new WritableCellFormat(wf);
        wcf.setBackground(jxl.format.Colour.YELLOW);
        wcf.setBorder(Border.BOTTOM, BorderLineStyle.THICK, Colour.RED);
        wcf.setAlignment(Alignment.CENTRE);
        wcf.setWrap(true);
        Label labelId = new Label(0, 0, "编号", wcf);
        Label labelItemName = new Label(1, 0, "物品名", wcf);
        Label labelNumber = new Label(2, 0, "数量", wcf);
        Label labelPurchaseId = new Label(3, 0, "采购单号", wcf);
        wcf.setVerticalAlignment(VerticalAlignment.TOP);
        // 添加表头
        ws.addCell(labelId);
        ws.addCell(labelItemName);
        ws.addCell(labelNumber);
        ws.addCell(labelPurchaseId);
        // 添加表数据
        for (int i = 0; i < purchaseApplyItems.size(); i++) {
            Number labelId_i = new Number(0, i + 1, purchaseApplyItems.get(i).getId());
            Label labelItemName_i = new Label(1, i + 1, purchaseApplyItems.get(i).getItemName());
            Number labelNumber_i = new Number(2, i + 1, purchaseApplyItems.get(i).getNumber());
            Label labelPurchaseId_i = new Label(3, i + 1, purchaseApplyItems.get(i).getPurchaseId());
            ws.addCell(labelId_i);
            ws.addCell(labelItemName_i);
            ws.addCell(labelNumber_i);
            ws.addCell(labelPurchaseId_i);
        }
        wwb.write();
        wwb.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

参看: poi将数据库表内容导出到excel
http://blog.youkuaiyun.com/chen270310978/article/details/52885410

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值