excel
来源:互联网 发布:双色球内部数据 编辑:程序博客网 时间:2024/05/17 16:13
public void exportExecel(){//判断用户类型BookUserInfo user = getUser();try {if(user!=null && user.isIsadmin()){Pager<BookInfoVo> pager =bookInfoService.getPagerBySql(null, null, page, 99999, sort, order);List<BookInfoVo> bookInfoList= pager.getDatas();if(null!=bookInfoList && bookInfoList.size()>0){String filePath =request.getSession().getServletContext().getRealPath("/export/exportbook.xls");File file = new File(filePath);InputStream is = new FileInputStream(file);////创建excel工作簿HSSFWorkbook wb = new HSSFWorkbook(is);//获取第一个sheet(页)Sheet sheet = wb.getSheetAt(0);//Row行 Cell方格 Row 和 Cell 都是从0开始计数的int i=1;for(BookInfoVo book : bookInfoList){// 创建一行,在页sheet上Row row = sheet.createRow(i);row.createCell(0).setCellValue(book.getId());row.createCell(1).setCellValue(book.getBookname());row.createCell(2).setCellValue(book.getBookprice().doubleValue());row.createCell(3).setCellValue(book.getBookauthor());row.createCell(4).setCellValue(book.getPublishs());row.createCell(5).setCellValue(ObjectFormatUtil.formatDateStr(book.getReleasedate()));row.createCell(6).setCellValue(book.getLibName());row.createCell(7).setCellValue(book.getTypeName());i++;} SimpleDateFormat sdformat = newSimpleDateFormat("yyyyMMdHHmmss");String saveFileName = "exportbook-"+sdformat.format(new Date())+"-"+user.getId()+".xls";String savePath =request.getSession().getServletContext().getRealPath("/export");File saveFile = new File(savePath,saveFileName);OutputStream fos = new FileOutputStream(saveFile);BufferedOutputStream buffout=newBufferedOutputStream(fos);// 把上面创建的工作簿输出到文件中wb.write(buffout);buffout.flush();buffout.close();downloadLocal(saveFile.getAbsolutePath(),saveFileName);}else{//无数据可导入message = "{\"error\":false,\"message\":"+"没有数据导出"+"}";outJsonPlainString(response, message);}}else{//未登陆或者无权限 ( 只有管理员才可以操作)message = "{\"error\":\"true\",\"message\":\"无权限导出,请联系管理员\"}";outJsonPlainString(response, message);}} catch (Exception e) {e.printStackTrace();message = "{\"error\":\"true\",\"message\":\"导出失败\"}";outJsonPlainString(response, message);}}/*** @Title: downloadLocal* @Description: 下载本地的文件* @param @param fileName 文件的名字,outFileName 下载的默认保存文件名* @return void* @throws*/public void downloadLocal(String filePath,String outFileName) throwsFileNotFoundException {// 读到流中InputStream inStream = new FileInputStream(filePath);// 文件的存放路径String downFileName = null;try {downFileName = URLEncoder.encode(outFileName, "UTF-8");} catch (Exception e) {downFileName = outFileName;}// 设置输出的格式response.reset();response.setContentType("application/octet-stream");response.addHeader("Content-Disposition", "attachment;filename=\""+downFileName+"\"");// 循环取出流中的数据byte[] b = new byte[1024*1024];int len;try {while ((len = inStream.read(b)) > 0)response.getOutputStream().write(b, 0, len);inStream.close();} catch (IOException e) {e.printStackTrace();}}Jsp页面function exportExcel(){location="<c:url value='/book/exportExecelBook.action'/>";}<a href="javascript:exportExcel()">导出到Excel</a>
0 0
- Excel
- Excel
- ExCel
- excel
- Excel
- EXCEL
- excel
- excel
- EXCEL
- Excel
- excel
- Excel
- excel
- excel
- excel
- EXCEL
- excel
- excel
- 搭建Apache James服务器用JAVA发送邮件
- 手机office办公——微软推出安卓手机端Office Mobile应用
- AndroidStudio JNI NDK CMake
- ajax的属性与事件、解决a ja x数据重复提交问题
- 如何创建一个依赖Android AAR文件的React Native组件
- excel
- 《数据结构与算法》--插入排序(类模板实现)
- Laravel 响应
- this串联构造方法
- ResultSet用法集锦
- 设计模式(一):工厂方法模式
- Mac通过aapt命令查看apk的包名等信息
- JS判断页面是否在微信浏览器、QQ浏览器等打开的方法
- 牛客网前端技能大挑战(dom节点查找)