poi 写 excel 文件

本文提供了一个使用Java导出Excel文件的示例代码。该示例创建了一个包含多个合并单元格的简单Excel表格,并详细展示了如何设置单元格类型、列宽及合并单元格等操作。

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

public static void exportOrderXLS()
            throws IOException {
        File file = new File("d:/workspace/test/test.xls");
        if(file.exists()){
            file.delete();
        }
        file.createNewFile();
        FileOutputStream out = new FileOutputStream(file);
        Workbook wb = new HSSFWorkbook();
        Sheet sheet = wb.createSheet();
        
        //sheet.autoSizeColumn(1, true);
        sheet.setColumnWidth(0, 20*256);
        sheet.setColumnWidth(1, 200*20);

        Row row = sheet.createRow(0);
        row.createCell(0, Cell.CELL_TYPE_STRING).setCellValue("1");
        row.createCell(1, Cell.CELL_TYPE_STRING).setCellValue("2");
        row.createCell(2, Cell.CELL_TYPE_STRING).setCellValue("3");
        row.createCell(3, Cell.CELL_TYPE_STRING).setCellValue("4");
        
        row = sheet.createRow(1);
        row.createCell(0, Cell.CELL_TYPE_STRING).setCellValue("1");
        row.createCell(1, Cell.CELL_TYPE_STRING).setCellValue("2");
        row.createCell(2, Cell.CELL_TYPE_STRING).setCellValue("3");
        row.createCell(3, Cell.CELL_TYPE_STRING).setCellValue("4");
        
        row = sheet.createRow(2);
        row.createCell(0, Cell.CELL_TYPE_STRING).setCellValue("1");
        row.createCell(1, Cell.CELL_TYPE_STRING).setCellValue("2");
        row.createCell(2, Cell.CELL_TYPE_STRING).setCellValue("3");
        row.createCell(3, Cell.CELL_TYPE_STRING).setCellValue("4");
        
        row = sheet.createRow(3);
        row.createCell(0, Cell.CELL_TYPE_STRING).setCellValue("1");
        row.createCell(1, Cell.CELL_TYPE_STRING).setCellValue("22");
        row.createCell(2, Cell.CELL_TYPE_STRING).setCellValue("3");
        row.createCell(3, Cell.CELL_TYPE_STRING).setCellValue("4");
        
        row = sheet.createRow(4);
        row.createCell(0, Cell.CELL_TYPE_STRING).setCellValue("1");
        row.createCell(1, Cell.CELL_TYPE_STRING).setCellValue("22");
        row.createCell(2, Cell.CELL_TYPE_STRING).setCellValue("3");
        row.createCell(3, Cell.CELL_TYPE_STRING).setCellValue("4");
        
        /**
         * addMergedRegion
         * 参数:起始行号,终止行号, 起始列号,终止列号
         */
        sheet.addMergedRegion(new CellRangeAddress(0, 4, 0, 0));
        sheet.addMergedRegion(new CellRangeAddress(0, 2, 1, 1));
        sheet.addMergedRegion(new CellRangeAddress(3, 4, 1, 1));
        
        wb.write(out);
        out.flush();
    }

113807_p3Gt_1390988.png

转载于:https://my.oschina.net/cwzhang/blog/334888

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值