Apache POI Excel 单元格边框

为了设置单元格的边框,Apache POI 提供了可用于设置边框颜色、细线、虚线等的方法。在下面的示例中,我们创建了一个单元格并设置了从上到下和右侧的边框。请参阅示例。

Apache POI Excel 单元格边框示例

package com.yiidian;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

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(CellStyle.BORDER_THICK);
            style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
            style.setBorderRight(CellStyle.BORDER_DASHED);
            style.setRightBorderColor(IndexedColors.BLUE.getIndex());
            style.setBorderTop(CellStyle.BORDER_DOUBLE);
            style.setTopBorderColor(IndexedColors.BLACK.getIndex());
            cell.setCellStyle(style);  
            try (OutputStream fileOut = new FileOutputStream("yiidian.xls")) {
                wb.write(fileOut);  
            }catch(Exception e) {  
                System.out.println(e.getMessage());  
            }  
    }  
}  

输出结果为:

热门文章

优秀文章