poi 写 excel 文件

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();
    }

poi 写 excel 文件

你可能感兴趣的:(poi 写 excel 文件)