/**
*
* @param infomation 资料信息
* @return 资料文件名称 ,资料文件路径
* @throws FileNotFoundException
*/
@SuppressWarnings("unchecked")
public Map<String, String> infomationExport(List<Map<String, String>> infomation) {
HSSFWorkbook workbook = new HSSFWorkbook();
//存储文件内容的map
Map<String,String> fileMap= new HashMap<String,String>();
HSSFSheet sheet = workbook.createSheet(Constants.CONSTANT_EXCEL_SHEET_NAME);
File file = new File(Constants.CONSTANT_EXCEL03_TEMPLATE);
//将表头值存储文件
Map<String,String> headerMap= new HashMap<String,String>();
headerMap = getHeaderInfomation(infomation);
//创建第一行
HSSFRow firstRow = sheet.createRow(0);
int firstRowCellIndex = 0 ;
//存储表头的下标及key
Map<String,Short> headerMapCollection = new HashMap<String,Short>();
//循环第一行所有列
for (Iterator iterator = headerMap.entrySet().iterator(); iterator.hasNext();) {
Map.Entry<String, String> firstMap = (Map.Entry<String, String>) iterator.next();
HSSFCell firstCell = firstRow.createCell((short)firstRowCellIndex);
headerMapCollection.put(firstMap.getKey(), firstCell.getCellNum());
firstCell.setCellType(HSSFCell.CELL_TYPE_STRING);
firstCell.setEncoding(HSSFCell.ENCODING_UTF_16);
if(null == firstMap.getKey()){
firstCell.setCellValue("");
}
else{
firstCell.setCellValue(firstMap.getKey());
}
firstRowCellIndex ++;
}
/**
* 循环列表
*/
int rowIndex = 1;
for (Iterator iterator = infomation.iterator(); iterator.hasNext();) {
Map<String, String> map = (Map<String, String>) iterator.next();
//根据资料的行数创建资料文件行数
HSSFRow row = sheet.createRow(rowIndex);
//循环所有列
for (Iterator fileIterator = map.entrySet().iterator(); fileIterator.hasNext();) {
Map.Entry<String, String> rowMap = (Map.Entry<String, String>) fileIterator.next();
for (Iterator iteratorCell = headerMapCollection.entrySet().iterator(); iteratorCell.hasNext();) {
Map.Entry<String, Short> headerMapValue = (Map.Entry<String, Short>) iteratorCell.next();
if(null != rowMap.getKey() && null != headerMapValue.getKey()){
if(rowMap.getKey().equals(headerMapValue.getKey()) ){
saveInfomation(row, rowMap, headerMapValue);
}
}
else
{
if(rowMap.getKey()==headerMapValue.getKey() ){
saveInfomation(row, rowMap, headerMapValue);
}
}
}
}
rowIndex++;
}
FileOutputStream out;
try {
out = new FileOutputStream(file);
fileMap.put(file.getName(),file.getAbsolutePath());
workbook.write(out);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return fileMap;
}