使用java实现 Excel在线预览
1 背景
当前有个需求,需要实时读取excel的内容,并显示在页面上。
收到需求的时候, java后端能不能生成个临时文件,然后展示到前台页面,前台只要能请求到后端,预览就生效了。
2 Do IT
2.1 引入pom文件
<dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>3.9</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>3.9</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-scratchpad</artifactId><version>3.9</version></dependency><dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId><version>3.9</version></dependency><dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.5</version></dependency><dependency><groupId>org.jsoup</groupId><artifactId>jsoup</artifactId><version>1.10.3</version></dependency>
2.2 直接上代码
@GetMapping(value = "/showexcel")public void showexcel(HttpServletResponse response) throws Exception {String path = "C:\\Users\\Administrator\\Desktop\\临时\\";String file = "2021-03-11_2021年3月11日差异报表.xls";InputStream input = new FileInputStream(path + file);HSSFWorkbook excelBook = new HSSFWorkbook(input);ExcelToHtmlConverter excelToHtmlConverter = new ExcelToHtmlConverter(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument());excelToHtmlConverter.processWorkbook(excelBook);List pics = excelBook.getAllPictures();if (pics != null) {for (int i = 0; i < pics.size(); i++) {Picture pic = (Picture) pics.get(i);try {pic.writeImageContent(new FileOutputStream(path + pic.suggestFullFileName()));} catch (FileNotFoundException e) {e.printStackTrace();}}}Document htmlDocument = excelToHtmlConverter.getDocument();ByteArrayOutputStream outStream = new ByteArrayOutputStream();DOMSource domSource = new DOMSource(htmlDocument);StreamResult streamResult = new StreamResult(outStream);TransformerFactory tf = TransformerFactory.newInstance();Transformer serializer = tf.newTransformer();serializer.setOutputProperty(OutputKeys.ENCODING, "utf-8");serializer.setOutputProperty(OutputKeys.INDENT, "yes");serializer.setOutputProperty(OutputKeys.METHOD, "html");serializer.transform(domSource, streamResult);outStream.close();String content = new String(outStream.toByteArray());String fileName =path + "exportExcel.html";FileUtils.writeStringToFile(new File(fileName), content, "utf-8");InputStream inputStream = new FileInputStream(fileName);FileInputStream fileIs = null;try {fileIs = (FileInputStream) inputStream;} catch (Exception e) {return;}//得到文件大小byte data[] = new byte[fileIs.available()];//读数据int count = 0;fileIs.read(data);//设置返回的文件类型response.setContentType("image.png");//得到向客户端输出二进制数据的对象OutputStream outStreams = response.getOutputStream();//输出数据outStreams.write(data);outStreams.flush();outStreams.close();fileIs.close();}
3 查看效果
4 相关参考
部分代码参考了如下博客,虽然他们只讲了一半,实操不行,但是还是有可取之处。
java实现在线预览--poi实现word、excel、ppt转html
使用java实现 Excel在线预览相关推荐
- aspose实现word,excel在线预览
aspose实现word,excel在线预览 一,项目中引入aspose依赖 <dependency><groupId>com.aspose</groupId>&l ...
- Java实现PDF在线预览
Java实现PDF在线预览 前言:之前一直PDF一直是下载后再查看,一直在想如何如何在线预览,现已找到方法,作此笔记,也希望都对其他人有所帮助 代码实现 @Slf4j @Controller @Req ...
- 图片、pdf、wrod和excel 在线预览
自定义文件服务器 图片.pdf.wrod和excel 在线预览 场景介绍 准备工作 预览图片,pdf 预览word 预览excel 效果图 场景介绍 因公司项目中需要使用到文件交互,因此在网上找了一个 ...
- java文档在线预览实现
Java文档在线预览实现 近期因需要完成对word.excel.ppt.txt等文档的内容检索,在用户检索到相关内容时,需要给用户提供一个在线预览文档的功能.在网上找到部分参考后,实现了该功能. Ja ...
- Vue+SpringBoot实现Excel在线预览功能(PS:添加样式比较费劲)
** 问题还原: ** 在做项目时,用户需要上传Excel模板,里面有对应的各种数据.我们拿到这个Excel后,定时的根据其中的数据去查对应的实时数据并进行计算,然后将实时数据和计算后的数据保存到Ex ...
- Python excel转成html页面 excel 在线预览
Python excel转成html页面 excel 在线预览 因为这两天公司的项目要用到在浏览excel 所以就在做这个功能.一开始查了很多资料 都是各种不行,最后好不容易找到一些辅助资料 终于是今 ...
- js-xlsx vue导入excel在线预览
js-xlsx vue导入excel在线预览 导入XLSX库 官方地址Github 安装 npm install xlsx --s 引入 import XLSX from 'xlsx' HTML &l ...
- 前端-Excel在线预览
前端-Excel在线预览 最近项目中有一个 Excel 预览的需求,就调研了一下 xls/xlsx.word.ppt 文件在线预览功能的实现 . 实现 xls/xlsx.word.ppt 在线预览功能 ...
- java图片的在线预览_【Java】web实现图片在线预览
一.场景还原 用户上传了一张图片,已有服务器保存路径,现由于系统配置无法直接通过图片URL打开预览图片,需实现点击预览将图片显示在浏览器上. 二.实现方法 html: 预览 此处用预览按钮方法实现 J ...
最新文章
- 20道常见初级Java面试题
- 内核级HOOK的几种实现与应用
- Android --- RecycleView获取第 i 个 item 里面的控件并进行赋值
- C++基础与面向对象
- cpu开核_电脑魔改篇——CPU散热器
- Spring Boot 热部署入门
- java定时器的使用(Timer)
- linux管道通信题目,操作系统实训(Linux)——习题解答、例题解析、实验指导-王红-实验实验7软中断及管道通信课案.ppt...
- 人工智能时代,我们需要什么样的芯片?| 码书
- 与成都的幸福行动家交流GTD
- 用 maven 命令启动项目和直接用tomcat 启动项目的区别
- BeanAir无线传感器方案
- 常用基本初等函数的求导公式推导
- 谈谈我职业生涯中的三次潦倒--Leo病中的思考 续
- ictclas java 64_ICTCLAS分词系统在win7 64 java平台下环境搭建
- java音频剪辑_Java混剪音频
- 毕业后的去向:继续读研还是直接就业?
- android ftp权限,实战android手机telnet、ftp登陆,权限修改
- 怎么在excel中导入ChemDraw结构图
- 自我反省,自我批评、解剖,对近期行为的反思