Java 我想将某些列的边框设置为“粗”,但所有列都已设置为“粗”
我想将第2列和excel工作表中最后一列的所有单元格的右边框设置为“厚”,其他单元格的边框设置为“薄”。但效果是所有列的边框都很厚 这是我的密码Java 我想将某些列的边框设置为“粗”,但所有列都已设置为“粗”,java,apache-poi,Java,Apache Poi,我想将第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
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);
}
}
我的代码哪部分出错了?为什么所有列的边框都很厚?Excel的单元格样式存储在工作簿级别。这就是为什么ApachePOI的CellStyles也在工作簿级别。因此,在您的代码中只有一种单元样式。根据您的代码,所有单元格都应用了相同的样式。因此,该样式的最后设置就是所使用的设置。那些是右边框很厚的
您至少需要两种单元格样式,一种没有,另一种右边框很厚。然后根据需要将两种单元格样式中的一种应用于单元格
另一种方法是使用PropertyTemplate,如中所示。因为这是最佳实践,在我看来,我将展示如何做到这一点。下面的代码首先在表区域中的所有单元格周围绘制细边框线。然后在表区域的第二列和最后一列中绘制一条粗的右边框线
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();
}
}
所有单元格应用相同的样式。因此,该样式的最后设置就是所使用的设置。那些是右边框很厚的。您至少需要两种单元格样式,一种没有,另一种右边框很厚。然后根据需要应用两种单元格样式之一。另一种方法是使用PropertyTemplate,如中所示。谢谢你,Axel Richter,这就是重点!所有的单元格都有相同的样式,如果我改变这种样式,看起来只有这个单元格会改变,但事实是它会改变所有设置为这种样式的单元格。当我创建一些特殊单元的新样式时,问题就解决了。谢谢。
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();
}
}