java操作poi的excel表格的线条怎么变为黑色?
public TestExcel(){
createExcelFile();
}
private void createExcelFile() {
HSSFWorkbook hwb = new HSSFWorkbook();
HSSFSheet sheet = hwb.createSheet("test sheet");
//创建一个样式
HSSFCellStyle style = hwb.createCellStyle();
//设置边框样式
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
//设置边框颜色
style.setTopBorderColor(HSSFColor.BLACK.index);
style.setBottomBorderColor(HSSFColor.BLACK.index);
style.setLeftBorderColor(HSSFColor.BLACK.index);
style.setRightBorderColor(HSSFColor.BLACK.index);
for(int j=0;j<5;j++){
HSSFRow row = sheet.createRow(j);
for(int i=0;i<10;i++){
HSSFCell cell = row.createCell(i);
cell.setCellStyle(style);
cell.setCellValue(j+"*"+i);
}
}
File file = new File("./test.xls");
try {
FileOutputStream fos = new FileOutputStream(file);
hwb.write(fos);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
Java中设置表格边线颜色:
import com.spire.xls.*;
import java.awt.*;
public class AddBorders {
public static void main(String[] args){
//Create a workbook instance
Workbook workbook = new Workbook();
//Load the Excel file
workbook.loadFromFile("Sample-SetBorder.xlsx");
//Get the first worksheet
Worksheet sheet = workbook.getWorksheets().get(0);
//Get the cell range that you want to apply border style
CellRange cr = sheet.getCellRange(sheet.getFirstRow(), sheet.getFirstColumn(), sheet.getLastRow(), sheet.getLastColumn());
//Apply border style
cr.getBorders().setLineStyle(LineStyleType.Double);
cr.getBorders().getByBordersLineType(BordersLineType.DiagonalDown).setLineStyle(LineStyleType.None);
cr.getBorders().getByBordersLineType(BordersLineType.DiagonalUp).setLineStyle(LineStyleType.None);
cr.getBorders().setColor(Color.BLUE);
//Save the resultant file
workbook.saveToFile("SetBorder.xlsx", ExcelVersion.Version2010);
}
}
参考自:网页链接