[英]How to set border by leaving one cell space along each side of entire data using Apache POI 4.0.1 and java
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);
}
}
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. 在这里, PropertyTemplate和CellUtil为您完成整个工作。 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.