Excel导出文件流下载
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导出文件流下载的更多相关文章
- World、Excel利用流下载
/** * 下载excel * @param request * @param response * @param filePath * @param fileName */public static ...
- excel导入、下载功能
1.excel导入.下载功能 2.首先,我们是居于maven项目进行开发引入poi,如果不是那就手动下载相应的jar包引入项目就可以了 <!-- poi --> <dependenc ...
- java实现点击查询数据生成excel文件并下载
须先导入关键maven包 <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi& ...
- asp.net将页面内容按需导入Excel,并设置excel样式,下载文件(解决打开格式与扩展名指定的格式不统一的问题)
//请求一个excel类 Microsoft.Office.Interop.Excel.ApplicationClass excel = null; //创建 Workbook对象 Microsoft ...
- Java 读取excel 文件流
public static void initType(String path) { try { //1 获取excel文件流 excel xls 文件 暂不支持xlsx if (path.conta ...
- java导出excel 浏览器直接下载或者或以文件形式导出
/** * excel表格直接下载 */ public static void exportExcelByDownload(HSSFWorkbook wb,HttpServletResponse ht ...
- js文件流下载通用方法
通常我们会用到文件流下载文件,下面给大家一个通用的文件流下载的js /* *下载文件 * options:{ * url:'', //下载地址 * isNewWinOpen:false,是否新窗口打开 ...
- js实现使用文件流下载csv文件
1. 理解Blob对象 在Blob对象出现之前,在javascript中一直没有比较好的方式处理二进制文件,自从有了Blob了,我们就可以使用它操作二进制数据了.现在我们开始来理解下Bolb对象及它的 ...
- 文件流下载时 axios blob文件大小不正确?
文件流下载时 js blob文件大小不正确? res.data的字节长度 length blob.size匹配不上.. axio请求里必须修改 responseType: 'blob' 参数, 默认是 ...
随机推荐
- urllib.request中Handler处理器
1.定义 自定义的urlopen()方法,urlopen()方法是一个特殊的opener(模块已定义好),不支持代理等功能,通过Handler处理器对象来自定义opener对象 2.常用方法 1.bu ...
- Beta周第14次Scrum会议(11/23)【王者荣耀交流协会】
一.小组信息 队名:王者荣耀交流协会 小组成员 队长:高远博 成员:王超,袁玥,任思佳,王磊,王玉玲,冉华 小组照片 二.开会信息 时间:2017/11/23 17:02~17:14,总计12min. ...
- mysql 开启binlog
登录mysql之后使用下面的命令查看是否开启binlog show variables like 'log_%'; if没有开启则在mysql的配置文件中 加入以下内容 server_id=2 log ...
- LeetCode - Baseball Game
You're now a baseball game point recorder. Given a list of strings, each string can be one of the 4 ...
- LeetCode - Find K Closest Elements
Given a sorted array, two integers k and x, find the k closest elements to x in the array. The resul ...
- JAVA (1)–第一个GUI程序 添加标题 关闭窗口 屏幕位置 导入图标
import java.awt.*; // 可以改成 import javax.swing.*; public class FirstFrame { public static void main ...
- MACOS-Can't-connect-to-local-MySQL-server-through-socket-'/tmp/mysql.sock'
mac os start mysql fail by "brew services start mysql"you can try : mysql.server start
- js中获取当前url参数值的一个方法
var $_GET = (function(){ var url = window.document.location.href.toString();//获得当前url地址并 ...
- System.Windows.Forms.Timer、System.Timers.Timer、System.Threading.Timer的 区别和用法
System.Windows.Forms.Timer执行的时候,如果你在过程中间加一个sleep整个的界面就死掉了,但是另外两个没有这个情况,System.Timers.Timer.System.Th ...
- Sublime Text怎么快速建立一个html5页面模板
在编辑器中输入一个半角英文的感叹号(!),然后按下TAB键