[英]How to set border by leaving one cell space along each side of entire data using Apache POI 4.0.1 and java
程式码片段
// 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);
}
}
不要以这种复杂的方式绘制边框。
如果要以这种方式(使用单个CellStyle
)进行操作,则需要创建8个单个单元格样式。 一种带有左上边缘的边框,一种带有顶线的边框,一种带有左上线的边框,一种带有左线的边框,一种带有右线的边框,一种带有左下边缘的边框,一种带有边框的边界。底线,右下角带有边框。 然后,在创建单元格并用内容填充它们之后,必须将正确的单元格样式(之前创建的8种之一)应用于该单元格。
这很丑陋,编写起来很复杂。 因此,人们经常在做您所要做的事情,并为每个单元格简单地创建一种新的单元格样式。 但是Excel
的唯一单元格格式/单元格样式数量有限。 请参阅Excel规格和限制 。 因此,如果要有大量具有大量数据的工作表,很容易超过64,000种独特的单元格格式/单元格样式的限制。 因此,仅为每个单元格创建新的单元格样式是错误的。
《 HSSF和XSSF功能的繁忙的开发人员指南》中的 画边界显示了如何做得更好。
完整的例子:
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);
}
}
}
结果:
在这里, PropertyTemplate和CellUtil为您完成整个工作。 PropertyTemplate
创建所需的属性Map
。 在应用到工作表时,它使用CellUtil
在工作簿级别创建8种所需的单元格样式,并将它们应用于正确的单元格。 甚至还不存在,但将创建所需的单元格。
代码样例
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.