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

Java导出Excel三表头

时间:2015-11-04 17:53:37      阅读:281      评论:0      收藏:0      [点我收藏+]

标签:java   excel   三表头   

1、问题背景

     Java导出Excel表格时,表头出现了三个,即多表头Excel


2、实现源码

/**
 * 
 * @Project:Report
 * @Title:ThreeHead.java
 * @Package:com.you.excel
 * @Description:
 * @Author:YouHaiDong
 * @Date:2015年11月4日 下午3:10:12
 * @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>导出三表头Excel</p>
 * @ClassName:ThreeHead
 * @Description:
 * @Author:YouHaiDong
 * @Date:2015年11月4日 下午3:10:12
 * 
 */
public class ThreeHead 
{
	/**
	 * 导出三表头表格
	 * @Title:ThreeHead
	 * @Description:
	 * @param args
	 * @Date:2015年11月4日 下午3:10:12
	 * @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(7); 
		c2.setCellValue(new HSSFRichTextString("物流学院")); 
		HSSFCell c3 = row.createCell(13); 
		c3.setCellValue(new HSSFRichTextString("行政学院")); 
		HSSFRow row1 = sheet.createRow(1); 
		HSSFCell c6 = row1.createCell(1); 
		c6.setCellValue(new HSSFRichTextString("一班")); 
		HSSFCell c7 = row1.createCell(3); 
		c7.setCellValue(new HSSFRichTextString("二班")); 
		HSSFCell c8 = row1.createCell(5); 
		c8.setCellValue(new HSSFRichTextString("三班"));
		HSSFCell c9 = row1.createCell(7); 
		c9.setCellValue(new HSSFRichTextString("一班")); 
		HSSFCell c10 = row1.createCell(9); 
		c10.setCellValue(new HSSFRichTextString("二班")); 
		HSSFCell c11 = row1.createCell(11); 
		c11.setCellValue(new HSSFRichTextString("三班"));
		HSSFCell c12 = row1.createCell(13); 
		c12.setCellValue(new HSSFRichTextString("一班")); 
		HSSFCell c13 = row1.createCell(15); 
		c13.setCellValue(new HSSFRichTextString("二班")); 
		HSSFCell c14 = row1.createCell(17); 
		c14.setCellValue(new HSSFRichTextString("三班"));
		HSSFRow row2 = sheet.createRow(2); 
		HSSFCell c15 = row2.createCell(1); 
		c15.setCellValue(new HSSFRichTextString("上学期"));
		HSSFCell c16 = row2.createCell(2); 
		c16.setCellValue(new HSSFRichTextString("下学期"));
		HSSFCell c17 = row2.createCell(3); 
		c17.setCellValue(new HSSFRichTextString("上学期"));
		HSSFCell c18 = row2.createCell(4); 
		c18.setCellValue(new HSSFRichTextString("下学期"));
		HSSFCell c19 = row2.createCell(5); 
		c19.setCellValue(new HSSFRichTextString("上学期"));
		HSSFCell c20 = row2.createCell(6); 
		c20.setCellValue(new HSSFRichTextString("下学期"));
		HSSFCell c21 = row2.createCell(7); 
		c21.setCellValue(new HSSFRichTextString("上学期"));
		HSSFCell c22 = row2.createCell(8); 
		c22.setCellValue(new HSSFRichTextString("下学期"));
		HSSFCell c23 = row2.createCell(9); 
		c23.setCellValue(new HSSFRichTextString("上学期"));
		HSSFCell c24 = row2.createCell(10); 
		c24.setCellValue(new HSSFRichTextString("下学期"));
		HSSFCell c25 = row2.createCell(11); 
		c25.setCellValue(new HSSFRichTextString("上学期"));
		HSSFCell c26 = row2.createCell(12); 
		c26.setCellValue(new HSSFRichTextString("下学期"));
		HSSFCell c27 = row2.createCell(13); 
		c27.setCellValue(new HSSFRichTextString("上学期"));
		HSSFCell c28 = row2.createCell(14); 
		c28.setCellValue(new HSSFRichTextString("下学期"));
		HSSFCell c29 = row2.createCell(15); 
		c29.setCellValue(new HSSFRichTextString("上学期"));
		HSSFCell c30 = row2.createCell(16); 
		c30.setCellValue(new HSSFRichTextString("下学期"));
		HSSFCell c31 = row2.createCell(17); 
		c31.setCellValue(new HSSFRichTextString("上学期"));
		HSSFCell c32 = row2.createCell(18); 
		c32.setCellValue(new HSSFRichTextString("下学期"));
		
		Region region1 = new Region(0, (short)0, 2, (short)0); 
		Region region2 = new Region(0, (short)1, 0, (short)6); 
		Region region3 = new Region(0, (short)7, 0, (short)12); 
		Region region4 = new Region(0, (short)13, 0, (short)18); 
		Region region5 = new Region(1, (short)1, 1, (short)2); 
		Region region6 = new Region(1, (short)3, 1, (short)4); 
		Region region7 = new Region(1, (short)5, 1, (short)6); 
		Region region8 = new Region(1, (short)7, 1, (short)8); 
		Region region9 = new Region(1, (short)9, 1, (short)10); 
		Region region10 = new Region(1, (short)11, 1, (short)12); 
		Region region11 = new Region(1, (short)13, 1, (short)14); 
		Region region12 = new Region(1, (short)15, 1, (short)16); 
		Region region13 = new Region(1, (short)17, 1, (short)18); 
		
		sheet.addMergedRegion(region1); 
		sheet.addMergedRegion(region2); 
		sheet.addMergedRegion(region3); 
		sheet.addMergedRegion(region4); 
		sheet.addMergedRegion(region5); 
		sheet.addMergedRegion(region6); 
		sheet.addMergedRegion(region7); 
		sheet.addMergedRegion(region8); 
		sheet.addMergedRegion(region9); 
		sheet.addMergedRegion(region10); 
		sheet.addMergedRegion(region11); 
		sheet.addMergedRegion(region12); 
		sheet.addMergedRegion(region13); 
		
		FileOutputStream stream = new FileOutputStream("d:/student.xls"); 
		workbook.write(stream);
	}

}

3、实现结果

技术分享

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

Java导出Excel三表头

标签:java   excel   三表头   

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

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