`
xu0213
  • 浏览: 8016 次
文章分类
社区版块
存档分类
最新评论

Java导出excel

 
阅读更多
public int createDataExcel(DataQueryParameters dataQueryParameters,String newfileName,String fileName,String[] headerKey, boolean showField ) throws IOException{
FileOutputStream out = null;
// String newfileName = fileName+"_"+ DateUtils.getTodayStr("yyyy-MM-dd")+".xlsx";
logger.info("开始进入数据文件导出********");
int recordCount = 0;
try {
//拼接临时文件保存地址
String flieAddress = excleAddress+newfileName;
logger.info("文件保存地址:"+flieAddress);
SXSSFWorkbook wb = new SXSSFWorkbook(Integer.valueOf(batchNum));//内存中保留 10000 条数据,以免内存溢出,其余写入 硬盘         
    Sheet sheet = wb.createSheet("0");
    sheet.setDefaultColumnWidth(18);
    wb.setSheetName(0, fileName);
//隐藏列:"MobileTel","Email"
    logger.info("开始根据权限得到对应的表头列*********");
List<String> headers = this.getHeadList(headerKey, showField);

//创建excle导出文件的表头列
logger.info("开始创建EXLCE表头列*************");
this.createExcleHeader(sheet, headers, wb);
long startTime = System.currentTimeMillis();
//组装Exlce体数据
logger.info("开始组装EXCLE数据信息**************");
recordCount = this.createExcleBody(dataQueryParameters, sheet, headers, wb);

logger.info("数据导出耗时:"+(System.currentTimeMillis() - startTime));
    out = new FileOutputStream(new File(flieAddress));
    wb.write(out); 
} catch (IOException e) {
e.printStackTrace();
logger.error("导出EXLCE数据异常",e);
}finally{
if(null != out){
out.close();
}
}
return  recordCount;
    }

if(dataCount > 0){
    for (int i = 0; i < listData.size(); i++) {
    Row datarow = sheet.createRow(sheetRowIndex+i);
    activityLeadsCommonModel = listData.get(i);
    for (int j = 0; j < headers.size(); j++) {
    Cell cell = datarow.createCell(j);  
cell.setCellType(Cell.CELL_TYPE_STRING);
cell.setCellValue(activityLeadsCommonModel.getValueByBeanName(headers.get(j)).toString());//写入内容 
    }
}
    }


private void createExcleHeader(Sheet sheet,List<String> headers,SXSSFWorkbook wb){
Row clumrow = sheet.createRow(0);
List<String> newCellList = ExportExcelUtil.replaceCellName(headers);
CellStyle  cell_Style = (CellStyle ) wb .createCellStyle();// 设置字体样式
cell_Style.setWrapText(true); // 设置为自动换行
cell_Style.setAlignment(CellStyle.ALIGN_CENTER);

//设置背景颜色
cell_Style.setFillForegroundColor(IndexedColors.BRIGHT_GREEN.getIndex());
cell_Style.setFillPattern(CellStyle.SOLID_FOREGROUND);
for (int m = 0; m < newCellList.size(); m++) {
Cell cell = clumrow.createCell(m);  
cell.setCellStyle(cell_Style);
cell.setCellValue(newCellList.get(m));//写入内容 
}
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics