如何使用java在excel中将特定单元格设置为边框形式

时间:2014-05-03 09:54:55

标签: java excel apache-poi

我可以知道如何使用java在excel中创建特定单元格作为边框形式? 我只需要特殊的桌子用桌子。 参考:我已附上屏幕截图以了解更多详情。

enter image description here

我的代码:

 try {
        int rowCount = 1;

        FileOutputStream fileOut = new FileOutputStream("poi-test.xls");

        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet();
        HSSFRow row = null;
        HSSFCell cell = null;
        HSSFFont heading_caption_font = null;
        HSSFCellStyle heading_caption_style = null;

        workbook.setSheetName(0,
                              "Ageing Report",
                              HSSFWorkbook.ENCODING_COMPRESSED_UNICODE);

        heading_caption_font = workbook.createFont();
        heading_caption_font.setFontHeightInPoints((short)12);
        heading_caption_font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        heading_caption_font.setFontName("Calibri");

        heading_caption_style = workbook.createCellStyle();
        heading_caption_style.setFont(heading_caption_font);

        row = sheet.createRow(rowCount);
        rowCount += 1;

        cell = row.createCell((short)3);

        cell.setCellValue("Aging Report on UN-acted (DETAILED)");
        cell.setCellStyle(heading_caption_style);
        sheet.addMergedRegion(new Region(1, (short)3, 1, (short)4));

        workbook.write(fileOut);
        fileOut.flush();
        fileOut.close();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

我无法获得所需的输出:

1 个答案:

答案 0 :(得分:2)

以下代码段将为您完成。

Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet("Ageing Report");
Row row = sheet.createRow(1);
Cell cell = row.createCell(3);
cell.setCellValue("Aging Report on UN-acted (DETAILED)");
CellRangeAddress region = new CellRangeAddress(1, 1, 3, 4);
RegionUtil.setBorderBottom(CellStyle.BORDER_MEDIUM, region, sheet, wb);
RegionUtil.setBorderTop(CellStyle.BORDER_MEDIUM, region, sheet, wb);
RegionUtil.setBorderRight(CellStyle.BORDER_MEDIUM, region, sheet, wb);
RegionUtil.setBorderLeft(CellStyle.BORDER_MEDIUM, region, sheet, wb);
sheet.addMergedRegion(region);