与批量导入相反,action代码如下:
// 处理导出到excel的数据
public String outExcel() throws IOException{
HSSFWorkbook wb=new HSSFWorkbook();//建立新的工作薄对象
HSSFSheet sheet=wb.createSheet("new sheet");//建立新的sheet对象
List<Employee> empList=employeeManager.getAll();
for(int i=0;i<empList.size();i++){
HSSFRow row=sheet.createRow(i);//建立新的行
HSSFCell cell1=row.createCell(0);//建立新的cell
cell1.setCellValue(empList.get(i).getMarking());//单元格赋值
HSSFCell cell2=row.createCell(1);//建立新的cell
cell2.setCellValue(empList.get(i).getName());//单元格赋值
HSSFCell cell3=row.createCell(2);//建立新的cell
cell3.setCellValue(empList.get(i).getNation());//单元格赋值
HSSFCell cell4=row.createCell(3);//建立新的cell
cell4.setCellValue(empList.get(i).getHometown());//单元格赋值
HSSFCell cell5=row.createCell(4);//建立新的cell
if(empList.get(i).getSex()){
cell5.setCellValue("男");//单元格赋值
}else{
cell5.setCellValue("女");//单元格赋值
}
}
String basePath = "E:/feehool/export/excel";
FileOutputStream fileout=new FileOutputStream(basePath+"/"+downFileName+".xls");//输出流创建excel
wb.write(fileout);//写入excel
fileout.close();//关闭
return "outExcel";
}
struts.xml中配置如下:
<result type="stream" name="outExcel"> <param name="contentType">application/octet-stream</param><!--下载格式--> <param name="inputName">inputStream</param> <param name="contentDisposition">attachment;filename="${downFileName}.xls"</param>
<param name="bufferSize">4096</param><!--下载文件名字-->
</result><!--大小-->
-----------------------------------------------------------其他留待以后补充