1. 程式人生 > >實現一個配置簡單功能強大的excel工具類搞定excel匯入匯出(二)

實現一個配置簡單功能強大的excel工具類搞定excel匯入匯出(二)

上篇文章留了個小懸念,您是否有這樣的需求呢:
1.實體類中存放的值是一個編碼,而匯出的檔案中需要把編碼轉成有意義的文字.例如:實體類中性別用0,1表示,而希望匯出的excel檔案中是"男","女".
2.想對匯入的內容加一些邏輯,例如:判斷某些值不能為空.或判斷年齡不能小於0且不能大於100.

我們可以用一箇中間類來實現這個功能.

請看程式碼:

(1)我們程式中真正的實體類:

package com.tgb.lk.test04;

import java.util.Date;

public class Student {
	private int id;

	private String name;

	private int sex;

	private int clazz;

	private Date birthday;

	private String company;

	public int getId() {
		return id;
	}

   //get和set方法(略).....

	@Override
	public String toString() {
		return "Student [birthday=" + birthday + ", clazz=" + clazz
				+ ", company=" + company + ", id=" + id + ", name=" + name
				+ ", sex=" + sex + "]";
	}

}

(2)我們用於匯入匯出的excelVO類:

package com.tgb.lk.test04;

import com.tgb.lk.util.ExcelVOAttribute;

public class StudentVO {

	@ExcelVOAttribute(name = "姓名", column = "B", isExport = true, prompt = "姓名為必填項哦!")
	private String name;

	@ExcelVOAttribute(name = "性別", column = "C", combo = { "男", "女" })
	private String sex;

	@ExcelVOAttribute(name = "班級", column = "D", combo = { "五期提高班", "六期提高班",
			"七期提高班" })
	private String clazz;
	
	@ExcelVOAttribute(name="生日",column="E")
	private String birthday;

	@ExcelVOAttribute(name = "公司", column = "F")
	private String company;

	public String getName() {
		return name;
	}

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

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public String getClazz() {
		return clazz;
	}

	public void setClazz(String clazz) {
		this.clazz = clazz;
	}

	public String getCompany() {
		return company;
	}

	public void setCompany(String company) {
		this.company = company;
	}

	public String getBirthday() {
		return birthday;
	}

	public void setBirthday(String birthday) {
		this.birthday = birthday;
	}

	@Override
	public String toString() {
		return "StudentVO [birthday=" + birthday + ", clazz=" + clazz
				+ ", company=" + company + ", name=" + name + ", sex=" + sex
				+ "]";
	}
}

(3)匯出demo,注意convertStu2VO方法,這個方法中實現了我們匯出時把編碼轉成有意義的文字功能.

package com.tgb.lk.test04;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.tgb.lk.util.ExcelUtil;

/*
 * 使用步驟:
 * 1.新建一個類,例如StudentVO.
 * 2.設定哪些屬性需要匯出,哪些需要設定提示.
 * 3.設定實體資料
 * 4.呼叫exportExcel方法.
 * 本例向您提供以下問題的解決方案:
 * 1.實體物件存放的值需要轉換為其他文字的情況,例如:例項中有0,1表示男,女;而匯入匯出的excel中是中文的"男","女".
 * 2.實體物件的時間型別處理.
 * 
 */
public class ExportTest04 {
	public static void main(String[] args) {
		// 初始化資料
		List<Student> list = new ArrayList<Student>();

		Student stu = new Student();
		stu.setId(1);
		stu.setName("李坤");
		stu.setSex(0);
		stu.setClazz(5);
		stu.setCompany("天融信");
		stu.setBirthday(new Date());
		list.add(stu);

		Student stu2 = new Student();
		stu2.setId(2);
		stu2.setName("曹貴生");
		stu2.setSex(0);
		stu2.setClazz(5);
		stu2.setCompany("中銀");
		list.add(stu2);

		Student stu3 = new Student();
		stu3.setId(3);
		stu3.setName("李學宇");
		stu3.setSex(1);
		stu3.setClazz(6);
		list.add(stu3);

		FileOutputStream out = null;
		try {
			out = new FileOutputStream("d:\\success4.xls");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		ExcelUtil<StudentVO> util = new ExcelUtil<StudentVO>(StudentVO.class);
		util.exportExcel(convertStu2VO(list), "學生資訊", 60000, out);
		System.out.println("----執行完畢----------");
	}
	
	//將student物件轉換為studentVO用於匯出.
	private static List<StudentVO> convertStu2VO(List<Student> list) {
		List<StudentVO> list2 = new ArrayList<StudentVO>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/M/d HH:mm:ss");
		for (int i = 0; i < list.size(); i++) {
			StudentVO stuVo = new StudentVO();
			Student stu = list.get(i);
			stuVo.setName(stu.getName());
			switch (stu.getSex()) {
			case 0:
				stuVo.setSex("男");
				break;
			case 1:
				stuVo.setSex("女");
				break;
			default:
				break;
			}
			//處理時間
			if (stu.getBirthday() != null) {
				stuVo.setBirthday(sdf.format(stu.getBirthday()));
			}

			switch (stu.getClazz()) {
			case 5:
				stuVo.setClazz("五期提高班");
				break;
			case 6:
				stuVo.setClazz("六期提高班");
				break;
			case 7:
				stuVo.setClazz("七期提高班");
			default:
				break;
			}
			list2.add(stuVo);
		}

		return list2;
	}

}

(4)匯入demo,在convertStu2VO這個方法中我們可以加一些邏輯控制.

package com.tgb.lk.test04;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import com.tgb.lk.util.ExcelUtil;

public class ImportTest04 {
	public static void main(String[] args) {
		FileInputStream fis = null;
		try {
			fis = new FileInputStream("d:\\success4.xls");
			ExcelUtil<StudentVO> util = new ExcelUtil<StudentVO>(
					StudentVO.class);
			List<StudentVO> list = util.importExcel("學生資訊", fis);
			System.out.println(list);
			//輸出:[StudentVO [birthday=2012/9/22 15:44:10, clazz=五期提高班, company=null, name=李坤, sex=男], StudentVO [birthday=null, clazz=五期提高班, company=null, name=曹貴生, sex=男], StudentVO [birthday=null, clazz=六期提高班, company=null, name=李學宇, sex=女]]
			System.out.println(convertStu2VO(list));
			//輸出:[Student [birthday=Sat Sep 22 15:44:10 CST 2012, clazz=5, company=null, id=0, name=李坤, sex=0], Student [birthday=null, clazz=5, company=null, id=0, name=曹貴生, sex=0], Student [birthday=null, clazz=6, company=null, id=0, name=李學宇, sex=1]]
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}

	private static List<Student> convertStu2VO(List<StudentVO> list) {
		List<Student> retList = new ArrayList<Student>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/M/d HH:mm:ss");
		for (int i = 0; i < list.size(); i++) {
			StudentVO vo = list.get(i);
			Student student = new Student();
			student.setName(vo.getName());

			//在這個方法中還可以控制不允許為空,年齡值不能小於0後大於100等邏輯.
			
			String sex = vo.getSex();
			if (sex.equals("男")) {
				student.setSex(0);
			} else {
				student.setSex(1);
			}
			
			if (vo.getBirthday()!=null && !vo.getBirthday().trim().equals("")) {
				try {
					student.setBirthday(sdf.parse(vo.getBirthday()));
				} catch (ParseException e) {
					e.printStackTrace();
				}
			}

			String clazz = vo.getClazz();
			if (clazz != null) {
				if (clazz.equals("五期提高班")) {
					student.setClazz(5);
				} else if (clazz.equals("六期提高班")) {
					student.setClazz(6);
				} else if (clazz.equals("七期提高班")) {
					student.setClazz(7);
				} else {
					System.out.println("輸入的資料不合法");
				}
			}
			retList.add(student);
		}
		return retList;
	}
}


    限於本人水平有限,很多地方寫的並不完美,希望大家不吝賜教.如果覺得本文對您有幫助請頂支援一下,如果有不足之處歡迎留言交流,希望在和大家的交流中得到提高.