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);
}
}
POI设置边框的更多相关文章
- poi设置excel表格边框、字体等
POI中可能会用到一些需要设置EXCEL单元格格式的操作小结: 先获取工作薄对象: HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb ...
- poi生成excel整理(设置边框/字体/颜色/加粗/居中/)
转: poi生成excel整理(设置边框/字体/颜色/加粗/居中/) 2016年12月02日 11:05:23 吃奶的牛 阅读数:34324 HSSFWorkbook wb = new HSSFW ...
- zxing生成二维码设置边框颜色
真是研究了很久很久,满满的泪啊 zxing生成二维码,默认是可以增加空白边框的,但是并没有可以设置边框颜色的属性. 其中增加空白边框的属性的一句话是: Map hints = new HashMap( ...
- POI 设置Excel样式(转)
POI 设置Excel样式 POI中可能会用到一些需要设置EXCEL单元格格式的操作小结: 先获取工作薄对象: HSSFWorkbook wb = new HSSFWorkbook(); HSSFSh ...
- Swift - UIView,UItableView,Cell设置边框方法
// 设置边框的宽度 cell.layer.borderWidth = 1 // 设置边框的颜色 cell.layer.borderColor = UIColor.blackColor().CGCol ...
- ImageView设置边框的两种方式
转载:http://www.2cto.com/kf/201308/239945.html package cc.testimageviewbounds; import android.os.Bundl ...
- android给View设置边框 填充颜色 弧度
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http: ...
- 【C#】分享一个可灵活设置边框的Panel
---------------------------更新:2014-05-19--------------------------- 优化了一下逻辑,就是既然可以通过设置BorderSide=Non ...
- css3设置边框属性
css设置边框属性:设置边框圆角 <!DOCTYPE html> <html> <head lang="en"> <meta charse ...
随机推荐
- HDU 2841 Visible Trees 数论+容斥原理
H - Visible Trees Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- Webservice加上SoapHeader验证方式
提供一种基于SoapHeader的自定义验证方式,代码如下: public class MySoapHeader : System.Web.Services.Protocols.SoapHeader ...
- .NET 下各种Resource的读取方式
1) Embedded Resource (Build Action 设置为 Embedded Resource) 在运行时使用GetManifestResourceStream读取 Image.Fr ...
- ☆☆在Eclipse中编译NDK的so文件(普通安卓项目转换为NDK项目的设定)
1 将Native的编译链接配置加入项目中 2 进行编译 3 项目支持Native后,在首尾分别新增了两个编译过程
- NDK编译生成so文件
1 首先加载项目
- 能加载文件或程序集“System.Data.SQLite”或它的某一个依赖项。试图加载格式不正确的程序。
现象: 能加载文件或程序集“System.Data.SQLite”或它的某一个依赖项.试图加载格式不正确的程序.
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2) A. Bear and Elections 优先队列
A. Bear and Elections ...
- ITEYE中的读书笔记:重构其实就是持续改进
原文地址:http://hawkon.iteye.com/blog/2093338#bc2355877 前段时间同事参加ITEYE的试读有奖, 没想到得了个奖,拿到一本书.由于同事的推荐我也认真读了一 ...
- mvc-4控制器和状态(1)
导语 将状态保存在客户端可以加快页面反映:但应当避免状态或数据保存在DOM中:在MVC中,状态应该保存在控制器中 控制器是视图和模型的纽带,只有控制器知道视图和模型的存在并将它们连接在一起:当加载页面 ...
- ZOJ2930 The Worst Schedule(最小割)
题目大概说有n个任务,每个任务可以提前或推迟,提前或推迟各有一定的费用,有的任务一旦推迟另一个任务也必须推迟,问怎么安排任务使花费最少,且最少花费的条件下提前的任务数最多能多少. 问题就是要把各个任务 ...