简体   繁体   English

如何使用Apache POI 4.0.1和Java在整个数据的每一侧留一个单元格空间来设置边框

[英]How to set border by leaving one cell space along each side of entire data using Apache POI 4.0.1 and java

  • Currently, I'm able to set border beside the entire data (You can refer following image). 目前,我可以在所有数据旁边设置边框(您可以参考下图)。

Current output 电流输出


Code snippet 程式码片段

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

  • I want to set border beside entire data but by leaving one cell space between data and border (You can refer following image). 我想在整个数据旁边设置边框,但要在数据和边框之间保留一个单元格空间(您可以参考下图)。

Expected output 预期产量

Don't do drawing borders that complicated way. 不要以这种复杂的方式绘制边框。

If one wants doing this that way (using single CellStyle s) then one would need creating 8 single cell styles. 如果要以这种方式(使用单个CellStyle )进行操作,则需要创建8个单个单元格样式。 One having borders for top left edge, one having borders for top line, one having borders for top right edge, one having borders for left line, one having borders for the right line, one having borders for bottom left edge, one having borders for bottom line and one having borders for bottom right edge. 一种带有左上边缘的边框,一种带有顶线的边框,一种带有左上线的边框,一种带有左线的边框,一种带有右线的边框,一种带有左下边缘的边框,一种带有边框的边界。底线,右下角带有边框。 Then, after creating the cells and filling them with content, the correct cell style (one out of the 8 created before) must be applied to the cell. 然后,在创建单元格并用内容填充它们之后,必须将正确的单元格样式(之前创建的8种之一)应用于该单元格。

That's ugly and complicated to code. 这很丑陋,编写起来很复杂。 So people often are doing what you do and simply create a new cell style for each single cell. 因此,人们经常在做您所要做的事情,并为每个单元格简单地创建一种新的单元格样式。 But Excel is limited in count of unique cell formats/cell styles. 但是Excel的唯一单元格格式/单元格样式数量有限。 See Excel specifications and limits . 请参阅Excel规格和限制 So having big sheets having much data, one easily exceeds that limit of 64,000 unique cell formats/cell styles. 因此,如果要有大量具有大量数据的工作表,很容易超过64,000种独特的单元格格式/单元格样式的限制。 So simply creating a new cell style for each single cell is wrong. 因此,仅为每个单元格创建新的单元格样式是错误的。

Drawing Borders in Busy Developers' Guide to HSSF and XSSF Features shows how to do it better. 《 HSSF和XSSF功能的繁忙的开发人员指南》中的 画边界显示了如何做得更好。

Complete Example: 完整的例子:

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 {

 public static void main(String[] args) throws Exception {

  try (Workbook workbook = new XSSFWorkbook(); 
       FileOutputStream fileout = new FileOutputStream("ExcelDrawingBorders.xlsx") ) {

   int startDataRow = 4;
   int endDataRow = 8;
   int startDataColumn = 2;
   int endDataColumn = 6;

   Sheet sheet = workbook.createSheet();

   for (int r = startDataRow; r <= endDataRow; r++) {
    Row row = sheet.createRow(r);
    for (int c = startDataColumn; c <= endDataColumn; c++) {
     Cell cell = row.createCell(c);
     cell.setCellFormula("RANDBETWEEN(10,50)");
    }
   }

   PropertyTemplate propertyTemplate = new PropertyTemplate();
   propertyTemplate.drawBorders(new CellRangeAddress(startDataRow-1, endDataRow+1, startDataColumn-1, endDataColumn+1), 
    BorderStyle.MEDIUM, BorderExtent.OUTSIDE);

   propertyTemplate.applyBorders(sheet);

   workbook.write(fileout);

  }
 }
}

Result: 结果:

在此处输入图片说明

Here the PropertyTemplate and CellUtil does the whole work for you. 在这里, PropertyTemplateCellUtil为您完成整个工作。 The PropertyTemplate creates the needed properties Map s. PropertyTemplate创建所需的属性Map And while applying to the sheet, it uses CellUtil which creates the 8 needed cell styles on workbook level and applies them to the correct cells. 在应用到工作表时,它使用CellUtil在工作簿级别创建8种所需的单元格样式,并将它们应用于正确的单元格。 Even not already present but needed cells will be created. 甚至还不存在,但将创建所需的单元格。

Code Sample 代码样例

          PropertyTemplate ptT = new PropertyTemplate();
          ptT.drawBorders(new CellRangeAddress(3, 3, 2, 6),
                  BorderStyle.THICK, BorderExtent.TOP);
          ptT.applyBorders(sheet);

          PropertyTemplate ptL = new PropertyTemplate();
          ptL.drawBorders(new CellRangeAddress(3, 9, 2, 2),
                  BorderStyle.THICK, BorderExtent.LEFT);
          ptL.applyBorders(sheet);

          PropertyTemplate ptR = new PropertyTemplate();          
          ptR.drawBorders(new CellRangeAddress(3, 9, 6, 6),
                  BorderStyle.THICK, BorderExtent.RIGHT);
          ptR.applyBorders(sheet);

          PropertyTemplate ptB = new PropertyTemplate();
          ptB.drawBorders(new CellRangeAddress(9, 9, 2, 6),
                  BorderStyle.THICK, BorderExtent.BOTTOM);
          ptB.applyBorders(sheet);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Apache POI 设置单元格边界不起作用 - Apache POI set cell border is not working Java Apache POI Excel在特定单元格上设置边框并设置货币单元格格式 - Java Apache POI Excel set border on specific cell and set currency cell format 如何使用 apache poi 4.0.1 和 java 生成可编辑的堆叠条形图? - How to generate editable Stacked-bar-chart using apache poi 4.0.1 and java? 如何使用Java Apache POI从excel中删除整行? - How to delete an entire row from excel using Java Apache POI? 如何在java中使用apache poi向excel表的整列添加数据验证? - How to add Data validation to entire column of an excel sheet using apache poi in java? 如何使用java Apache POI在excel中动态构建Border - How to Build Border dynamically in excel using java Apache POI 如何使用Apache POI(SXSSF)设置特定单元格的数据(数字)格式语言环境? - How to set data (number) format locale for specific cell using Apache POI (SXSSF)? 如何使用Apache POI在具有公式的单元格上设置数值? - How to set Numeric value on cell that has a formula using Apache POI? 如何使用 apache poi 4.1.0 设置单元格的背景颜色 - How to set background color of a cell using apache poi 4.1.0 如何使用 JAVA Apache POI 为幻灯片的每张幻灯片的背景设置不同的图像? - How to set a different image for the background of each slide of the powerpoint using JAVA Apache POI?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM