private IOutputsummaryService iOutputsummaryServices;
private OutputSummaryDetailBean outputSummaryDetailBean;
private boolean success = false;
private InputStream excelStream;
private String xlsname;
private String filename;
public String exportsummaryDetail() throws Exception {
try {
// 创建一个webbook,对应一个Excel文件
HSSFWorkbook wb = new HSSFWorkbook();
// 在webbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet = wb.createSheet("各班产量报表管理");
// 在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
HSSFRow row = sheet.createRow((int) 0);
// 创建单元格,并设置值表头 设置表头
HSSFCellStyle style = wb.createCellStyle();
// style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
HSSFFont f = wb.createFont();
f.setFontHeightInPoints((short)11);//字号
// f.setColor(HSSFFont.COLOR_RED); //设置为红色
f.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //加粗
style.setFont(f);
HSSFSheet s = wb.getSheetAt(0);
// s.setColumnWidth((short)0,(short)900);//设置列的宽度
s.setColumnWidth((short)1,(short)4000);
s.setColumnWidth((short)2,(short)4000);
s.setColumnWidth((short)3,(short)4000);
s.setColumnWidth((short)4,(short)4000);
s.setColumnWidth((short)5,(short)5000);
s.setColumnWidth((short)6,(short)3500);
s.setColumnWidth((short)7,(short)3500);
s.setColumnWidth((short)8,(short)3500);
s.setColumnWidth((short)9,(short)3500);
s.setColumnWidth((short)10,(short)8000);
HSSFCell cell = row.createCell((short) 0);
cell.setCellValue("班组");
cell.setCellStyle(style);
cell = row.createCell((short) 1);
cell.setCellValue("型号");
cell.setCellStyle(style);
cell = row.createCell((short) 2);
cell.setCellValue("机台编号");
cell.setCellStyle(style);
cell = row.createCell((short) 3);
cell.setCellValue("人员编号");
cell.setCellStyle(style);
cell = row.createCell((short) 4);
cell.setCellValue("操作工");
cell.setCellStyle(style);
cell = row.createCell((short) 5);
cell.setCellValue("产品规格");
cell.setCellStyle(style);
cell = row.createCell((short) 6);
cell.setCellValue("计划产品");
cell.setCellStyle(style);
cell = row.createCell((short) 7);
cell.setCellValue("实际产品");
cell.setCellStyle(style);
cell = row.createCell((short) 8);
cell.setCellValue("入库数");
cell.setCellStyle(style);
cell = row.createCell((short) 9);
cell.setCellValue("差额");
cell.setCellStyle(style);
cell = row.createCell((short) 10);
cell.setCellValue("未完成原因分析");
cell.setCellStyle(style);
// 写入实体数据 实际应用中这些数据从数据库得到,
List list = iOutputsummaryServices.findAllOutputsummaryDetail(outputSummaryDetailBean);
for (int i = 0; i < list.size(); i++) {
row = sheet.createRow((int) i + 1);
OutputSummaryDetailBean stu = (OutputSummaryDetailBean) list.get(i);
// 创建单元格,并设置值
row.createCell((short) 0).setCellValue(stu.getClassname());
row.createCell((short) 1).setCellValue(stu.getEqmodel());
row.createCell((short) 2).setCellValue(stu.getEqname());
row.createCell((short) 3).setCellValue(stu.getNameid());
row.createCell((short) 4).setCellValue(stu.getUsername());
row.createCell((short) 5).setCellValue(stu.getGg());
row.createCell((short) 6).setCellValue((double) stu.getRatedoutput());
row.createCell((short) 7).setCellValue((double) stu.getActualoutput());
row.createCell((short) 8).setCellValue((double) stu.getIncomeoutput());
row.createCell((short) 9).setCellValue((double) stu.getBalanceoutput());
row.createCell((short) 10).setCellValue(stu.getUnfinishedcause());
}
//下载流
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();wb.write(byteArrayOutputStream);
excelStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
String agent = ServletActionContext.getRequest().getHeader("USER-AGENT");
xlsname = "各班产量报表.xls";
if(null != agent && -1 != agent.indexOf("MSIE")){
xlsname = URLEncoder.encode(xlsname,"UTF8");
}else if(null != agent && -1 != agent.indexOf("Mozilla")){
xlsname = MimeUtility.encodeText(xlsname,"UTF8","B");
}
success = true;
} catch (Exception e) {
e.printStackTrace();
}
return SUCCESS;
}
struts.xml配置
<!--设备有效作业率月报表导出下载-->
<action name="eqmonthreportAction" class="downloadFileAction">
<result name="success" type="stream">
<param name="contentType">application/octet-stream</param>
<param name="contentDisposition">attachment;filename=${xlsname}</param>//表格的名称
<param name="inputName">inputStream</param> //流
</result>
</action>
在页面上调用要用url的方式来调用方法
下面是点击导出按钮所出现的图片:
导出表格的里面格式: