public String createExcel(List<Egrkh> clientList) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String newdate = sdf.format(new Date()); //创建一个EXCEL Workbook wb = new HSSFWorkbook(); DataFormat format = wb.createDataFormat(); CellStyle style; //创建一个SHEET Sheet sheet1 = wb.createSheet(newdate); String[] title = {"公司名称","联系电话","传真","身份证号","联系人","最后联系时间","录入时间"}; int i=0; Font font = wb.createFont(); font.setBoldweight((short)12); font.setFontHeight((short)12); font.setFontHeightInPoints((short)12); CellStyle cellStyle = wb.createCellStyle(); cellStyle.setFont(font); //创建一行 Row row = sheet1.createRow((short)0); //填充标题 for (String s:title){ Cell cell = row.createCell(i); cell.setCellValue(s); cell.setCellStyle(cellStyle); i++; } for (int j=0 ;j<clientList.size();j++){ Egrkh egkh = clientList.get(j); Row row1 = sheet1.createRow((short)j+1); Cell cell = row1.createCell(0);//公司名称 cell.setCellStyle(cellStyle); cell.setCellValue(egkh.getKhgsname()); cell = row1.createCell(1);//联系电话 cell.setCellStyle(cellStyle); cell.setCellValue(egkh.getKhtel()); cell = row1.createCell(2);//传真 cell.setCellStyle(cellStyle); cell.setCellValue(egkh.getKhfax()); cell = row1.createCell(3);//身份证号 cell.setCellStyle(cellStyle); cell.setCellValue(egkh.getKhsfnumber()); cell = row1.createCell(4);//联系人 cell.setCellStyle(cellStyle); cell.setCellValue(egkh.getKhlxr()); cell = row1.createCell(5);//最后联系时间 cell.setCellStyle(cellStyle); if(egkh.getKhlasttime()!= null){ cell.setCellValue(egkh.getKhlasttime()); } cell = row1.createCell(6);//录入时间 } FileOutputStream fileOut = null; //String filePath = ServletActionContext.getRequest().getRealPath("/")+"\\excel\\"+newdate+".xls"; String filePath = ServletActionContext.getServletContext().getRealPath("WEB-INF/")+"\\excel\\"+newdate+".xls"; try { fileOut = new FileOutputStream(filePath); wb.write(fileOut); } catch (IOException e) { e.printStackTrace(); } finally{ try { fileOut.close(); } catch (IOException e) { e.printStackTrace(); } } return filePath; } @SuppressWarnings("unchecked") public String exExcel() throws IOException, Exception{ @SuppressWarnings("unused") ActionContext ac=ActionContext.getContext(); List<Egrkh> clientLists=this.clientService.getAllEgrkhs(khtel,khmob,khfax,khinputtimeStart,khinputtimeEnd,khqq,ztid,khaddress,khsex,khcsny,khsfnumber,khemail,khkhqk,khgsname,khlxr,username,yxid); File file = new File(createExcel(clientLists)); contentType = "application/octet-stream;charset=utf-8"; downloadFile = new FileInputStream(file); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); fileName = simpleDateFormat.format(new Date())+".xls"; return SUCCESS; } //----------download---------- private String contentType;//文件编码 private InputStream downloadFile;//下载的文件流 private String fileName;//下载文件名
上面的是action中写的
下面是struts配置文件中写的
<action name="exExcel" method="exExcel" class="com.gnet.client.clientAct"> <result name="success" type="stream"> <param name="inputName">downloadFile</param> <param name="contentType">${contentType}</param> <param name="contentDisposition">attachment;filename="${fileName}"</param> <param name="bufferSize">2048</param> </result> </action>
引用来自“小编辑”的答案
分享一个功能丰富、兼容性好、高性能的 Excel 文档基础库 Excelize:https://github.com/xuri/excelize