Struts.xml部分
<package name="demo" namespace="/test" extends="struts-default">
<action name="exportExcel" class="com.test.action.StudentAction " method="exportExcel">
<result name="exportExcel" type="stream">
<param name="contentType">application/vnd.ms-excel</param>
<param name="contentDisposition">attachment;filename="${fileName}"</param>
<param name="bufferSize">1024</param>
<param name="inputName">excelFile</param>
</result>
</action>
</package>
Action部分
public class StudentAction extends ActionSupport{
private static final long serialVersionUID = 1L;
private Student student;
private InputStream excelFile;
private String fileName;
//set,get方法
public String exportExcel() throws Exception{
List<Student> stuList = stuService.exportMarkActive(student);
HSSFWorkbook workbook = exportExcel(stuList);
ByteArrayOutputStream output = new ByteArrayOutputStream();
workbook.write(output);
byte[] ba = output.toByteArray();
excelFile = new ByteArrayInputStream(ba);
output.flush();
output.close();
return "exportExcel";
}
exportExcel方法
public HSSFWorkbook exportExcel(List<Student> studentList) throws Exception {
fileName = "学生活动报表.xls";
//设置字符,防止乱码
fileName= new String(fileName.getBytes("UTF-8"),"ISO8859-1");
HSSFWorkbook wb;
//根据studentList生成excel文件
wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("学生活动列表");
HSSFRow row=sheet.createRow(0);
HSSFCell cell=row.createCell(0);
cell.setCellValue("ID");
cell=row.createCell(1);
cell.setCellValue("学生活动名称");
cell=row.createCell(2);
cell.setCellValue("学生活动类型");
cell=row.createCell(3);
cell.setCellValue("学生活动状态");
cell=row.createCell(4);
cell.setCellValue("学生活动开始日期");
cell=row.createCell(5);
cell.setCellValue("学生活动结束日期");
cell=row.createCell(6);
cell.setCellValue("学生活动所有者");
cell=row.createCell(7);
cell.setCellValue("预算成本");
cell=row.createCell(8);
cell.setCellValue("实际成本");
cell=row.createCell(9);
cell.setCellValue("创建人");
cell=row.createCell(10);
cell.setCellValue("学生活动创建时间");
cell=row.createCell(11);
cell.setCellValue("修改人");
cell=row.createCell(12);
cell.setCellValue("学生活动修改时间");
cell=row.createCell(13);
cell.setCellValue("详细描述");
cell=row.createCell(14);
if(studentList!=null&&studentList.size()>0){
MarketingActivities ma=null;
for(int i=0;i<studentList.size();i++){
ma=studentList.get(i);
row=sheet.createRow(i+1);
cell=row.createCell(0);
cell.setCellValue(ma.getId());
cell=row.createCell(1);
cell.setCellValue(ma.getName());
cell=row.createCell(2);
cell.setCellValue(ma.getType());
cell=row.createCell(3);
cell.setCellValue(ma.getState());
cell=row.createCell(4);
cell.setCellValue(ma.getStartDate());
cell=row.createCell(5);
cell.setCellValue(ma.getEndDate());
cell=row.createCell(6);
cell.setCellValue(ma.getOwner());
cell=row.createCell(7);
cell.setCellValue(ma.getBudgetcost());
cell=row.createCell(8);
cell.setCellValue(ma.getActualcost());
cell=row.createCell(9);
cell.setCellValue(ma.getCreateBy());
cell=row.createCell(10);
cell.setCellValue(ma.getCreateTime());
cell=row.createCell(11);
cell.setCellValue(ma.getEditBy());
cell=row.createCell(12);
cell.setCellValue(ma.getEditTime());
cell=row.createCell(13);
cell.setCellValue(ma.getDescription());
}
}
return wb;
}