POI设置边框

在做一个电子表格时,边框的设置有时是必不可少的。这一节就来介绍边框,设置时,可以指定边框的位置,边框的种类,边框的顔色。

首先是边框的位置和种类。对单元格设置边框时,有上下左右位置之分,所以POI也准备了四个不同的方法。

上部的边框:

setBorderTop
public void setBorderTop(short border)
set the type of border to use for the top
border of the cell 

Parameters:
  border - type

下部的边框:

setBorderBottom
public void setBorderBottom(short border)
set the type of border to use for the
bottom border of the cell 

Parameters:
  border - type

左侧的边框:

setBorderLeft
public void setBorderLeft(short border)
set the type of border to use for the
left border of the cell 

Parameters:
  border - type

右侧的边框:

setBorderRight
public void setBorderRight(short border)
set the type of border to use for the
right border of the cell 

Parameters:
  border - type

参数通过表示边框种类的short型值来指定。下面是定义在「HSSFCellStyle」类里可以被指定值的一览表。

值说明

BORDER_DASH_DOT dash-dot border
BORDER_DASH_DOT_DOT dash-dot-dot border
BORDER_DASHED dash border
BORDER_DOTTED dot borderhair-line border
BORDER_DOUBLE double-line border
BORDER_HAIR hair-line border
BORDER_MEDIUM Medium border
BORDER_MEDIUM_DASH_DOT medium dash-dot border
BORDER_MEDIUM_DASH_DOT_DOT medium dash-dot-dot border
BORDER_MEDIUM_DASHED Medium dashed border
BORDER_NONE No border
BORDER_SLANTED_DASH_DOT slanted dash-dot border
BORDER_THICK Thick border
BORDER_THIN Thin border

比如要在单元格下边框设置两重线的边框时,按如下方法:

HSSFWorkbook workbook = new HSSFWorkbook();

HSSFCellStyle style = workbook.createCellStyle();
style.setBorderRight(HSSFCellStyle.BORDER_THIN);

下面再看看指定边框顔色。同样也分为上下左右边框来操作。

上部的边框:

setTopBorderColor
public void setTopBorderColor(short color)
set the color to use for the top border 

Parameters:
  color -

下部的边框:

setBottomBorderColor
public void setBottomBorderColor(short color)
set the color to use for the bottom border 

Parameters:
  color -

左侧的边框:

setLeftBorderColor
public void setLeftBorderColor(short color)
set the color to use for the left border 

Parameters:
  color -

右侧的边框:

setRightBorderColor
public void setRightBorderColor(short color)
set the color to use for the right border 

Parameters:
  color -

仍然是通过参数来指定顔色,而且使用方法和前面一节也是一样。具体如下:

HSSFWorkbook workbook = new HSSFWorkbook();

HSSFCellStyle style = workbook.createCellStyle();
style.setRightBorderColor(HSSFColor.RED.index);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);

示例程序

实际动手做做吧。首先看看如何设置上下左右的边框。

import java.io.*;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.hssf.usermodel.HSSFPalette;

public class POISample{
  public static void main(String[] args){
    HSSFWorkbook workbook = new HSSFWorkbook();
    HSSFSheet sheet = workbook.createSheet();

    HSSFRow row = sheet.createRow(1);
    HSSFCell cell1 = row.createCell((short)1);
    HSSFCell cell2 = row.createCell((short)2);

    HSSFCellStyle style1 = workbook.createCellStyle();
    style1.setBorderTop(HSSFCellStyle.BORDER_DOUBLE);
    style1.setBorderLeft(HSSFCellStyle.BORDER_DOUBLE);
    style1.setTopBorderColor(HSSFColor.GOLD.index);
    style1.setLeftBorderColor(HSSFColor.PLUM.index);
    cell1.setCellStyle(style1);

    HSSFCellStyle style2 = workbook.createCellStyle();
    style2.setBorderBottom(HSSFCellStyle.BORDER_DOUBLE);
    style2.setBorderRight(HSSFCellStyle.BORDER_DOUBLE);
    style2.setBottomBorderColor(HSSFColor.ORANGE.index);
    style2.setRightBorderColor(HSSFColor.SKY_BLUE.index);
    cell2.setCellStyle(style2);

    cell1.setCellValue("U & L");
    cell2.setCellValue("B & R");

    FileOutputStream out = null;
    try{
      out = new FileOutputStream("sample.xls");
      workbook.write(out);
    }catch(IOException e){
      System.out.println(e.toString());
    }finally{
      try {
        out.close();
      }catch(IOException e){
        System.out.println(e.toString());
      }
    }
  }
}

上面程序既改了顔色,也设置了上和左的边框各一个,右和下的边框各一个。

上下左右边框

下面再对边框种类进行各种各样的顔色改变来看看效果。

import java.io.*;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.util.HSSFColor;

public class POISample{
  static HSSFWorkbook workbook;

  public static void main(String[] args){
    workbook = new HSSFWorkbook();

    HSSFSheet sheet = workbook.createSheet();

    HSSFRow row[] = new HSSFRow[5];
    for (int i = 0; i < 5 ; i++){
      row[i] = sheet.createRow(i);
    }

    HSSFCell cell[][] = new HSSFCell[5][3];
    for (int i = 0; i < 5; i++){
      for (int j = 0; j < 3 ; j++){
        cell[i][j] = row[i].createCell((short)j);
      }
    }

    setStyle(cell[0][0], "DASH_DOT",
                          HSSFCellStyle.BORDER_DASH_DOT);
    setStyle(cell[0][1], "DASH_DOT_DOT",
                          HSSFCellStyle.BORDER_DASH_DOT_DOT);
    setStyle(cell[0][2], "DASHED",
                          HSSFCellStyle.BORDER_DASHED);

    setStyle(cell[1][0], "DOTTED",
                          HSSFCellStyle.BORDER_DOTTED);
    setStyle(cell[1][1], "DOUBLE",
                          HSSFCellStyle.BORDER_DOUBLE);
    setStyle(cell[1][2], "HAIR",
                          HSSFCellStyle.BORDER_HAIR);

    setStyle(cell[2][0], "MEDIUM",
                          HSSFCellStyle.BORDER_MEDIUM);
    setStyle(cell[2][1], "MEDIUM_DASH_DOT",
                          HSSFCellStyle.BORDER_MEDIUM_DASH_DOT);
    setStyle(cell[2][2], "MEDIUM_DASH_DOT_DOT",
                          HSSFCellStyle.BORDER_MEDIUM_DASH_DOT_DOT);

    setStyle(cell[3][0], "MEDIUM_DASHED",
                          HSSFCellStyle.BORDER_MEDIUM_DASHED);
    setStyle(cell[3][1], "NONE",
                          HSSFCellStyle.BORDER_NONE);
    setStyle(cell[3][2], "SLANTED_DASH_DOT",
                          HSSFCellStyle.BORDER_SLANTED_DASH_DOT);

    setStyle(cell[4][0], "THICK", HSSFCellStyle.BORDER_THICK);
    setStyle(cell[4][1], "THIN", HSSFCellStyle.BORDER_THIN);

    FileOutputStream out = null;
    try{
      out = new FileOutputStream("sample.xls");
      workbook.write(out);
    }catch(IOException e){
      System.out.println(e.toString());
    }finally{
      try {
        out.close();
      }catch(IOException e){
        System.out.println(e.toString());
      }
    }
  }

  public static void setStyle(HSSFCell cell,
                          String bn, short border){
    HSSFCellStyle style = workbook.createCellStyle();
    style.setBorderBottom(border);
    style.setBottomBorderColor(HSSFColor.ORANGE.index);
    cell.setCellStyle(style);

    cell.setCellValue(bn);
  }
}
posted @ 2016-08-19 11:28  疯子110  阅读(25886)  评论(0编辑  收藏  举报