apache poi操作office文档----java在线预览txt、word、ppt、execel,pdf代码
在页面上显示各种文档中的内容。在servlet中的逻辑
word:
BufferedInputStream bis = null;
URL url = null;
HttpURLConnection httpUrl = null; // 建立链接
url = new URL(urlReal);
httpUrl = (HttpURLConnection) url.openConnection();// 连接指定的资源
httpUrl.connect();// 获取网络输入流
bis = new BufferedInputStream(httpUrl.getInputStream());
String bodyText = null;
WordExtractor ex = new WordExtractor(bis);
bodyText = ex.getText();
response.getWriter().write(bodyText);
excel:
BufferedInputStream bis = null;
URL url = null;
HttpURLConnection httpUrl = null; // 建立链接
url = new URL(urlReal);
httpUrl = (HttpURLConnection) url.openConnection();// 连接指定的资源
httpUrl.connect();// 获取网络输入流
bis = new BufferedInputStream(httpUrl.getInputStream());
content = new StringBuffer();
HSSFWorkbook workbook = new HSSFWorkbook(bis);
for (int numSheets = 0; numSheets < workbook.getNumberOfSheets(); numSheets++) {
HSSFSheet aSheet = workbook.getSheetAt(numSheets);// 获得一个sheet
content.append("/n");
if (null == aSheet) {
continue;
}
for (int rowNum = 0; rowNum <= aSheet.getLastRowNum(); rowNum++) {
content.append("/n");
HSSFRow aRow = aSheet.getRow(rowNum);
if (null == aRow) {
continue;
}
for (short cellNum = 0; cellNum <= aRow.getLastCellNum(); cellNum++) {
HSSFCell aCell = aRow.getCell(cellNum);
if (null == aCell) {
continue;
}
if (aCell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
content.append(aCell.getRichStringCellValue()
.getString());
} else if (aCell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
boolean b = HSSFDateUtil.isCellDateFormatted(aCell);
if (b) {
Date date = aCell.getDateCellValue();
SimpleDateFormat df = new SimpleDateFormat(
"yyyy-MM-dd");
content.append(df.format(date));
}
}
}
}
}
response.getWriter().write(content.toString());
ppt:
BufferedInputStream bis = null;
URL url = null;
HttpURLConnection httpUrl = null; // 建立链接
url = new URL(urlReal);
httpUrl = (HttpURLConnection) url.openConnection();// 连接指定的资源
httpUrl.connect();// 获取网络输入流
bis = new BufferedInputStream(httpUrl.getInputStream());
StringBuffer content = new StringBuffer("");
SlideShow ss = new SlideShow(new HSLFSlideShow(bis));
Slide[] slides = ss.getSlides();
for (int i = 0; i < slides.length; i++) {
TextRun[] t = slides[i].getTextRuns();
for (int j = 0; j < t.length; j++) {
content.append(t[j].getText());
}
content.append(slides[i].getTitle());
}
response.getWriter().write(content.toString());
pdf:
BufferedInputStream bis = null;
URL url = null;
HttpURLConnection httpUrl = null; // 建立链接
url = new URL(urlReal);
httpUrl = (HttpURLConnection) url.openConnection();// 连接指定的资源
httpUrl.connect();// 获取网络输入流
bis = new BufferedInputStream(httpUrl.getInputStream());
PDDocument pdfdocument = null;
PDFParser parser = new PDFParser(bis);
parser.parse();
pdfdocument = parser.getPDDocument();
ByteArrayOutputStream out = new ByteArrayOutputStream();
OutputStreamWriter writer = new OutputStreamWriter(out);
PDFTextStripper stripper = new PDFTextStripper();
stripper.writeText(pdfdocument.getDocument(), writer);
writer.close();
byte[] contents = out.toByteArray();
String ts = new String(contents);
response.getWriter().write(ts);
txt:
BufferedReader bis = null;
URL url = null;
HttpURLConnection httpUrl = null; // 建立链接
url = new URL(urlReal);
httpUrl = (HttpURLConnection) url.openConnection();// 连接指定的资源
httpUrl.connect();// 获取网络输入流
bis = new BufferedReader( new InputStreamReader(httpUrl.getInputStream()));
StringBuffer buf=new StringBuffer();
String temp;
while ((temp = bis.readLine()) != null) {
buf.append(temp);
response.getWriter().write(temp);
if(buf.length()>=1000){
break;
}
}
bis.close();
————————————————
apache poi操作office文档----java在线预览txt、word、ppt、execel,pdf代码的更多相关文章
- Java实现office文档与pdf文档的在线预览功能
最近项目有个需求要java实现office文档与pdf文档的在线预览功能,刚刚接到的时候就觉得有点难,以自己的水平难以在三四天做完.压力略大.后面查找百度资料.以及在同事与网友的帮助下,四天多把它做完 ...
- java在线预览txt、word、ppt、execel,pdf代码
在页面上显示各种文档中的内容.在servlet中的逻辑 word: BufferedInputStream bis = null; URL url = null; HttpURLConnection ...
- 怎么在线预览.doc,.docx,.ofd,.pdf,.wps,.cad文件以及Office文档的在线解析方式。
前言 Office文件在线预览是目前移动化办公的一种新趋势.Office在线预览指的是Office系列的文件在线查看而不依附域客户端的存在.在浏览器或者浏览器控件中可以预览查看Word.PDF.Exc ...
- ASP.NET Core2集成Office Online Server(OWAS)实现办公文档的在线预览与编辑(支持word\excel\ppt\pdf等格式)
Office Online Server是微软开发的一套基于Office实现在线文档预览编辑的技术框架(支持当前主流的浏览器,且浏览器上无需安装任何插件,支持word.excel.ppt.pdf等文档 ...
- [转发]ASP.NET Core2集成Office Online Server(OWAS)实现办公文档的在线预览与编辑(支持word\excel\ppt\pdf等格式)
转载自:https://www.cnblogs.com/Andre/p/9549874.html Office Online Server是微软开发的一套基于Office实现在线文档预览编辑的技术框架 ...
- 文件在线预览doc,docx转换pdf(一)
文件在线预览doc,docx转换pdf(一) 1. 前言 文档转换是一个是一块硬骨头,但是也是必不可少的,我们正好做的知识库产品中,也面临着同样的问题,文档转换,精准的全文搜索,知识的转换率,是知识库 ...
- 使用微软提供的Office Online实现Office文档的在线查看,编辑等功能
使用微软提供的Office Online平台只需要一个网址即可在线查看Xls,doc,PPT等文档 http://view.officeapps.live.com/op/view.aspx?src=要 ...
- 实战动态PDF在线预览及带签名的PDF文件转换
开篇语: 最近工作需要做一个借款合同,公司以前的合同都是通过app端下载,然后通过本地打开pdf文件,而喜欢创新的我,心想着为什么不能在线H5预览,正是这个想法,说干就干,实践过程总是艰难的,折腾了3 ...
- 前端实现文件在线预览txt,pdf,doc,xls,ppt几种格式
做法就是使用iframe标签 1.text,pdf的文件预览 <iframe class="filename" :src="文件的地址" width='1 ...
随机推荐
- tkinter中的messagebox
from tkinter import * from tkinter import messagebox def myMsg(): messagebox.showinfo("My Messa ...
- 【已采纳】新项目第一次怎么上传到github里面
言归正传,最近学习了怎么将新创建的本地代码上传到github上,这里简单的记录一下,我喜欢使用命令行,这里全用命令行来实现,不了解git命令的可以去了解下. 第一步:建立git仓库 cd到你的本地 ...
- PHP严重致命错误处理:php Fatal error: Cannot redeclare class or function【转】
这篇文章主要介绍了PHP严重致命错误处理:php Fatal error: Cannot redeclare class or function,需要的朋友可以参考下 1.错误类型:PHP致命错误 E ...
- 起步:SpringBoot
pom.xml <parent> <groupId>org.springframework.boot</groupId> <artifactId>spr ...
- GCN python 实现2:利用GCN进行节点分类
参考:https://www.baidu.com/link?url=5oU-O_YQV8DdSTtRkgzsQ_vuwjJHyUOxqeAKhq98ZA5XtvKW8PNQwXgSlr5GpESRqh ...
- 目标检测的评价标准mAP, Precision, Recall, Accuracy
目录 metrics 评价方法 TP , FP , TN , FN 概念 计算流程 Accuracy , Precision ,Recall Average Precision PR曲线 AP计算 A ...
- 使用nginx配置带有权限验证的反向代理
环境:centos6u3 1.安装nginx (1)上传nginx nginx-1.14.0.tar.gz.可以从nginx官网下载http://nginx.org/en/download.html ...
- 201871010106-丁宣元 《面向对象程序设计(java)》第十六周学习总结
201871010106-丁宣元 <面向对象程序设计(java)>第十六周学习总结 正文开头: 项目 内容 这个作业属于哪个课程 https://home.cnblogs.com/u/nw ...
- Netty的常用API(二)
在使用Netty之前先介绍下Netty的常用API,对其有一个大概的了解. 一.EventLoop和EventLoopGroup EventLoop如同它的名字,它是一个无限循环(Loop),在循环中 ...
- JDOJ 1928: 排队买票
JDOJ 1928: 排队买票 JDOJ传送门 Description 一场演唱会即将举行.现有n个歌迷排队买票,一个人买一张,而售票处规定,一个人每次最多只能买两张票.假设第i位歌迷买一张票需要时间 ...