public InputStream getInputStream() { HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb.createSheet("sheet1"); HSSFRow row = sheet.createRow(0); HSSFCell cell = row.createCell((short) 0); cell.setEncoding(HSSFCell.ENCODING_UTF_16); cell.setCellValue("序号"); cell = row.createCell((short) 1); cell.setEncoding(HSSFCell.ENCODING_UTF_16); cell.setCellValue("姓"); cell = row.createCell((short) 2); cell.setEncoding(HSSFCell.ENCODING_UTF_16); cell.setCellValue("名"); cell = row.createCell((short) 3); cell.setEncoding(HSSFCell.ENCODING_UTF_16); cell.setCellValue("年龄"); List<User> list = this.findAll(); for (int i = 0; i < list.size(); ++i) { User user = list.get(i); row = sheet.createRow(i + 1); cell = row.createCell((short) 0); cell.setEncoding(HSSFCell.ENCODING_UTF_16); cell.setCellValue(i + 1); cell = row.createCell((short) 1); cell.setEncoding(HSSFCell.ENCODING_UTF_16); cell.setCellValue(user.getFirstname()); cell = row.createCell((short) 2); cell.setEncoding(HSSFCell.ENCODING_UTF_16); cell.setCellValue(user.getLastname()); cell = row.createCell((short) 3); cell.setEncoding(HSSFCell.ENCODING_UTF_16); cell.setCellValue(user.getAge()); } ByteArrayOutputStream os = new ByteArrayOutputStream(); try { wb.write(os); } catch (IOException e) { e.printStackTrace(); } byte[] content = os.toByteArray(); InputStream is = new ByteArrayInputStream(content); return is; // byte[] content = wb.getBytes(); // InputStream is = new ByteArrayInputStream(content); // // return is; // String fileName = CharacterUtils.getRandomString(10); // String fileName = RandomStringUtils.randomAlphanumeric(10); // // fileName = new StringBuffer(fileName).append(".xls").toString(); // // final File file = new File(fileName); // // try // { // OutputStream os = new FileOutputStream(file); // wb.write(os); // os.close(); // } // catch (Exception e) // { // e.printStackTrace(); // } // // InputStream is = null; // try // { // is = new FileInputStream(file); // } // catch (FileNotFoundException e) // { // e.printStackTrace(); // } // // new Thread(new Runnable() // { // public void run() // { // try // { // Thread.sleep(15000); // } // catch (InterruptedException e) // { // e.printStackTrace(); // } // // file.delete();//删除临时文件 // } // }).start(); // // return is; }