无涯教程-POI Excel - 边框单元格

246 阅读1分钟

为了设置单元格的边框,Apache POI提供了可用于设置边框颜色(color),细线(thin),虚线(dashed)等的方法。在以下示例中,无涯教程创建了一个单元格,并从上到下,从右设置边框。参见示例。

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);
		   //单元格的样式边框。
		    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("Learnfk.xls")) {
		        wb.write(fileOut);
		    }catch(Exception e) {
		    	System.out.println(e.getMessage());
		    }
	}
}

输出:

Apache POI Excel Cell Border

参考链接

www.learnfk.com/apache-poi-…