赞
踩
部署Nginx,启动,配置端口(防火墙记得打开端口), 配置文件路径.此时只作为静态服务器来使用,把文件暴露出来,(这里自行百度)
然后就是连接远程服务器在结合数据下载excel,直接上代码
- //list就是你要写入到excel里面的数据
- public Workbook exportExcelByTemplate(List<List<Object>> list) throws Exception {
- Map<String, Object> returnMap = new HashMap<>();
- int HttpResult; // 服务器返回的状态
-
- //这里fileName excelName 文件名,excel名都是放在list数据的最后一行,这是一种处理方式
- String fileName = (String) list.get(list.size() - 1).get(0);
- String excelName = (String) list.get(list.size() - 1).get(1);
-
- //中文名字要改编码格式
- String nmsg = URLEncoder.encode(fileName, "UTF-8");
-
- //filePath 是你定义在yml文件里面服务器地址,类似http://123.0.0.11:231
- //这里的straddr 就是一个完整的访问地址,类似http://123.0.0.11:231/test.txt
- String straddr = filePath + nmsg;
-
-
- URL url = new URL(straddr);
- HttpURLConnection connection = (HttpURLConnection) url.openConnection();
- connection.connect();
- HttpResult = connection.getResponseCode();
-
- //上面一段都是连接服务器
- Workbook oldWk = null;
- if (HttpResult != HttpURLConnection.HTTP_OK) {
- System.out.print("无法连接到服务器");// 不等于HTTP_OK说明连接不成功
- returnMap.put("result", HttpResult);
- } else {
-
- //这里就是得到服务器的excel模板流,里面可能只有表头,表内容用list填充
- POIFSFileSystem ps = new POIFSFileSystem(connection.getInputStream());
- oldWk = new HSSFWorkbook(ps);//原服务器excel文件
- Sheet sheet = oldWk.getSheetAt(0); //获取到工作表,因为一个excel可能有多个工作表
- //表体,就是循环了,判断一下单元格类型然后填充进每一个单元格
- for (int r = 0; r < list.size() - 1; r++) {
- Row rowBt = sheet.createRow(r);
- for (int c = 0; c < list.get(0).size(); c++) {
- Cell cell = rowBt.createCell(c);
- if (r == 0) {
- cell.setCellType(CellType.STRING);
- cell.setCellValue((String) list.get(r).get(c));
- } else {
- if (ifNumberByObj(list.get(r).get(c))) {
- cell.setCellType(CellType.NUMERIC);
- cell.setCellValue(Double.parseDouble(list.get(r).get(c).toString()));
- }
- }
- }
- }//表体循环结束
- }
- return oldWk;
- }
-
- //该Ccontroller没有使用@restController, 没有给所有方法标记以JSON方式返回,因为这个导出的方法是返回的文件流,以JSON格式返回会报错
- @RequestMapping(value = "/exportExcelByTemplateByGG",method = RequestMethod.POST)
- public ResponseResult exportExcelByTemplate(@RequestBody List<List<Object>> list,HttpServletResponse response) throws Exception {
- Workbook sheets = flExcelAddressApi.exportExcelByTemplate(list);
-
- response.setContentType("application/vnd.ms-excel");
- response.setHeader("Content-disposition", "attachment;");
- ServletOutputStream out = response.getOutputStream();
- try {
- sheets.write(out);
- out.flush();
- out.close();
- } catch (IOException ioe) {
- return ResponseResult.build(500,"导出错误",null);
- }
-
- return ResponseResult.success();
-
- 这就很简单,获取输出流, 设置相应的文件,然后Workbook有一个往外面写的方法write
- // 创建模板下载链接
- downloads(data, name) {
- // for IE
- if (window.navigator && window.navigator.msSaveOrOpenBlob) {
- window.navigator.msSaveOrOpenBlob(data, `${name}.xls`);
- } else {
- let url = window.URL.createObjectURL(data)
- let link = document.createElement('a');
- link.style.display = 'none';
- link.href = url;
- link.setAttribute('download', `${name}.xls`);
- document.body.appendChild(link);
- link.click();
- document.body.removeChild(link);
- window.URL.revokeObjectURL(url);
- }
- }
-
-
- 下载原理,<a>标签的href属性连接到blob对象
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。