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<String> tables = new ArrayList<String>();
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<String> columns = new ArrayList<String>();
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;
}

Excel导出文件流下载的更多相关文章

  1. World、Excel利用流下载

    /** * 下载excel * @param request * @param response * @param filePath * @param fileName */public static ...

  2. excel导入、下载功能

    1.excel导入.下载功能 2.首先,我们是居于maven项目进行开发引入poi,如果不是那就手动下载相应的jar包引入项目就可以了 <!-- poi --> <dependenc ...

  3. java实现点击查询数据生成excel文件并下载

    须先导入关键maven包 <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi& ...

  4. asp.net将页面内容按需导入Excel,并设置excel样式,下载文件(解决打开格式与扩展名指定的格式不统一的问题)

    //请求一个excel类 Microsoft.Office.Interop.Excel.ApplicationClass excel = null; //创建 Workbook对象 Microsoft ...

  5. Java 读取excel 文件流

    public static void initType(String path) { try { //1 获取excel文件流 excel xls 文件 暂不支持xlsx if (path.conta ...

  6. java导出excel 浏览器直接下载或者或以文件形式导出

    /** * excel表格直接下载 */ public static void exportExcelByDownload(HSSFWorkbook wb,HttpServletResponse ht ...

  7. js文件流下载通用方法

    通常我们会用到文件流下载文件,下面给大家一个通用的文件流下载的js /* *下载文件 * options:{ * url:'', //下载地址 * isNewWinOpen:false,是否新窗口打开 ...

  8. js实现使用文件流下载csv文件

    1. 理解Blob对象 在Blob对象出现之前,在javascript中一直没有比较好的方式处理二进制文件,自从有了Blob了,我们就可以使用它操作二进制数据了.现在我们开始来理解下Bolb对象及它的 ...

  9. 文件流下载时 axios blob文件大小不正确?

    文件流下载时 js blob文件大小不正确? res.data的字节长度 length blob.size匹配不上.. axio请求里必须修改 responseType: 'blob' 参数, 默认是 ...

随机推荐

  1. flask的安装

    1.查看已安装的Flask版本 在 python 的交互模式中 : 1. import flask 没报错:已经安装了Flask,可以继续查看版本 报错:没安装Flask 2. flask.__ver ...

  2. 自定义bootbox:dialog方法

    <script src="static/ace/js/bootbox.js"></script>bootbox.dialog({ message: '< ...

  3. HDU2027:统计元音

    Problem Description 统计每个元音字母在字符串中出现的次数. Input 输入数据首先包括一个整数n,表示测试实例的个数,然后是n行长度不超过100的字符串. Output 对于每个 ...

  4. Manjaro启动项目及服务配置备忘

    Manjaro启动项目及服务配置备忘 =============== 系统服务GUI管理搜索 systemdgenie 并安装,类似Windows的服务管理. ================ 系统启 ...

  5. 05 ajax,jquery,xstream,json解析

    Ajax “Asynchronous Javascript And XML”(异步JavaScript和XML),把原有的技术,整合到一起           1.使用CSS和XHTML来表示.   ...

  6. ajax的原生调用

    https://blog.csdn.net/jitianxia68/article/details/78393406

  7. js framework comparation

    starting a new project:(finance project for p2p -- like lending club, or prosper ) ,we considering a ...

  8. CS程序中XML编码Encode和解码Decode

    VB6的代码,原则上只要是Windows系统均可以使用此方法 Function XMLEncode(ByVal text As String) As String Dim xmldoc Set xml ...

  9. Cassandra--启用用户认证和用户管理

    ======================================================== 启用用户认证和创建超级用户 需要针对每个节点进行配置修改和重启,但授权操作仅需要在任一 ...

  10. The key of real time embedded system

    对于实时嵌入式系统来说,最重要的是每一个进程所需时间的可检测性,可预测性.要不你的实时性是没有办法保证的.有些时候你对一些没有从事过嵌入式开发的人谈这个进程(TASK)设计是按8ms被调度一次,他们会 ...