(1)在service中定义格式 表单的内容
public void writeToExcel(List<Commodity> logList, String filePath)
throws IOException, Exception, WriteException {
File file = new File(filePath);
file.createNewFile();
WritableWorkbook workBook = Workbook.createWorkbook(file);
WritableSheet sheet = workBook.createSheet("品种资质审核", 0);
Label label = new Label(0, 0, "产品Id");
sheet.addCell(label);
label = new Label(1, 0, "产品名称");
sheet.addCell(label);
label = new Label(2, 0, "剂型");
sheet.addCell(label);
label = new Label(3, 0, "规格");
sheet.addCell(label);
label = new Label(4, 0, "包装规格");
sheet.addCell(label);
label = new Label(5, 0, "生产企业");
sheet.addCell(label);
label = new Label(6, 0, "资料完整性");
sheet.addCell(label);
label = new Label(7, 0, "资料有效性");
sheet.addCell(label);
label = new Label(8, 0, "审核状态");
sheet.addCell(label);
for (int i = 0; i < logList.size(); i++) {
int j = i + 1;
Commodity info = logList.get(i);
Label dynamicLabel = null;
dynamicLabel = new Label(0, j, StringTools.getTrim(info.getInnerId()));
sheet.addCell(dynamicLabel);
dynamicLabel = new Label(1, j, StringTools.getTrim(info.getTradeName()));
sheet.addCell(dynamicLabel);
dynamicLabel = new Label(2, j, StringTools.getTrim(info.getDose()));
sheet.addCell(dynamicLabel);
dynamicLabel = new Label(3, j, StringTools.getTrim(info.getSpec()));
sheet.addCell(dynamicLabel);
dynamicLabel = new Label(4, j, StringTools.getTrim(info.getPackSpec()));
sheet.addCell(dynamicLabel);
dynamicLabel = new Label(5, j, StringTools.getTrim(info.getPname()));
sheet.addCell(dynamicLabel);
// 资料完整性
String wanzhengxing ="";
if ("1".equals(info.getWz())) {
wanzhengxing="完整";
} else{
wanzhengxing="不完整";
}
// 资料有效性
String yxx="";
if ("1".equals(info.getYx())) {
yxx="有效";
} else
yxx="无效";
//审核结果 0待审核 ,1审核通过,-1否决
String auditResult="";
if("-1".equals(info.getData())){
auditResult="否决";
}else if("1".equals(info.getData())){
auditResult="通过";
}else{
auditResult="待审核";
}
dynamicLabel = new Label(6, j, StringTools.getTrim(wanzhengxing));
sheet.addCell(dynamicLabel);
dynamicLabel = new Label(7, j, StringTools.getTrim(yxx));
sheet.addCell(dynamicLabel);
dynamicLabel = new Label(8, j, StringTools.getTrim(auditResult));
sheet.addCell(dynamicLabel);
}
workBook.write();
workBook.close();
}
(2)在action中控制导出excel
@Action(value = "toGetExcelFile", results = {
@Result(name = "fail", type = "dispatcher", location = "/login.jsp"),
@Result(name = "success", type = "dispatcher", location = "/qualify/qyzzsh.jsp") })
public String toGetExcelFile() throws Exception {
List<PublicMember> list = enczzservice.queryQyzzshExcel(pubmem);
EnczzAuditServiceImpl ec = new EnczzAuditServiceImpl();
String filePath = "export.xls";// 临时文件
ec.writeToExcel(list, filePath);
FileInputStream inputstream = new FileInputStream(filePath);
HttpServletResponse response = ServletActionContext.getResponse();
OutputStream os = response.getOutputStream();// 取得输出流
response.reset();// 清空输出流
response.setHeader("Content-disposition", "attachment; filename="
+ URLEncoder.encode("企业资质审核", "utf-8") + ".xls");// 设定输出文件头
response.setContentType("application/msexcel");// 定义输出类型
response.setContentLength(inputstream.available());
byte[] fileByte = new byte[1024];
while (inputstream.read(fileByte) > 0) {
os.write(fileByte, 0, fileByte.length);
}
new File(filePath).deleteOnExit();
return null;
}