本文共 4018 字,大约阅读时间需要 13 分钟。
展开全部
简单的poiEXCEL操作。部分代码。嘿嘿
/**
* 创建标准模板
* @62616964757a686964616fe59b9ee7ad9431333264636232param 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;
}
吧不知道能不能帮你,我也不动感我在csdn看看的。实在不行你在那问问。。。。。。。。。。。。。
追问
感谢这么大段的回答~~~~~但是不是我要的东西啊,后台生成我已经写好了,问题是前台的提交和提示的问题。。。。
已赞过
已踩过<
你对这个回答的评价是?
评论
收起
转载地址:http://fhqhp.baihongyu.com/