在页面上显示各种文档中的内容。在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();
http://blog.csdn.net/wanglj7525/archive/2011/04/01/6295231.aspx
分享到:
相关推荐
\java通过url在线预览Word、excel、ppt、pdf、txt文档中的内容 \java通过url在线预览Word、excel、ppt、pdf、txt文档中的内容
java通过url在线预览Word、excel、ppt、pdf、txt文档
实现word excel ppt转为pdf,并提供转格式后删除原来的文件。(需要使用jcom)
本源码主要是ASP.net后台通过相应的类把word\excel\pdf\txt文件转化为html,将地址传回前端,前端获取地址,用iframe进行展现预览。
在线预览文档flash版(支持word、excel、ppt、pdf)+在线预览文档插件
java文档excel、word、pdf、ppt转图片
Android 本地预览Excel,Word,PPT,PDF 解决Android asposed转Excel乱码问题 带有Jar包
poi将word、PPT、Excel转pdf实现在线预览的jar包
Java实现在线预览word/excel/ppt,在网上找了很多的方式,有将word/excel/ppt转换为HTML的,还有转swf的,但是转swf的偏多,转HTML有些可是不好,转swf的效果好一些,但是在转excel的时候,默认是已A4的纸张来进行...
小伙伴,让自己的上传的附件支持在线浏览吧,跟百度文科一样的
办公自动化python将Word、Excel和PPT批量转成PDF文档,win32com操作word文档、Excel表格和PPT文档,WPS转为PDF文件。提高办公效率,快速阅读office文件,保护office文件格式和文件安全。
1.文件不需下载 2.在线打开预览 3.支持多种格式
本源码主要是ASP.net后台通过相应的类把word\excel\pdf\txt文件转化为html,将地址传回前端,前端获取地址,用iframe进行展现预览。
aspose实现在线预览word,ppt,excel,pdf,实现在线预览文档的功能。因架包较大,分为两份,文件夹后缀有提示
主要利用free spire.office for java 为word,ppt,pdf 文档添加文本水印。利用poi 给excel文档添加文本水印。所需要的包在lib文件夹下
前端实现文件预览(word、excel、pdf、ppt、mp4、图片、文本)
在线预览文档flash版(支持word、excel、ppt、pdf)+在线预览文档html版
在线预览常用办公文档(word、excel、ppt、pdf)
使用OpenOffice + jodconverter 将文档转成pdf,通过pdf.js在页面展示 写的是Stringboot 版的demo,核心代码很少,很容易懂
Word、Excel、PPT、PDF在线预览解决方案