设置Java导出Excel表头

1、问题背景

     有一个学生表,需要导出到Excel,有学号、姓名、性别和年龄四个字段


2、实现源码

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

import java.io.FileOutputStream;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

/**
 * <p>请用一句话概括功能</p>
 * @ClassName:ExcelExport
 * @Description:
 * @Author:YouHaiDong
 * @Date:2015年11月4日 下午2:23:49
 * 
 */
public class ExcelExport 
{

	/**
	 * 
	 * @Title:ExcelExport
	 * @Description:
	 * @param args
	 * @Date:2015年11月4日 下午2:23:49
	 * @return: void 
	 * @throws Exception
	 */
	@SuppressWarnings("resource")
	public static void main(String[] args) throws Exception 
	{
		// 创建一个Workbook
		HSSFWorkbook workbook = new HSSFWorkbook();
		// 创建一个sheet页
		HSSFSheet sheet = workbook.createSheet("学生表");
		// 创建第一行
		HSSFRow row = sheet.createRow(0);
		// 创建单元格
		HSSFCell cell1 = row.createCell(0);
		HSSFCell cell2 = row.createCell(1);
		HSSFCell cell3 = row.createCell(2);
		HSSFCell cell4 = row.createCell(2);
		// 设置表头
		cell1.setCellValue("学号");
		cell2.setCellValue("姓名");
		cell3.setCellValue("性别");
		cell4.setCellValue("年龄");
		
		FileOutputStream stream = new FileOutputStream("d:/student.xls"); 
		workbook.write(stream); 
	}

}

3、实现结果


原文地址:https://www.cnblogs.com/hzcya1995/p/13314337.html