POI实现EXCEL单元格合并及边框样式

  POI实现EXCEL单元格合并及边框样式



下面例子为创建产生一个excel,合并单元格,然后为合并后的单元格添加边框

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
           
           
package test ;
import java.io.FileOutputStream ;
import java.io.IOException ;
import org.apache.poi.hssf.usermodel.HSSFCell ;
import org.apache.poi.hssf.usermodel.HSSFCellStyle ;
import org.apache.poi.hssf.usermodel.HSSFRow ;
import org.apache.poi.hssf.usermodel.HSSFSheet ;
import org.apache.poi.hssf.usermodel.HSSFWorkbook ;
import org.apache.poi.ss.usermodel.BorderStyle ;
import org.apache.poi.ss.usermodel.Font ;
import org.apache.poi.ss.usermodel.HorizontalAlignment ;
import org.apache.poi.ss.util.CellRangeAddress ;
import org.apache.poi.ss.util.RegionUtil ;
public class ExcelPoiTest {
public static void main ( String [] args ) {
HSSFWorkbook workbook = new HSSFWorkbook (); // 创建一个excel
// excel生成过程: excel-->sheet-->row-->cell
HSSFSheet sheet = workbook . createSheet ( "test" ); // 为excel创建一个名为test的sheet页
HSSFRow row = sheet . createRow ( 1 ); // 创建一行,参数2表示第一行
HSSFCell cellB2 = row . createCell ( 1 ); // 在B2位置创建一个单元格
HSSFCell cellB3 = row . createCell ( 2 ); // 在B3位置创建一个单元格
cellB2 . setCellValue ( "单元格B2" ); // B2单元格填充内容
cellB3 . setCellValue ( "单元格B3" ); // B3单元格填充内容
HSSFCellStyle cellStyle = workbook . createCellStyle (); // 单元格样式
Font fontStyle = workbook . createFont (); // 字体样式
fontStyle . setBold ( true ); // 加粗
fontStyle . setFontName ( "黑体" ); // 字体
fontStyle . setFontHeightInPoints (( short ) 11 ); // 大小
// 将字体样式添加到单元格样式中
cellStyle . setFont ( fontStyle );
// 边框,居中
cellStyle . setAlignment ( HorizontalAlignment . CENTER );
cellStyle . setBorderBottom ( BorderStyle . THIN );
cellStyle . setBorderLeft ( BorderStyle . THIN );
cellStyle . setBorderRight ( BorderStyle . THIN );
cellStyle . setBorderTop ( BorderStyle . THIN );
cellB2 . setCellStyle ( cellStyle ); // 为B2单元格添加样式
// 合并单元格
CellRangeAddress cra = new CellRangeAddress ( 1 , 3 , 1 , 3 ); // 起始行, 终止行, 起始列, 终止列
sheet . addMergedRegion ( cra );
// 使用RegionUtil类为合并后的单元格添加边框
RegionUtil . setBorderBottom ( 1 , cra , sheet ); // 下边框
RegionUtil . setBorderLeft ( 1 , cra , sheet ); // 左边框
RegionUtil . setBorderRight ( 1 , cra , sheet ); // 有边框
RegionUtil . setBorderTop ( 1 , cra , sheet ); // 上边框
// 输出到本地
String excelName = "/myExcel.xls" ;
FileOutputStream out = null ;
try {
out = new FileOutputStream ( excelName );
workbook . write ( out );
out . flush ();
out . close ();
} catch ( Exception e ) {
e . printStackTrace ();
} finally {
if ( out != null )
try {
out . close ();
} catch ( IOException e ) {
e . printStackTrace ();
}
out = null ;
}
}
}
 来自CODE的代码片
ExcelPoiTest.java


生成的excel样式为


简单说明:
  1.excel生成过程: excel-->sheet-->row-->cell

  2.索引从0开始

  3.合并单元格后保留最左上角的单元格(B3单元格被B2单元格覆盖)

  4.合并后单元格边框通过RegionUtil设置,如果删除以下代码

[java]  view plain  copy
  1. <span style="font-size:18px;"><strong>      // 使用RegionUtil类为合并后的单元格添加边框  
  2.         RegionUtil.setBorderBottom(1, cra, sheet); // 下边框  
  3.         RegionUtil.setBorderLeft(1, cra, sheet); // 左边框  
  4.         RegionUtil.setBorderRight(1, cra, sheet); // 有边框  
  5.         RegionUtil.setBorderTop(1, cra, sheet); // 上边框</strong></span>  
效果为:


可以看到只有B2单元格有边框。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值