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

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

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

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页		
		/**
		* 行和列起始都是从0开始
		*/
		HSSFRow row = sheet.createRow(1); // 创建一行,参数1表示第2行
		HSSFCell cellB2 = row.createCell(1); // 在B2(2行2列)位置创建一个单元格
		HSSFCell cellB3 = row.createCell(2); // 在B3(2行3列)位置创建一个单元格
		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;
		}
	}
}
生成的excel样式为
简单说明:

  1.excel生成过程: excel-->sheet-->row-->cell

  2.索引从0开始

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

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

// 使用RegionUtil类为合并后的单元格添加边框
RegionUtil.setBorderBottom(1, cra, sheet); // 下边框
RegionUtil.setBorderLeft(1, cra, sheet); // 左边框
RegionUtil.setBorderRight(1, cra, sheet); // 有边框
RegionUtil.setBorderTop(1, cra, sheet); // 上边框

效果为:

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

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值