我想将第 2 列的所有单元格和 excel 工作表中最后一列的严格边框设置为粗,其他的很细。但效果是所有列的边框都很粗。
这是我的代码
for(int i = 0; i < sheet.getLastRowNum(); i++){
HSSFRow row = sheet.getRow(i);
for (int j = 0; j < row.getLastCellNum(); j++) {
HSSFCell cell = row.getCell(j);
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
if(j == 1 || j == row.getLastCellNum()-1){
style.setBorderRight(HSSFCellStyle.BORDER_THICK);
}
style.setTopBorderColor(HSSFColor.BLACK.index);
style.setBottomBorderColor(HSSFColor.BLACK.index);
style.setLeftBorderColor(HSSFColor.BLACK.index);
style.setRightBorderColor(HSSFColor.BLACK.index);
System.out.println("Row:"+i+", Column:"+j+", BorderRightStyleIndex:"+style.getBorderRight());
cell.setCellStyle(style);
}
}
这是控制台中的信息
Row:355, Column:0, BorderRightStyleIndex:1
Row:355, Column:1, BorderRightStyleIndex:5
Row:355, Column:2, BorderRightStyleIndex:1
Row:355, Column:3, BorderRightStyleIndex:1
Row:355, Column:4, BorderRightStyleIndex:1
Row:355, Column:5, BorderRightStyleIndex:1
Row:355, Column:6, BorderRightStyleIndex:1
Row:355, Column:7, BorderRightStyleIndex:1
Row:355, Column:8, BorderRightStyleIndex:1
Row:355, Column:9, BorderRightStyleIndex:1
Row:355, Column:10, BorderRightStyleIndex:5
Row:356, Column:0, BorderRightStyleIndex:1
Row:356, Column:1, BorderRightStyleIndex:5
Row:356, Column:2, BorderRightStyleIndex:1
Row:356, Column:3, BorderRightStyleIndex:1
Row:356, Column:4, BorderRightStyleIndex:1
Row:356, Column:5, BorderRightStyleIndex:1
Row:356, Column:6, BorderRightStyleIndex:1
Row:356, Column:7, BorderRightStyleIndex:1
Row:356, Column:8, BorderRightStyleIndex:1
Row:356, Column:9, BorderRightStyleIndex:1
Row:356, Column:10, BorderRightStyleIndex:5
我的代码的哪一部分出错了?为什么所有列的边框都很粗?
Excel
的单元格样式存储在工作簿级别。这就是为什么apache poi
CellStyles
也处于Workbook
水平。所以在你的代码中有一个也是唯一一个CellStyle
style
.根据您的代码,所有单元格都会应用相同的style
。因此,该style
的最后设置是使用的设置。那是右边框较粗的那些。
您至少需要两个CellStyles
,一个没有,一个有粗右边框。然后根据需要将两种单元格样式之一应用于Cell
。
另一种方法是使用PropertyTemplate
如DrawingBorders所示。由于这是最佳实践,在我看来,我将展示如何做到这一点。下面的代码首先在表区域中的所有单元格周围绘制细边框线。然后,它还在表格区域的第二列和最后一列中绘制一条粗的右边框线。
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.PropertyTemplate;
class DrawingBorders {
public static void main(String[] args) throws Exception {
Workbook workbook = WorkbookFactory.create(new FileInputStream("Excel.xls"));
Sheet sheet = workbook.getSheetAt(0);
boolean emptySheet = false;
int headerRowNum = sheet.getFirstRowNum();
Row headerRow = sheet.getRow(headerRowNum);
if (headerRow == null) emptySheet = true;
if (!emptySheet) {
int lastRow = sheet.getLastRowNum();
short firstCol = headerRow.getFirstCellNum();
short lastCol = headerRow.getLastCellNum();
lastCol--;
PropertyTemplate pt = new PropertyTemplate();
pt.drawBorders(new CellRangeAddress(
headerRowNum,
lastRow,
firstCol,
lastCol), BorderStyle.THIN, BorderExtent.ALL);
if (lastCol - firstCol > 1) {
pt.drawBorders(new CellRangeAddress(
headerRowNum,
lastRow,
firstCol + 1,
firstCol + 1), BorderStyle.THICK, BorderExtent.RIGHT);
}
pt.drawBorders(new CellRangeAddress(
headerRowNum,
lastRow,
lastCol,
lastCol), BorderStyle.THICK, BorderExtent.RIGHT);
pt.applyBorders(sheet);
}
FileOutputStream out = new FileOutputStream("ExcelWithBorders.xls");
workbook.write(out);
out.close();
workbook.close();
}
}