如何使用 Apache POI 4.0.1 和 java 通过在整个数据的每一侧留下一个单元格空

  • 目前,我可以在整个数据旁边设置边框(您可以参考下图)。

电流输出

http://img4.mukewang.com/62d7b17e0001f13d04070162.jpg

代码片段


  // Code to draw Border at left side

    int rowstart = 3, rowend = 9;

    int col = 2;

    for (rowstart = 1; rowstart <= rowend; rowstart++) {

        Row rowL = sheet.createRow(rowstart); 

        Cell cell = rowL.createCell(col); 

        {

            XSSFCellStyle style = workbook.createCellStyle();

            style.setBorderLeft(BorderStyle.MEDIUM);

            cell.setCellStyle(style);

        }

    }


    // Code to draw Border at bottom

    int colstart1 = 2, colend1 = 6;


    Row rowB = sheet.createRow(90);

    for (colstart1 = 2; colstart1 <= colend1; colstart1++) {

        Cell cellB = rowB.createCell(colstart1);

        XSSFCellStyle style = workbook.createCellStyle();

        style.setBorderTop(BorderStyle.MEDIUM);

        cellB.setCellStyle(style);

    }


    // Code to draw Border at top

    int colstart = 2, colend = 6;


    Row rowT = sheet.createRow(0);

    for (colstart = 2; colstart <= colend; colstart++) {

        Cell cell = rowT.createCell(colstart);

        XSSFCellStyle style = workbook.createCellStyle();

        style.setBorderBottom(BorderStyle.MEDIUM);

        cell.setCellStyle(style);

    }


    // Code to draw Border at Right side

    int rowstart1 = 1, rowend1 = 9;

    for (rowstart1 = 1; rowstart1 <= rowend1; rowstart1++) {

        Row rowR = sheet.getRow(rowstart1); 

        Cell cellR = rowR.createCell(20); 

        {

            XSSFCellStyle style = workbook.createCellStyle();

            style.setBorderRight(BorderStyle.MEDIUM);

            cellR.setCellStyle(style);

        }

    }

我想在整个数据旁边设置边框,但在数据和边框之间留一个单元格空间(您可以参考下图)。

预期产出

http://img3.mukewang.com/62d7b18a0001a4d005030192.jpg

千万里不及你
浏览 109回答 2
2回答

子衿沉夜

不要以那种复杂的方式绘制边框。如果想这样做(使用单个CellStyles),则需要创建 8 个单个单元格样式。一种具有左上边缘的边框,一种具有上线的边框,一种具有右上边缘的边框,一种具有左线的边框,一种具有右线的边框,一种具有左下边缘的边框,一种具有边框底线和一个具有右下边缘的边框。然后,在创建单元格并用内容填充它们之后,必须将正确的单元格样式(之前创建的 8 个中的一个)应用于单元格。这是丑陋和复杂的编码。所以人们经常做你所做的事情,只是为每个单元格创建一个新的单元格样式。但是Excel在独特的单元格格式/单元格样式的数量上是有限的。请参阅Excel 规范和限制。因此,拥有包含大量数据的大工作表,很容易超过 64,000 种独特单元格格式/单元格样式的限制。因此,简单地为每个单元格创建一个新的单元格样式是错误的。Busy Developers' Guide to HSSF and XSSF Features中的绘制边框展示了如何做得更好。完整示例:import java.io.FileOutputStream;import org.apache.poi.ss.usermodel.*;import org.apache.poi.ss.util.CellRangeAddress;import org.apache.poi.ss.util.PropertyTemplate;import org.apache.poi.xssf.usermodel.XSSFWorkbook;class ExcelDrawingBorders {&nbsp;public static void main(String[] args) throws Exception {&nbsp; try (Workbook workbook = new XSSFWorkbook();&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;FileOutputStream fileout = new FileOutputStream("ExcelDrawingBorders.xlsx") ) {&nbsp; &nbsp;int startDataRow = 4;&nbsp; &nbsp;int endDataRow = 8;&nbsp; &nbsp;int startDataColumn = 2;&nbsp; &nbsp;int endDataColumn = 6;&nbsp; &nbsp;Sheet sheet = workbook.createSheet();&nbsp; &nbsp;for (int r = startDataRow; r <= endDataRow; r++) {&nbsp; &nbsp; Row row = sheet.createRow(r);&nbsp; &nbsp; for (int c = startDataColumn; c <= endDataColumn; c++) {&nbsp; &nbsp; &nbsp;Cell cell = row.createCell(c);&nbsp; &nbsp; &nbsp;cell.setCellFormula("RANDBETWEEN(10,50)");&nbsp; &nbsp; }&nbsp; &nbsp;}&nbsp; &nbsp;PropertyTemplate propertyTemplate = new PropertyTemplate();&nbsp; &nbsp;propertyTemplate.drawBorders(new CellRangeAddress(startDataRow-1, endDataRow+1, startDataColumn-1, endDataColumn+1),&nbsp;&nbsp; &nbsp; BorderStyle.MEDIUM, BorderExtent.OUTSIDE);&nbsp; &nbsp;propertyTemplate.applyBorders(sheet);&nbsp; &nbsp;workbook.write(fileout);&nbsp; }&nbsp;}}结果:在这里,PropertyTemplate和CellUtil为您完成了全部工作。PropertyTemplate创建所需的属性s&nbsp;Map。在应用于工作表时,它使用CellUtil它在工作簿级别创建 8 种所需的单元格样式并将它们应用于正确的单元格。即使不存在但需要的单元格也会被创建。

开满天机

代码示例&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PropertyTemplate ptT = new PropertyTemplate();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ptT.drawBorders(new CellRangeAddress(3, 3, 2, 6),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; BorderStyle.THICK, BorderExtent.TOP);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ptT.applyBorders(sheet);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PropertyTemplate ptL = new PropertyTemplate();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ptL.drawBorders(new CellRangeAddress(3, 9, 2, 2),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; BorderStyle.THICK, BorderExtent.LEFT);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ptL.applyBorders(sheet);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PropertyTemplate ptR = new PropertyTemplate();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ptR.drawBorders(new CellRangeAddress(3, 9, 6, 6),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; BorderStyle.THICK, BorderExtent.RIGHT);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ptR.applyBorders(sheet);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PropertyTemplate ptB = new PropertyTemplate();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ptB.drawBorders(new CellRangeAddress(9, 9, 2, 6),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; BorderStyle.THICK, BorderExtent.BOTTOM);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ptB.applyBorders(sheet);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java