代码段
// 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
s),那么就需要创建8个单细胞样式。一个具有用于顶部左边缘的边界,一个具有用于顶部边缘的边界,一个具有用于右上边缘的边界,一个具有用于左边界线的边界,一个具有用于右边缘的边界,一个具有用于左下边缘的边界,一个具有用于左边缘的边界底线和一个有右下角边框的底线。然后,在创建单元格并用内容填充它们之后,必须将正确的单元格样式(之前创建的单元格中的一个)应用于单元格。
编码很丑陋而且很复杂。所以人们经常做你做的事情,只为每个单细胞创建一个新的细胞样式。但Excel
的独特细胞格式/细胞类型数量有限。见Excel specifications and limits。因此,如果有大量数据表,那么很容易超过64,000种独特单元格/单元格样式的限制。因此,简单地为每个单元格创建新的单元格样式是错误的。
Drawing Borders的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 {
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
s。在应用于工作表时,它使用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);