[英]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.