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();
————————————————
相关文章
- Java应用架构的演化之路
- Apache Druid 安装的时候进行 Java 版本校验没有输出
- java后台发起get请求获取响应数据学习记录: 话不多说直接上代码
- Java前辈:学习J2EE流程中的经验和教训
- org.apache.catalina.connector.ClientAbortException: java.io.IOException: 您的主机中的软件中止了一个已建立的连接。
- Caused by: org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'company' in 'class java.lang.String'
- Error compiling file: /private/var/folders/tm/rj18p_ls10lb_fsqfc7h4trm0000gn/T/jetty-0.0.0.0-8081-WebRoot-_-any-/jsp/org/apache/jsp/login_jsp.java
- 阿里P7大牛整理!java后端开发自我介绍
- Java 四种线程池的使用
- java.lang.NoSuchMethodError: org.apache.commons.io.FileUtils.copyInputStreamToFile(Ljava/io/InputStream;Ljava/io/File;)V
- Java之Apache Tomcat教程[归档]
- JAVA Apache POI 之sax 解析10万级大数量数据
- 【错误解决】 java.lang.ClassNotFoundException: org.apache.jsp.WEB_002dINF.classes.views.index_jsp
- java.lang.ClassNotFoundException: org.apache.jsp.WEB_002dINF.views.login_jsp
- Java精选笔记_集合【Map(映射)接口】
- 遍历java对象中的属性
- java.lang.AssertionError: Status expected:<200> but was:<400>
- 异常 java.lang.NullPointerException at org.apache.jsp.index_jsp._jspService(index_jsp.java:124)
- Java输入流之BufferReader和Scanner的用法!
- 已解决maven中出现java.lang.ClassNotFoundException: org.apache.http.entity.mime.content.ContentBody异常的正确解决方
- dubbo升级到Apache的 2.7.7报错Caused by: java.io.IOException: invalid constant type: 18
- 【Android Gradle】安卓应用构建流程 ( Java 源码编译 和 AIDL 文件编译 )
- Java的IDEA最常用快捷键汇总+快速写出Main函数
- Eclipse创建并运行Java程序输出Hello World
- java.lang.NoClassDefFoundError: org/apache/curator/RetryPolicy解决方法
- pagehelper调用mybatis报错java.lang.NoSuchMethodError:org.apache.ibatis.reflection.MetaObject.forObject