java的excel導出[poi]

Excel生成的主要的類

 

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.formula.functions.T;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;

import com.hnair.callcenter.common.StringUtil;

/**
 * 創建Excel
 * 
 * @author [email protected]
 * 
 */
@SuppressWarnings("all")
public class CreateExcel {

	private static CreateExcel ce = new CreateExcel();
	private HSSFWorkbook wb = null;
	private Sheet sheet = null;

	public final static String DATA_START = "data";
	/* 初始化列數 */
	private int initColIndex;
	/* 初始化行數 */
	private int initRowIndex;
	/* 當前列數 */
	private int curColIndex;
	/* 當前行數 */
	private int curRowIndex;
	/* 最後一行的數據 */
	private int lastRowIndex;

	private Row curRow;

	private CellStyle style;

	private CreateExcel() {
	}

	public static CreateExcel getInstance() {
		return ce;
	}

	public CreateExcel initDataNoTemplate() {
		// 沒有模板時初始化創建
		wb = new HSSFWorkbook();
		sheet = this.wb.createSheet("sheet1");
		initRowIndex = 0;
		initColIndex = 0;
		initStyle();
		return this;
	}
	
	public CreateExcel initStyle(){
		// 初始化樣式
		style = wb.createCellStyle();
		style.setAlignment(CellStyle.ALIGN_CENTER); // 居中
		return this;
	}

	/**
	 * 初始化當前的行列
	 * @return
	 */
	private CreateExcel initConfigDataNoTemplate() {
		curRowIndex  = initRowIndex;
		curColIndex= initColIndex ;
		return this;
	}

	/**
	 * 創建新的行
	 * 
	 * @return
	 */
	private CreateExcel createRow() {
		curColIndex = initColIndex;
		curRow = this.sheet.createRow(curRowIndex);
		curRowIndex++;
		return this;
	}

	/**
	 * 創建表頭
	 * 
	 * @param title
	 *            表頭內容
	 * @param colSpan
	 *            表頭寬度(佔據的單元格的列的數量)
	 * @return
	 */
	public CreateExcel createTitle(String title, int colSpan) {
		createRow();
		sheet.addMergedRegion(new CellRangeAddress(0, 1, 0, colSpan));
		// 頭部佔據2行所以要繼續加一行
		curRowIndex++;
		Cell c = curRow.createCell(curColIndex);
		c.setCellValue(title);
		// 重置樣式防止出現樣式不對
		CellStyle style = wb.createCellStyle();
		// 居中
		style.setAlignment(CellStyle.ALIGN_CENTER);
		Font f = wb.createFont();
		// 字體
		f.setFontHeightInPoints((short) 14);
		style.setFont(f);
		c.setCellStyle(style);
		return this;
	}

	/**
	 * 新建表頭
	 * 
	 * @param head
	 *            包含表頭字符串的集合
	 * @return
	 */
	public CreateExcel createHead(List<String> head) {
		createRow();
		Cell c = null;
		for (String query : head) {
			c = curRow.createCell(curColIndex);
			curColIndex++;
			this.setCellValue(String.class, query, c);
			c.setCellStyle(style);
		}
		return this;
	}

	/**
	 * 表的內容
	 * 
	 * @param body
	 *            包含行數據的集合
	 * @return
	 */
	public CreateExcel createBody(List<?> body) {
		for (Object t : body) {
			createRow();
			Map<String, ColProperty> indexMap = findBeanProperty(t.getClass());
			Method m = null;
			try {
				for (int i = 0; i < indexMap.size(); i++) {
					Iterator<String> it = indexMap.keySet().iterator();
					while (it.hasNext()) {
						String key = it.next();
						ColProperty value = indexMap.get(key);
						// 從1開始查找字段
						if (value.getIndex() != (i + 1)) {
							continue;
						}
						// 得到get方法
						m = t.getClass().getMethod(
								"get" + StringUtil.toUpperCaseFirstOne(key),
								null);
						Class<?> clazz = value.getType();
						// 反射得到值
						Object oo = m.invoke(t, null);
						
						setCellValue(clazz, oo,createCell());
					}
				}
			} catch (SecurityException e) {
				e.printStackTrace();
			} catch (NoSuchMethodException e) {
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			}

		}
		return this;
	}
	
	public CreateExcel createBodyCoalition(List<?> body) {
		
		if(null == body||body.size() == 0){
			throw new RuntimeException("表內容不能爲空!");
		}
		
		for (Object t : body) {
			curColIndex = 0;
			createRow();
			Method m = null;
			try {
				m = t.getClass().getMethod("getL",null);
				Object oo = m.invoke(t, null);
				List<Object> ol = (List)oo;

				Field fs[] = t.getClass().getDeclaredFields();
				for (Field f : fs) {
					String methodName = f.getName().substring(0, 1).toUpperCase()
							+ f.getName().substring(1);
					if(f.getType() != java.util.List.class){
						m = t.getClass().getDeclaredMethod("get" + methodName);
						Object oper = m.invoke(t,null);
						System.out.println(oper);
						setCellValue(f.getType(), oper, createCell(ol.size()-1, 1));
					}
				}
				for(int i = 0;i<ol.size();i++){
					if(i != 0){
						createRow();
						curColIndex = 3;
					}
					
					Field fields[] = ol.get(i).getClass().getDeclaredFields();
					for (Field field : fields) {
						String methodNameL = field.getName().substring(0, 1).toUpperCase()
								+ field.getName().substring(1);
						m = ol.get(i).getClass().getDeclaredMethod("get" + methodNameL);
						Object value = m.invoke(ol.get(i), null);
						setCellValue(value.getClass(), value, createCell());
					}
					System.out.println("");
				}
			} catch (SecurityException e) {
				e.printStackTrace();
			} catch (NoSuchMethodException e) {
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			}
		}
		return this;
	}
	
	public CreateExcel createBodyCoalition(Map<String, Map<String, List<List<Object>>>> result) {
		
		if(result == null || result.size() == 0)
			throw new RuntimeException("表內容不能爲空!");
		Iterator<String> resultIt = result.keySet().iterator();
		while(resultIt.hasNext()){
			curColIndex = 0;
			createRow();
			String resultKey = resultIt.next();
			Map<String, List<List<Object>>> map = result.get(resultKey);
			setCellValue(String.class, resultKey, createCell(countMapList(map)-1, 1));
			boolean isSecondColFirst = true;
			
			Iterator<String> mapIt = map.keySet().iterator();
			while(mapIt.hasNext()){
				if(!isSecondColFirst){
					createRow();
					curColIndex = 1;
				}
				isSecondColFirst = false;
				String mapKey = mapIt.next();
				List<List<Object>> l = map.get(mapKey);
				setCellValue(String.class, mapKey, createCell(l.size()-1, 1));
				
				for(int i=0;i<l.size();i++){
					if(i!=0){
						createRow();
						curColIndex = 2;
					}
					for(int j=0;j<l.get(i).size();j++){
						setCellValue(String.class, l.get(i).get(j), createCell());
					}
				}
			}
		}
		return this;
	}
	
	private int countMapList(Map<String, List<List<Object>>> map){
		int count = 0;
		Iterator<String> mapIt = map.keySet().iterator();
		while(mapIt.hasNext()){
			count += map.get(mapIt.next()).size();
		}
		return count;
	}

	/**
	 * 創建新內容
	 * 
	 * @param clazz
	 *            單元格內容的類型
	 * @param value
	 *            單元格內容的數據
	 * @return
	 */
	public CreateExcel setCellValue(Class<?> clazz, Object value,Cell c) {
		if(null == value){
			c.setCellValue("");
			return this;
		}
		
		// 根據數據的類型set值
		if (clazz == java.lang.String.class) {
			c.setCellValue(value.toString());
		} else if (clazz == java.lang.Integer.class || clazz == int.class
				|| clazz == java.lang.Double.class || clazz == double.class
				|| clazz == java.lang.Long.class || clazz == long.class
				|| clazz == java.lang.Short.class || clazz == short.class
				|| clazz == java.lang.Float.class || clazz == float.class) {
			c.setCellValue(Double.parseDouble(value.toString()));
		} else if (clazz == java.lang.Boolean.class || clazz == boolean.class) {
			c.setCellValue(Boolean.parseBoolean(value.toString()));
		} else if (clazz == java.util.Date.class) {
			CreationHelper createHelper = wb.getCreationHelper(); 
			if(value.toString().length()>=8&&value.toString().length()<=10)
				this.style.setDataFormat(createHelper.createDataFormat().getFormat("yyyy-MM-dd"));
			else
				this.style.setDataFormat(createHelper.createDataFormat().getFormat("yyyy-MM-dd hh:mm:ss"));
			c.setCellValue((Date)value);
		} else {
			throw new RuntimeException("setCellValue:未知的參數類型!");
		}
		c.setCellStyle(this.style);
		initStyle();
		return this;
	}

	/**
	 * 創建新的單元格(合併單元格)
	 * @param rowSpan
	 * 		  跨行數
	 * @param colSpan
	 * 	 	  跨列數
	 * @return
	 */
	public Cell createCell(int rowSpan,int colSpan){
		Cell c = curRow.createCell(curColIndex); 
		sheet.addMergedRegion(new CellRangeAddress(curRowIndex-1, curRowIndex+rowSpan-1, curColIndex, curColIndex+colSpan-1));
		curColIndex+=colSpan;
		c.setCellStyle(style);
		return c;
	} 
	
	/**
	 * 創建新的單元格
	 * @return
	 */
	public Cell createCell(){
		Cell c = curRow.createCell(curColIndex); 
		curColIndex++;
		return c;
	}
	
	/**
	 * 得到實體的帶有註解的字段的內容
	 * 
	 * @param class1
	 * @return
	 */
	private Map<String, ColProperty> findBeanProperty(Class<?> clazz) {
		Field fs[] = clazz.getDeclaredFields();
		Map<String, ColProperty> indexMap = new HashMap<String, ColProperty>();
		for (Field f : fs) {
			// 尋找有註解的字段
			TabCol tabCol = f.getAnnotation(TabCol.class);
			if (null == tabCol) {
				continue;
			} else {
				ColProperty colProperty = new ColProperty(f.getType(),
						tabCol.Index(), tabCol.title());
				indexMap.put(f.getName(), colProperty);
			}
		}

		return indexMap;
	}

	/**
	 * 得到表頭的集合
	 * @param map
	 * @return
	 */
	private List<String> getHead(Map<String, ColProperty> map) {
		List<String> heads = new ArrayList<String>();
		
		for(int i=0;i<map.size();i++){
			Iterator<String> it = map.keySet().iterator();
			while (it.hasNext()) {
				String key = it.next();
				ColProperty value = map.get(key);
				if(value.getIndex() != (i+1)){
					continue;
				}
				heads.add(value.getTitle());
			}
		}
		return heads;
	}

	/**
	 * 創建統計數據
	 * @param t
	 * 		包含統計數據的實體
	 * @return
	 */
	public CreateExcel createCounts(Object t) {
		createRow();
		Map<String, CountProperty> map = getCountProperty(t.getClass());
		Method m = null;
		for (int i = 0; i < map.size(); i++) {
			Iterator<String> it = map.keySet().iterator();
			while (it.hasNext()) {
				String key = it.next();
				//查找註解字段
				CountProperty countProperty = map.get(key);
				if (countProperty.getIndex() != (i + 1)) {
					continue;
				}
				try {
					m = t.getClass().getMethod(
							"get" + StringUtil.toUpperCaseFirstOne(key), null);
					Class<?> clazz = countProperty.getType();
					Object oo;
					//反射得到值
					oo = m.invoke(t, null);
					if (countProperty.getColSpan() == 1) {
						this.setCellValue(clazz, oo,createCell());
					} else {
						// 根據註解判斷是否合併單元格
						if (countProperty.getIsCoalition()) {
							setCellValue(clazz, oo, createCell(0, countProperty.getColSpan()));
						} else {
							setCellValue(clazz, oo,createCell());
							//填充空的單元格
							for (int j = 0; j < countProperty.getColSpan() - 1; j++) {
								setCellValue(String.class, "",createCell());
							}
						}
					}
				} catch (SecurityException e) {
					e.printStackTrace();
				} catch (NoSuchMethodException e) {
					e.printStackTrace();
				} catch (IllegalArgumentException e) {
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					e.printStackTrace();
				}

			}
		}

		return this;
	}

	/**
	 * 處理統計數據的屬性,得到值
	 * @param clazz
	 * @return
	 */
	public Map<String, CountProperty> getCountProperty(Class<?> clazz) {
		Field fs[] = clazz.getDeclaredFields();
		Map<String, CountProperty> indexMap = new HashMap<String, CountProperty>();
		for (Field f : fs) {
			// 尋找有註解的字段
			CountData countData = f.getAnnotation(CountData.class);
			if (null == countData) {
				continue;
			} else {
				CountProperty countProperty = new CountProperty(f.getType(),
						countData.index(), countData.colSpan(),
						countData.isCoalition());
				indexMap.put(f.getName(), countProperty);
			}
		}
		return indexMap;
	}

	/**
	 * 生成Excel文件
	 * @param filePath
	 */
	public void writeToFile(String filePath) {
		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream(new File(filePath));
			wb.write(fos);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 返回workbook對象
	 * @return
	 */
	public Workbook getWorkbook() {
		return wb;
	}

	/**
	 * 生成帶有統計數據的excel
	 * @param title
	 * 		  表頭數據
	 * @param body
	 * 		  表的內容-->帶有註解的泛型實體  註解見:com.hnair.callcenter.common.excel.TabCol
	 * @param t
	 * 		統計數據
	 * @return
	 */
	public CreateExcel CreateNewExcelNoTemplate(String title, List<?> body, Object t) {
		if (null == title || "".equals(title)) {
			throw new RuntimeException("表頭不能爲空!");
		}
		if (null == body || body.size() == 0) {
			throw new RuntimeException("表內容爲空!");
		}
		if (null == t) {
			throw new RuntimeException("統計數據爲空!");
		}
		initDataNoTemplate();
		initConfigDataNoTemplate();
		List<String> heads = getHead(findBeanProperty(body.get(0).getClass()));
		createTitle(title, heads.size()-1);
		createHead(heads);
		createBody(body);
		createCounts(t);
		return this;
	};

	public CreateExcel CreateNewExcelNoTemplate(String title, List<?> body) {
		if (null == title || "".equals(title)) {
			throw new RuntimeException("表頭不能爲空!");
		}
		if (null == body || body.size() == 0) {
			throw new RuntimeException("表內容爲空!");
		}
		initDataNoTemplate();
		initConfigDataNoTemplate();
		List<String> heads = getHead(findBeanProperty(body.get(0).getClass()));
		createTitle(title, heads.size());
		
		createHead(heads);
		createBody(body);
		return this;
	};

	public CreateExcel CreateNewExcelWithTemplate(String title, List<?> body,
			Object o, String filePath) {
		if (null == title || "".equals(title)) {
			throw new RuntimeException("表頭不能爲空!");
		}
		if (null == body || body.size() == 0) {
			throw new RuntimeException("表內容爲空!");
		}
		if (null == o) {
			throw new RuntimeException("統計數據爲空!");
		}
		readTemplate(filePath);
		initConfigData();
		List<String> heads = getHead(findBeanProperty(body.get(0).getClass()));
		createTitle(title, heads.size()-1);
		createHead(heads);
		createBody(body);
		createCounts(o);
		return this;
	}

	public CreateExcel CreateNewExcelWithTemplate(String title, List<?> body,
			String filePath) {
		if (null == title || "".equals(title)) {
			throw new RuntimeException("表頭不能爲空!");
		}
		if (null == body || body.size() == 0) {
			throw new RuntimeException("表內容爲空!");
		}
		readTemplate(filePath);
		initConfigData();
		List<String> heads = getHead(findBeanProperty(body.get(0).getClass()));
		createTitle(title, heads.size()-1);
		createHead(heads);
		createBody(body);
		CreateNewExcelNoTemplate(title, body);
		return this;
	}

	/**
	 * 讀取表頭模板
	 * 
	 * @param filePath
	 * @return
	 */
	public CreateExcel readTemplate(String filePath) {
		try {
			wb = new HSSFWorkbook(new FileInputStream(new File(filePath)));
			sheet = wb.getSheetAt(0);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			throw new RuntimeException("未找到相關模板!");
		} catch (IOException e) {
			e.printStackTrace();
		}
		return this;
	}

	/**
	 * 查找數據的起始標記
	 */
	@SuppressWarnings("static-access")
	private void initConfigData() {
		boolean findData = false;
		for (Row row : sheet) {
			if (findData)
				break;
			for (Cell cell : row) {
				if (cell.getCellType() != cell.CELL_TYPE_STRING)
					continue;
				String str = cell.getStringCellValue();
				// DATA_START就是默認的起始標記
				if (str.equals(DATA_START)) {
					initColIndex = cell.getColumnIndex();
					initRowIndex = row.getRowNum();
					// 初始化數據
					curColIndex = initColIndex;
					curRowIndex = initRowIndex;
					findData = true;
					break;
				}
			}
		}
	}
}


封裝的實體

 

 

public class ColProperty {
	private Class<?> type;
	private Integer index;
	private String title;

	public ColProperty() {
	}

	public ColProperty(Class<?> type, Integer index, String title) {
		this.type = type;
		this.index = index;
		this.title = title;
	}

	public Class<?> getType() {
		return type;
	}

	public void setType(Class<?> type) {
		this.type = type;
	}

	public Integer getIndex() {
		return index;
	}

	public void setIndex(Integer index) {
		this.index = index;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

}

 

 

 

public class CountProperty {

	private int index;
	private int colSpan;
	private Class<?> type;
	private boolean isCoalition;

	public CountProperty() {
	}

	public CountProperty(Class<?> type, int index, int colSpan, boolean isCoalition) {
		this.type = type;
		this.index = index;
		this.colSpan = colSpan;
		this.isCoalition = isCoalition;
	}

	public Class<?> getType() {
		return type;
	}

	public void setType(Class<?> type) {
		this.type = type;
	}

	public int getIndex() {
		return index;
	}

	public void setIndex(int index) {
		this.index = index;
	}

	public int getColSpan() {
		return colSpan;
	}

	public void setColSpan(int colSpan) {
		this.colSpan = colSpan;
	}

	public boolean getIsCoalition() {
		return isCoalition;
	}

	public void setIsCoalition(boolean isCoalition) {
		this.isCoalition = isCoalition;
	}
}



 


CountData統計字段的實體註解信息(一般統計有合併單元格的操作)

 

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)
public @interface CountData {
	int index();
	int colSpan();
	boolean isCoalition();
}


表內容對應的實體註解的信息

 

 

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)
/**
 * 表內容對應的實體的信息
 * @author [email protected]
 *
 */
public @interface TabCol {
	/**
	 * 字段在表中列的順序 1 開始
	 * @return
	 */
	int Index();
	
	/**
	 * 字段在表頭
	 * @return
	 */
	String title();
}



 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章