ExcelTemplateImpl

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.HSSFFont;
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.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.xssf.usermodel.XSSFWorkbook;





public class ExcelTemplateImpl implements ExcelTemplate {

private final Map<String, Workbook> wbmap = new HashMap<String, Workbook>(2);

private List<Map<String, Object>> datas;
private String dataFormat = "yyyy-MM-dd";
private String sheetName;
private boolean isAutoSizeCoumn = 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 dateFormat) {
this.dataFormat = dateFormat;
}

/**
* 设置表单名称。
*/
public void setSheetName(String sheetName) {
this.sheetName = sheetName;
}

/**
* 是否自动调整列宽。
*/
public void isAutoSizeColumn(boolean b) {
this.isAutoSizeCoumn = b;

}

/**
* 转换中应为呼唤
*
* @param mess
*/
public void setMess(Map<String, String> mess) {
this.mess = mess;

}

/**
* 同时创建03、07版
*
* @return key=2003或key=2007 对应为excel2003、excel2007
*/
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;
}

/**
* 创建Excel具体版
*
* @param excelVersion
*            2003或2007
* @return
*/
public Workbook createExcel(String excelVersion) {
Workbook wb = wbmap.get(excelVersion);
initExcel(wb);
return wb;
}

/**
* @param workbook
*/
private void initExcel(Workbook workbook) {
// 设置样式
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setDataFormat(workbook.createDataFormat().getFormat(
dataFormat));// 日期格式
cellStyle.setAlignment(CellStyle.ALIGN_CENTER);// 居中
cellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);

//设置单元格字体
Font  font = workbook.createFont();
font.setColor(HSSFFont .COLOR_RED);
cellStyle.setFont(font);

// 创建一个新的表、并创建 名称
Sheet sheet = null;
if (null == sheetName || "".equals(sheetName)) {
sheet = workbook.createSheet();
} else {
sheet = workbook.createSheet(sheetName);
}

for (int i = 1; i < datas.size(); i++) {
Map<String, Object> map = datas.get(i);// 每一个map是一条记录

// 定义行(列名第一行)
Row titleRow = sheet.createRow(0);
titleRow.setHeight((short) 500);// 设置表头行的高度
Row row = sheet.createRow(i);
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 = titleRow.createCell(x);
String titleName = entry.getKey();
if (null == mess || null == mess.get(titleName)) {
titleCell.setCellValue(titleName);
} else {
titleCell.setCellValue(mess.get(titleName));
}
titleCell.setCellStyle(cellStyle);// 设置表头行的样式
// 设置列名样式
Cell cell = row.createCell(x);
cell.setCellStyle(cellStyle);
Object value = entry.getValue();

// 处理单元格数据
if (value instanceof String) {
cell.setCellValue(value.toString());
} else if (value instanceof Double) {
cell.setCellValue((Double) value);
} else if (value instanceof Integer) {
cell.setCellValue((Integer) value);
} else if (value instanceof Float) {
cell.setCellValue((Float) value);
} else if (value instanceof Boolean) {
cell.setCellValue((Boolean) value);
} else if (value instanceof java.util.Date
| value instanceof java.sql.Date) {
cell.setCellValue((Date) value);
}
x++;
}
// 自动调整列宽
if (isAutoSizeCoumn) {
sheet.autoSizeColumn(i);
}
}
}

public List<Map<String, Object>> getDatas() {
return datas;
}

public void setDatas(List<Map<String, Object>> datas) {
this.datas = datas;
}

public String getSheetName() {
return sheetName;
}

public Map<String, String> getMess() {
return mess;
}

}

你可能感兴趣的