Java生成xlsx格式的excel文件

本文介绍如何使用Java批量写入xlsx格式文件的方法,包括文件和目录的创建、设置单元格字体与边框、填充数据等步骤,并提供了一个具体的代码示例。

xlsx格式的写入的数据量据说有百万级,结合实际需要该格式。

public static void main(String[] args) throws Exception {
        OutputStream outputStreamExcel = null;
        File tmpFile = new File("E:" + File.separator + "file_route" + File.separator + "detail.xlsx");
        if (!tmpFile.getParentFile().exists()) {
            tmpFile.getParentFile().mkdirs();//创建目录
        }
        if(!tmpFile.exists()) {
            tmpFile.createNewFile();//创建文件
        }
        Workbook workbook = null;
        workbook = new XSSFWorkbook();//创建Workbook对象(excel的文档对象)
        Sheet sheet1 = workbook.createSheet("Sheet1");// 建建sheet对象(excel的表单)
        // 设置单元格字体
        Font headerFont = workbook.createFont(); // 字体
        headerFont.setFontHeightInPoints((short)14);
        headerFont.setFontName("黑体");
        // 设置单元格边框及颜色
        CellStyle style = workbook.createCellStyle();
        style.setBorderBottom((short)1);
        style.setBorderLeft((short)1);
        style.setBorderRight((short)1);
        style.setBorderTop((short)1);
        style.setWrapText(true);
        
        Row row = sheet1.createRow(0);
        row.createCell(0).setCellValue("序号");
        row.createCell(1).setCellValue("编号");
        row.createCell(2).setCellValue("支付订单号");
        row.createCell(3).setCellValue("商品订单号");

        Row row1 = sheet1.createRow(1);
        row1.createCell(0).setCellValue("10001");
        row1.createCell(1).setCellValue("90001");
        row1.createCell(2).setCellValue("1000000000012");
        row1.createCell(3).setCellValue("9000000000099");

        outputStreamExcel = new FileOutputStream(tmpFile);
        workbook.write(outputStreamExcel);
        outputStreamExcel.flush();
        outputStreamExcel.close();
    }

导入相应的jar包后,直接运行即可。

转载于:https://www.cnblogs.com/jingyi17/p/7808586.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值