本文概述
为了设置单元格的边框,Apache POI提供了可用于设置边框颜色,细线,虚线等的方法。在以下示例中,我们创建了一个单元格,并从上到下,从右设置边框。参见示例。
Apache POI Excel单元格边框示例
package poiexample;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
public class BorderExample {
public static void main(String[] args) throws FileNotFoundException, IOException {
Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet("Sheet");
Row row = sheet.createRow(1);
Cell cell = row.createCell(1);
cell.setCellValue(101);
// Styling border of cell.
CellStyle style = wb.createCellStyle();
style.setBorderBottom(BorderStyle.THIN);
style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
style.setBorderRight(BorderStyle.THIN);
style.setRightBorderColor(IndexedColors.BLUE.getIndex());
style.setBorderTop(BorderStyle.MEDIUM_DASHED);
style.setTopBorderColor(IndexedColors.BLACK.getIndex());
cell.setCellStyle(style);
try (OutputStream fileOut = new FileOutputStream("srcmini.xls")) {
wb.write(fileOut);
}catch(Exception e) {
System.out.println(e.getMessage());
}
}
}
输出:
评论前必须登录!
注册