java poi excel导出

/**
*
* @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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值