java之poiEXCEL操作2014-10-20 16:43:28

( 还没有投票,继续加油! )
分享:
31.3K

部分代码:

**
 * 创建标准模板
 * @param request
 * @param response
 * @throws Exception
 */
private void createEXCELTemplet(HttpServletRequest request,
HttpServletResponse response) throws Exception {
HSSFWorkbook hssfworkbook = new HSSFWorkbook();
HSSFSheet hssfsheet = hssfworkbook.createSheet();
String downloadFileName = sellActivityTitle;
createSheetTableTile(hssfsheet,ExcelConstantTitle.sellActivityTitleMap,TEMPLET_CELL_COUNT);
downloadEXCEL(hssfworkbook, downloadFileName, request, response);
}
/**
 * 提供下载
 * @param hssfworkbook
 * @param downloadFileName
 * @param request
 * @param response
 * @throws Exception
 */
private void downloadEXCEL(HSSFWorkbook hssfworkbook,
String downloadFileName, HttpServletRequest request,
HttpServletResponse response) throws Exception {
try {
//设置response,下载附件头设置
response.setContentType("application/vnd.ms-excel");
response.setHeader("content-disposition",
"attachment;filename=\""
+ new String(downloadFileName.getBytes("GBK"),
"ISO-8859-1") + "\";size="
+ hssfworkbook.getBytes().length);
OutputStream out = response.getOutputStream();
//将工作簿输出到response
hssfworkbook.write(out);
out.flush();
out.close();
} catch (Exception e) {
throw new Exception(e);
}
}
   
/**
 * 创建校验数据之后的模板
 */
private void createValidateExcelAfterTemplet(HttpServletRequest request,
HttpServletResponse response,List list) throws Exception {
HSSFWorkbook hssfworkbook = new HSSFWorkbook();
HSSFSheet hssfsheet = hssfworkbook.createSheet();
// 创建正文
String downloadFileName = validateSellActivityAfterTitle;
createSheetTableTile(hssfsheet,ExcelConstantTitle.sellActivityTitleMap,VALIDATE_AFTER_CELL_COUNT);
//添加数据
addSheetContentList(hssfsheet,ExcelConstantTitle.sellActivityContentMap,list);
downloadEXCEL(hssfworkbook, downloadFileName, request, response);
}
/**
 * 创建EXCEL表头
 * @param hssfsheet
 * @param titleMap
 * @param size
 * @return
 */
private HSSFSheet createSheetTableTile(HSSFSheet hssfsheet,Map titleMap,int size){
// 创建正文
HSSFRow hssfrow = hssfsheet.createRow(0);
for (int i = 0; i < size; i++) {
// 设置列宽度
hssfsheet.setColumnWidth((short) i, (short) 4000);
HSSFCell hssfcell = hssfrow.createCell((short) i);
hssfcell.setEncoding(HSSFWorkbook.ENCODING_UTF_16);
switch (i) {
case 0:
hssfcell.setCellValue((String)titleMap.get(String.valueOf(i)));
break;
case 1:
hssfcell.setCellValue((String)titleMap.get(String.valueOf(i)));
break;
case 2:
hssfcell.setCellValue((String)titleMap.get(String.valueOf(i)));
break;
case 3:
hssfcell.setCellValue((String)titleMap.get(String.valueOf(i)));
break;
case 4:
hssfcell.setCellValue((String)titleMap.get(String.valueOf(i)));
break;
case 5:
hssfcell.setCellValue((String)titleMap.get(String.valueOf(i)));
break;
case 6:
hssfcell.setCellValue((String)titleMap.get(String.valueOf(i)));
break;
}
}
return hssfsheet;
}
/**
 * 添加excel正文数据
 */
private HSSFSheet addSheetContentList(HSSFSheet hssfsheet,Map contentMap,List contentList){
for(int k = 0;k < contentList.size();k++){
HSSFRow hssfrow = hssfsheet.createRow(k+1);
for (int i = 0; i < VALIDATE_AFTER_CELL_COUNT; i++) {
Map map = (Map) contentList.get(k);
HSSFCell hssfcell = hssfrow.createCell((short) i);
hssfcell.setEncoding(HSSFWorkbook.ENCODING_UTF_16);
switch (i) {
case 0:
hssfcell.setCellValue((String)map.get(contentMap.get(String.valueOf(i))));
break;
case 1:
hssfcell.setCellValue((String)map.get(contentMap.get(String.valueOf(i))));
break;
case 2:
hssfcell.setCellValue((String)map.get(contentMap.get(String.valueOf(i))));
break;
case 3:
hssfcell.setCellValue((String)map.get(contentMap.get(String.valueOf(i))));
break;
case 4:
hssfcell.setCellValue((String)map.get(contentMap.get(String.valueOf(i))));
break;
case 5:
hssfcell.setCellValue((String)map.get(contentMap.get(String.valueOf(i))));
break;
case 6:
hssfcell.setCellValue((String)map.get(contentMap.get(String.valueOf(i))));
break;
}
}
}
return hssfsheet;
}






头像

snowcoal
  • java
  • poiEXCEL

本文标签:

javapoiEXCEL

收藏到我的私密空间

标题:java之poiEXCEL操作

作者:柳岸花明

你暂未登录,请登录后才可收藏至您的私密空间 确认取消
雪炭网

键盘操作 更便捷 -雪炭网雪中送炭-乐趣无限

如果本站的内容有幸帮助到了您,建议您了解一下当页的广告内容哦,我们的进步离不开您的支持,Thank you~