代码改变世界

java poi 导出Excel文件

2016-10-07 12:55  深蓝大道  阅读(316)  评论(0编辑  收藏  举报

1,导包  poi-3.9-XXX.JAR

2, 创建一个实体对象

public class Student implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private int id;
	private String name;
	private int age;
	private Date borth;
	
	public Student(int id, String name, int age, Date borth){
		this.id = id;
		this.name = name;
		this.age = age;
		this.borth = borth;
	}

	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 getBorth() {
		return borth;
	}

	public void setBorth(Date borth) {
		this.borth = borth;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

3,创建实体数据,也可以获取数据库的信息

public class StuData  {
	
	public static List<Map<String, Object>> getStuInfo() throws ParseException {
		List<Map<String, Object>> listStuInfo = new ArrayList<Map<String, Object>>();
		List<Student> listStu = new ArrayList<Student>();
		DateFormat format = new SimpleDateFormat("yyyy-mm-dd");

		Student s1 = new Student(1, "zhangsan", 16, format.parse("1987-05-06"));
		Student s2 = new Student(2, "li", 17, format.parse("1988-05-06"));
		Student s3 = new Student(3, "wangwu", 18, format.parse("1989-05-06"));
		Student s4 = new Student(4, "zhaoliu", 19, format.parse("1990-05-06"));
		listStu.add(s1);
		listStu.add(s2);
		listStu.add(s3);
		listStu.add(s4);
		for (Student stu : listStu) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("id", stu.getId());
			map.put("name", stu.getName());
			map.put("age", stu.getAge());
			map.put("borth", stu.getBorth());
			listStuInfo.add(map);
		}
		return listStuInfo;
	}

}

4, 创建表头,以及单元格的样式等

private static Map<String, Object> createHeaderInfo(Workbook wb, Sheet sheet, int headerNumber) {
		Row row = sheet.createRow(headerNumber);
		Map<String, Object> header = createHeader();
		for(String str : header.keySet()) {
			int rowNumber = (int) header.get(str);
			Cell cell = row.createCell(rowNumber);
			CellStyle cellstyle = wb.createCellStyle();
			cellstyle.setAlignment(CellStyle.ALIGN_CENTER);
			Font font = wb.createFont();
			font.setBoldweight(Font.BOLDWEIGHT_BOLD);
			cellstyle.setFont(font);
			cell.setCellStyle(cellstyle);
			cell.setCellValue(str);
		}
		headerNumber++;
		return header;
	}

5,表头信息:

private static Map<String, Object> createHeader(){
		Map<String, Object> header = new HashMap<String, Object>();
		header.put("id", 0);
		header.put("name", 1);
		header.put("age", 2);
		header.put("borth", 3);
		return header;
	}

6,创建Excel对象,创建sheet页签,创建行,创建每行的单元格

public static void main(String[] args) throws ParseException, IOException {
		Workbook wb = new HSSFWorkbook();
		Sheet sheet = wb.createSheet("StuInfo");
		//创建表头
		int rowNumber = 0 ;
		Map<String, Object> header = createHeaderInfo(wb, sheet, rowNumber);
		List<Map<String, Object>> listStuInfo = StuData.getStuInfo();
		for(Map<String, Object> stuMap : listStuInfo) {
			rowNumber++;
			Row row = sheet.createRow(rowNumber);
			Iterator<Entry<String, Object>> iterator = header.entrySet().iterator();
			while(iterator.hasNext()){
				Entry<String, Object> entry = iterator.next();
				String headerCell = entry.getKey();
				int cellNumber = (int) entry.getValue();
				Cell cell = row.createCell(cellNumber);
				CellStyle cellstyle = wb.createCellStyle();
				cellstyle.setAlignment(CellStyle.ALIGN_CENTER);
				cell.setCellStyle(cellstyle);
				Object value = stuMap.get(headerCell);
				if(value instanceof String) {
					cell.setCellValue((String)value);
				}else if(value instanceof Date){
					cell.setCellValue(((Date) value).toLocaleString());
				}else if(value instanceof Integer){
					cell.setCellValue((Double.valueOf(value.toString())));
				}
				
			}
		}
		
		FileOutputStream fos = new FileOutputStream("E:/studentInfo.xlsx");
		wb.write(fos);
		fos.flush();
		fos.close();
		System.out.println("OK");

	}