日期:2014-05-16  浏览次数:20597 次

java poi 创建excel 兼容2003\2007 模拟数据库导出 解决自动调整宽度、日期格式
必须依赖poi包




结果:



package best.utils;

import java.util.Map;

import org.apache.poi.ss.usermodel.Workbook;

public interface ExcelTemplate {
	
	public final static String EXCEL_03="2003";
	
	public final static String EXCEL_07="2007";
	
	/**
	 * 可选
	 * 设置excel日期样式
	 * @param dataFormat
	 */
	public void setDateFormat(String dataFormat);
	/**
	 * 可选
	 * 设置工作薄名称
	 * @param sheetName
	 */
	public void setSheetName(String sheetName) ;
	/**
	 * 可选
	 * 是否自动调整列宽
	 * @param b
	 */
	public void isAutoSizeColumn(boolean b);
	/**
	 * 转换 中应为呼唤
	 * @param mess
	 */
	public void setMess(Map<String, String> mess);
	/**
	 * 同时创建excel 03、07版
	 * @return key=2003或2007 对应为excel2003、excel2007
	 */
	public Map<String,Workbook> createExcel();
	
	/**
	 * 创建excel 具体版
	 * @param excelVerson 2003或2007
	 * @return 
	 */
	public Workbook createExcel(String excelVerson);
	
}



package best.utils;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
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.xssf.usermodel.XSSFWorkbook;

public class ExcelTemplateImpl implements ExcelTemplate {
	
	private List<Map<String, Object>> datas;
	
	private final Map<String,Workbook> wbmap = new HashMap<String, Workbook>(2);
	
	private String dataFormat = "yyyy-MM-dd";
	
	private String sheetName;
	
	private boolean isAutoSizeColumn=false;
	
	private Map<String,String> mess;
	
	
	public ExcelTemplateImpl(List<Map<String, Object>> datas){
		init(datas);
	}
	
	/**
	 * 初始化
	 * @param datas
	 */
	private void init(List<Map<String, Object>> datas){
		this.datas = datas;
		//构造2003、2007版excel
		wbmap.put(ExcelTemplate.EXCEL_03, new HSSFWorkbook());
		wbmap.put(ExcelTemplate.EXCEL_07, new XSSFWorkbook());

	}
	
	public void setDateFormat(String dataFormat){
		
		this.dataFormat = dataFormat;
	}
	
	public Workbook createExcel(String excelVerson){
		Workbook wb = wbmap.get(excelVerson);
		initExcel(wb);
		return wb;
	}

	public Map<String,Workbook> createExcel(){

		Set<Entry<String, Workbook>> wbset = wbmap.entrySet();
		Iterator<Entry<String, Workbook>> eit = wbset.iterator();
		
		while(eit.hasNext()){
			Entry<String, Workbook> workEntry = eit.next();
			Workbook wb = workEntry.getValue();
			initExcel(wb);
		}
		return wbmap;
	}
	
	private void initExcel(Workbook wb){
		//设置样式
		CellStyle cellStyle = wb.createCellStyle();//创建样式
		cellStyle.setDataFormat(wb.createDataFormat().getFormat(dataFormat));//日期格式
		cellStyle.setAlignment(CellStyle.ALIGN_CENTER);//居中
		
		//创建一个新的表\并创建名称
		Sheet s = null==sheetName?s = wb.createSheet():wb.createSheet(sheetName);
		
		
		for(int i=0; i<datas.size(); i++){ //遍历数据
			
			Map<String,Object> map = datas.get(i);
			
			//定义列(列名第一行)
			Row titleColl = s.createRow(0);
	
			//定义行(内容第二行起)
			Row row = s.createRow(i+1);
			
			Set<Entry<String, Object>> set = map.entrySet();
			
			Iterator<Entry<String, Object>> it = set.iterator();
			int x = 0;
			while(it.hasNext()){
				//列名
				Entry<String, Object> entry = it.next();
				Cell titleCell = titleColl.createCell(x);
				String titleName = entry.getKey();
				titleCell.setCellValue(null==mess||null==mess.get(titleName)?titleName:mess.get(titleName));
				//设置列名样式
				titleCell.setCellStyle(cellStyle);
				//内容