博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Excel导出文件流下载
阅读量:5924 次
发布时间:2019-06-19

本文共 4022 字,大约阅读时间需要 13 分钟。

Controller.cs

@CrossOrigin(allowCredentials="true", allowedHeaders="*", methods={RequestMethod.GET,              RequestMethod.POST, RequestMethod.DELETE, RequestMethod.OPTIONS,              RequestMethod.HEAD, RequestMethod.PUT, RequestMethod.PATCH}, origins="*")    @RequestMapping("/ot")    @ResponseBody    public void ot(String key, HttpServletResponse response) {        try {            String storeName = key+".xlsx";            XSSFWorkbook workBook = excelService.outputExcel(key);            ByteArrayOutputStream bt = new ByteArrayOutputStream();            workBook.write(bt);            workBook.close();            bt.flush();            ByteArrayInputStream bis = new ByteArrayInputStream(bt.toByteArray());            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");              response.setHeader("Content-disposition", "attachment; filename="                  + new String(storeName.getBytes("utf-8"), "ISO8859-1"));             //设置输出长度            response.setHeader("Content-Length", String.valueOf(bt.size()));            BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream());            byte[] buff = new byte[2048];              int bytesRead;              while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {                bos.write(buff, 0, bytesRead);              }              //关闭流            bis.close();              bos.close();          } catch (Exception e) {            e.printStackTrace();        }    }

Service.cs

public XSSFWorkbook outputExcel(String key){        String json = "{status:'OK', msg: '导出完毕!'}";                String schame = key.split("\\.")[0];        String tablename = key.split("\\.")[1];        XSSFWorkbook workBook = new XSSFWorkbook();        XSSFSheet sheet = workBook.createSheet();        workBook.setSheetName(0,"Sheet1");        XSSFRow titleRow = sheet.createRow(0);        titleRow.createCell(0).setCellValue(tablename);        Jedis jedis = new Jedis("10.8.4.94");        List
tables = new ArrayList
(); jedis.select(2); try { tables = jedis.lrange("db_tables", 0, -1); } catch (Exception e) { // TODO: handle exception }finally{ jedis.close(); } for(String s:tables){ JSONObject obj = JSON.parseObject(s); if(obj.getString("owner").equals(schame) && obj.getString("table_name").equals(tablename)){ titleRow.createCell(1).setCellValue(obj.getString("comment")); } } XSSFRow secondRow = sheet.createRow(1); secondRow.createCell(0).setCellValue("字段英文名"); secondRow.createCell(1).setCellValue("字段类型"); secondRow.createCell(2).setCellValue("注释"); secondRow.createCell(3).setCellValue("是否主键"); secondRow.createCell(4).setCellValue("是否非空"); secondRow.createCell(5).setCellValue("默认值"); jedis = new Jedis("10.8.4.94"); List
columns = new ArrayList
(); jedis.select(2); try { columns = jedis.lrange(key, 0, -1); } catch (Exception e) { // TODO: handle exception }finally{ jedis.close(); } int k = 2; for(String s: columns){ JSONObject obj = JSON.parseObject(s); XSSFRow row = sheet.createRow(k); row.createCell(0).setCellValue(obj.getString("column_name")); row.createCell(1).setCellValue(obj.getString("data_type").equals("VARCHAR2")?(obj.getString("data_type")+"("+obj.getString("data_length")+")"):obj.getString("data_type")); row.createCell(2).setCellValue(obj.getString("comment")); row.createCell(3).setCellValue(obj.getString("pk")); row.createCell(4).setCellValue(obj.getString("nullable")); row.createCell(5).setCellValue(obj.getString("data_default")); k++; } return workBook; }

 

转载地址:http://nfavx.baihongyu.com/

你可能感兴趣的文章
c++ 参赛设置
查看>>
数据库之触发器
查看>>
Linux pipe函数
查看>>
java检测http请求的ip地址 Java问题通用解决代码
查看>>
Circuit provides reference for multiple ADCs
查看>>
c++ json 详解
查看>>
解决嵌入WinForm的WPF控件无法显示图片问题
查看>>
Java多线程详解(三)
查看>>
RGB 颜色对照表
查看>>
RGB色彩模式
查看>>
匈牙利算法
查看>>
OGRE之跳出漫长的编译等待
查看>>
通用分页存储过程真的有注入漏洞吗?
查看>>
atitit.软件开发GUI 布局管理优缺点总结java swing wpf web html c++ qt php asp.net winform
查看>>
php操作memcache的使用测试总结
查看>>
8天玩转并行开发——第一天 Parallel的使用
查看>>
Sublime Text 3 史上最性感的编辑器
查看>>
BZOJ3832 : [Poi2014]Rally
查看>>
让Java和JavaScript进行交互
查看>>
android 上传文件
查看>>