Service: 上面那个贴错了 /** * 导出员工信息 */ @Override public InputStream exportEmployeeExcel(List<?> list) { if(list==null){ throw new SystemException("list is null in exportEmployeeExcel method,it mustn't be null."); } ByteArrayOutputStream out = new ByteArrayOutputStream(); putDataOnOutputStream(out,list); return new ByteArrayInputStream(out.toByteArray()); }
private void putDataOnOutputStream(OutputStream os,List<?> list) { if(list==null) throw new SystemException("list is null in exportEmployeeExcel method,it mustn't be null.");
给你看点代码吧..
Action:
/**
* 员工信息导出excel
*/
@SuppressWarnings("unchecked")
public String exportEmployeeExcel() {
DataGrid g = employeeService.gainEmployeeList(employee);
List<Employee> list = g.getRows();
excelStream = employeeService.exportEmployeeExcel(list);// 导出excel表格
return "exportEmployeeExcel";
}service:
/**
* 员工信息导出excel
*/
@SuppressWarnings("unchecked")
public String exportEmployeeExcel() {
DataGrid g = employeeService.gainEmployeeList(employee);
List<Employee> list = g.getRows();
excelStream = employeeService.exportEmployeeExcel(list);// 导出excel表格
return "exportEmployeeExcel";
}struts2配置文件<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="employee" extends="basePackage">
<action name="employeeExport" class="com.gx.employee.action.EmployeeAction">
<result name="exportEmployeeExcel" type="stream">
<param name="contentType">
application/vnd.ms-excel
</param>
<param name="inputName">excelStream</param>
<param name="contentDisposition">
filename="employee.xls"
</param>
<param name="bufferSize">1024</param>
</result>
</action>
</package>
</struts>
/**
* 导出员工信息
*/
@Override
public InputStream exportEmployeeExcel(List<?> list) {
if(list==null){
throw new SystemException("list is null in exportEmployeeExcel method,it mustn't be null.");
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
putDataOnOutputStream(out,list);
return new ByteArrayInputStream(out.toByteArray());
}
private void putDataOnOutputStream(OutputStream os,List<?> list) {
if(list==null)
throw new SystemException("list is null in exportEmployeeExcel method,it mustn't be null.");
WritableWorkbook workbook;
try {
workbook = Workbook.createWorkbook(os);
WritableSheet sheet = workbook.createSheet("Sheet1", 0);
sheet.getSettings().setDefaultColumnWidth(20);
sheet.addCell(new jxl.write.Label(0, 0, "id"));
sheet.addCell(new jxl.write.Label(1, 0, "姓名"));
sheet.addCell(new jxl.write.Label(2, 0, "性别"));
sheet.addCell(new jxl.write.Label(3, 0, "年龄"));
sheet.addCell(new jxl.write.Label(4, 0, "编号"));
sheet.addCell(new jxl.write.Label(5, 0, "手机号"));
sheet.addCell(new jxl.write.Label(6, 0, "身份证号"));
sheet.addCell(new jxl.write.Label(7, 0, "级别"));
sheet.addCell(new jxl.write.Label(8, 0, "部门"));
sheet.addCell(new jxl.write.Label(9, 0, "家庭地址"));
sheet.addCell(new jxl.write.Label(10, 0, "入职时间"));
sheet.addCell(new jxl.write.Label(11, 0, "离职时间"));
sheet.addCell(new jxl.write.Label(12, 0, "是否在职"));
sheet.addCell(new jxl.write.Label(13, 0, "备注"));
//循环遍历到数据集
for(int i= 0;i<list.size();i++){
Employee e = (Employee) list.get(i);
sheet.addCell(new jxl.write.Label(0,i+1 ,e.getId()+""));
sheet.addCell(new jxl.write.Label(1,i+1 ,e.getName()));
sheet.addCell(new jxl.write.Label(2,i+1 ,e.getGender()==1?"男":"女"));
sheet.addCell(new jxl.write.Label(3,i+1 ,e.getAge()+""));
sheet.addCell(new jxl.write.Label(4,i+1 ,e.getBn()));
sheet.addCell(new jxl.write.Label(5,i+1 ,e.getMobile()));
sheet.addCell(new jxl.write.Label(6,i+1 ,e.getIdCard()));
sheet.addCell(new jxl.write.Label(7,i+1 ,e.getLevelName()));
sheet.addCell(new jxl.write.Label(8,i+1 ,e.getDepartmentName()));
sheet.addCell(new jxl.write.Label(9,i+1 ,e.getAddress()));
sheet.addCell(new jxl.write.Label(10,i+1 ,CommonUtil.getDate(e.getCreateTime())));
sheet.addCell(new jxl.write.Label(11,i+1 ,CommonUtil.getDate(e.getLeaveTime())));
sheet.addCell(new jxl.write.Label(12,i+1 ,e.getStatus()==1?"在职":"离职"));
sheet.addCell(new jxl.write.Label(13,i+1 ,e.getRe()));
}
workbook.write();
workbook.close();
} catch (Exception e) {
logger.error("putDataOnOutputStream has some error:",e);
throw new SystemException("putDataOnOutputStream has some error:",e);
}
}