在做一個電子表格時,邊框的設置有時是必不可少的。這一節就來介紹邊框,設置時,可以指定邊框的位置,邊框的種類,邊框的顔色。
首先是邊框的位置和種類。對單元格設置邊框時,有上下左右位置之分,所以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);
}
}
