`

poi导出excel

    博客分类:
  • Java
 
阅读更多
import java.util.Date;

public class Student
{
	private int id;
	private String name;
	private int age;
	private Date birth;

	public Student()
	{
	}

	public Student(int id, String name, int age, Date birth)
	{
		this.id = id;
		this.name = name;
		this.age = age;
		this.birth = birth;
	}

	public int getId()
	{
		return id;
	}

	public void setId(int id)
	{
		this.id = id;
	}

	public String getName()
	{
		return name;
	}

	public void setName(String name)
	{
		this.name = name;
	}

	public int getAge()
	{
		return age;
	}

	public void setAge(int age)
	{
		this.age = age;
	}

	public Date getBirth()
	{
		return birth;
	}

	public void setBirth(Date birth)
	{
		this.birth = birth;
	}

}

 

import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

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;

public class ExportToExcel {
	/**
	 * @功能:手工构建一个简单格式的Excel
	 */
	private static List<Student> getStudent() throws Exception {
		List<Student> list = new ArrayList<Student>();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-mm-dd");

		Student user1 = new Student(1, "张三", 16, df.parse("1997-03-12"));
		Student user2 = new Student(2, "李四", 17, df.parse("1996-08-12"));
		Student user3 = new Student(3, "王五", 26, df.parse("1985-11-12"));
		Student user4 = new Student(4, "王6", 23, df.parse("1987-11-12"));
		list.add(user1);
		list.add(user2);
		list.add(user3);
		list.add(user4);

		return list;
	}
	
	public static void writeDataToExcel(String[] heads, String filePath,String sheetName) throws Exception{
		// 第一步,创建一个webbook,对应一个Excel文件
		HSSFWorkbook wb = new HSSFWorkbook();
		// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
		HSSFSheet sheet = wb.createSheet(sheetName);
		// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
		HSSFRow row = sheet.createRow(0);
		// 第四步,创建单元格,并设置值表头 设置表头居中
		HSSFCellStyle style = wb.createCellStyle();
		style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
		
		HSSFCell cell = null;
		int line=0;
		for(String head:heads){
			cell = row.createCell(line++);
			cell.setCellValue(head);
			cell.setCellStyle(style);
		}
		
		// 第五步,写入实体数据 实际应用中这些数据从数据库得到,
		List<Student> list = ExportToExcel.getStudent();
		int count = 0;
		for (Student student : list) {
			row = sheet.createRow(++count);
			// 第四步,创建单元格,并设置值
			row.createCell(0).setCellValue((double) student.getId());
			row.createCell(1).setCellValue(student.getName());
			row.createCell(2).setCellValue((double) student.getAge());
			cell = row.createCell(3);
			cell.setCellValue(new SimpleDateFormat("yyyy-mm-dd").format(student
					.getBirth()));
		}
		// 第六步,将文件存到指定位置
		try {
			File file = new File(filePath);
			if(file.exists()){
				file.delete();
			}
			FileOutputStream fout = new FileOutputStream(file);
			wb.write(fout);
			fout.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) throws Exception {
		String[] heads = new String[]{"学号","姓名","年龄","出生日期"};
		writeDataToExcel(heads, "E:/students.xls","学生表一");
	}
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics