用的poi版本是poi-3.8-20120326.jar
首先定义Excel的格式和数据源:
@Override
public Map definitionExcel(HttpServletRequest request) {
Map map = new HashMap();
try{
String sdfDate = new java.text.SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
String fileName = "TrainDemandGatherSheet" +sdfDate;
String RealPath = request.getSession().getServletContext().getRealPath("/");
String path = RealPath + "download" + "//" + fileName + ".xls";
map.put("path",path);
map.put("tempPath", "/download/" + fileName + ".xls" );
map.put("sheetname", "培训需求汇总");
List list1 = new ArrayList();
String[] title1={"用户名","时间","产品知识","销售技能","心态建设","主顾开拓","契约知识","保险意义与功用","其它方面","需求描述"};
list1.add(title1);
List<CourseDemandPublish> courseDemandPublishList = courseDemandPublishDao.getCourseDemandPublish();
List<String[]> courseDemandPublishArr = convertValue(courseDemandPublishList);
for(String[] str : courseDemandPublishArr){
String userName = courseDemandPublishDao.getUserNameById(str[0]);
String[] checkOut={userName,str[1],
str[2],str[3],
str[4],str[5],
str[6],
str[7],
str[8],str[9]};
list1.add(checkOut);
}
Date date = new Date();
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dt1 = sdf1.format(date);
map.put("list", list1);
}catch (Exception e) {
e.printStackTrace();
}
return map;
}
写Excel:
/**
* 写Excel
*/
public void writeExcel(HttpServletResponse response,Map map){
String outputFile = (String) map.get("path");
HSSFWorkbook workbook; //实例化一个工作簿
try {
response.reset();
workbook = new HSSFWorkbook();
HSSFCellStyle style1 = workbook.createCellStyle();
HSSFFont font = workbook.createFont();
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
style1.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style1.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style1.setBorderRight(HSSFCellStyle.BORDER_THIN);
style1.setBorderTop(HSSFCellStyle.BORDER_THIN);
style1.setAlignment(HSSFCellStyle.ALIGN_CENTER);//左右居中
style1.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//上下居中
style1.setFont(font);
HSSFCellStyle style2 = workbook.createCellStyle();
style2.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style2.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style2.setBorderRight(HSSFCellStyle.BORDER_THIN);
style2.setBorderTop(HSSFCellStyle.BORDER_THIN);
style1.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//上下居中
HSSFSheet sheet = workbook.createSheet((String) map.get("sheetname"));//建立一张表格
List<String[]> l = new ArrayList<String[]>();
l=(List<String[]>) map.get("list");
for(int i=0;i<l.size();i++){
HSSFRow row=sheet.createRow(i);
String[] str = l.get(i);
if(i<l.size()){
for(int j=0;j<str.length;j++){
HSSFCell cell=row.createCell(j);
cell.setCellValue(str[j]);
if(i<1){
cell.setCellStyle(style1);
}else{
cell.setCellStyle(style2);
}
if(str[j]!=""&&str[j]!=null){
int value = str[j].getBytes().length;
sheet.setColumnWidth(j, 20*250);
}
}
}
}
FileOutputStream fout=new FileOutputStream(outputFile);
workbook.write(fout);
fout.flush();
fout.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
在Action中进行调用:
@RequestMapping(value = "/courseDemandGather/export.do")
public String export(HttpServletRequest request,HttpServletResponse response,ModelMap model){
try{
setModel(request,model);
Map map = trainDemandGatherSev.definitionExcel(request);
model.put("href", map.get("tempPath"));
trainDemandGatherSev.writeExcel(response,map);
}catch(Exception e){
e.printStackTrace();
}
log.debug("导出培训需求汇总数据成功,跳转到培训需求汇总主页面!");
return "courseDemandGather/homepage";
}
jsp页面的请求:
<span style="padding-left:490px;">
<input type="submit" value="导出" onClick="this.form.action='export.do';"/>
<#if href?? && "" != href>
<a href="${base}${href}" id="downLoadButton" name="downLoadButton" target="_bank" >文件下载</a> </#if></span>