码迷,mamicode.com
首页 > 编程语言 > 详细

Java导出Excel合并单元格

时间:2015-11-04 16:07:04      阅读:320      评论:0      收藏:0      [点我收藏+]

标签:java   excel   合并单元格   

1、问题背景

     利用POI导出Excel表格,在导出的过程中涉及到双表头,即需要合并单元格


2、实现源码

/**
 * 
 * @Project:Report
 * @Title:MergeCell.java
 * @Package:com.you.excel
 * @Description:
 * @Author:YouHaiDong
 * @Date:2015年11月4日 下午2:36:46
 * @Version:
 */
package com.you.excel;

import java.io.FileOutputStream;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
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.hssf.util.Region;

/**
 * <p>合并单元格</p>
 * @ClassName:MergeCell
 * @Description:
 * @Author:YouHaiDong
 * @Date:2015年11月4日 下午2:36:46
 * 
 */
public class MergeCell 
{
	/**
	 * 合并单元格
	 * @Title:MergeCell
	 * @Description:
	 * @param args
	 * @Date:2015年11月4日 下午2:36:46
	 * @return: void 
	 * @throws Exception
	 */
	@SuppressWarnings({ "resource", "deprecation" })
	public static void main(String[] args) throws Exception 
	{
		//创建workbook 
		HSSFWorkbook workbook = new HSSFWorkbook(); 
		//创建sheet页
	    HSSFSheet sheet = workbook.createSheet("学生表"); 
		//创建单元格
		HSSFRow row = sheet.createRow(0); 
		HSSFCell c0 = row.createCell(0); 
		c0.setCellValue(new HSSFRichTextString("学号")); 
		HSSFCell c1 = row.createCell(1); 
		c1.setCellValue(new HSSFRichTextString("姓名")); 
		HSSFCell c2 = row.createCell(2); 
		c2.setCellValue(new HSSFRichTextString("性别")); 
		HSSFCell c3 = row.createCell(3); 
		c3.setCellValue(new HSSFRichTextString("年龄")); 
		HSSFCell c4 = row.createCell(4); 
		c4.setCellValue(new HSSFRichTextString("2015年分数")); 
		HSSFCell c5 = row.createCell(7); 
		c5.setCellValue(new HSSFRichTextString("2014年分数")); 
		HSSFRow row1 = sheet.createRow(1); 
		HSSFCell c6 = row1.createCell(4); 
		c6.setCellValue(new HSSFRichTextString("语文")); 
		HSSFCell c7 = row1.createCell(5); 
		c7.setCellValue(new HSSFRichTextString("数学")); 
		HSSFCell c8 = row1.createCell(6); 
		c8.setCellValue(new HSSFRichTextString("外语"));
		HSSFCell c9 = row1.createCell(7); 
		c9.setCellValue(new HSSFRichTextString("语文")); 
		HSSFCell c10 = row1.createCell(8); 
		c10.setCellValue(new HSSFRichTextString("数学")); 
		HSSFCell c11 = row1.createCell(9); 
		c11.setCellValue(new HSSFRichTextString("外语"));
		
		Region region1 = new Region(0, (short)0, 1, (short)0); 
		Region region2 = new Region(0, (short)1, 1, (short)1); 
		Region region3 = new Region(0, (short)2, 1, (short)2); 
		Region region4 = new Region(0, (short)3, 1, (short)3); 
		Region region5 = new Region(0, (short)4, 0, (short)6); 
		Region region6 = new Region(0, (short)7, 0, (short)9); 
		sheet.addMergedRegion(region1); 
		sheet.addMergedRegion(region2); 
		sheet.addMergedRegion(region3); 
		sheet.addMergedRegion(region4); 
		sheet.addMergedRegion(region5); 
		sheet.addMergedRegion(region6); 
		
		FileOutputStream stream = new FileOutputStream("d:/student.xls"); 
		workbook.write(stream);
	}

}

3、实现结果

技术分享

版权声明:本文为博主原创文章,未经博主允许不得转载。

Java导出Excel合并单元格

标签:java   excel   合并单元格   

原文地址:http://blog.csdn.net/you23hai45/article/details/49638165

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!