jsp中如何把word文档直接显示在网页中?
最近小弟正在作以网站,在把上传的word显示在网页中的时候,用的是流读取方式,但在网页中显示的都是乱码?
代码如下:
try
{
BufferedReader reader=new BufferedReader(new FileReader(newFile));
out.println(reader);
String s= " ";
while((s=reader.readLine())!=null)
out.println(StrConvert.chStr(s));
reader.close();
}
catch(Exception e)
{
out.println(e);
}
------解决方案--------------------用poi读取word文档吧:
package org.poi.hwsf.test;
import java.io.FileInputStream;
import
java.io.FileNotFoundException;
import
java.io.IOException;
import java.util.List;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.extractor.WordExtractor;
/**
* @author
*
*/
public class PoiTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String filename = "test2.doc ";
try {
FileInputStream in = new FileInputStream (filename);
HWPFDocument doc = new HWPFDocument(new FileInputStream(filename));
WordExtractor extractor = new WordExtractor(in);
String[] str = extractor.getParagraphText();
List name = doc.getTextTable().getTextPieces();
// System.out.println( "the result length is : "+str.length());
System.out.println( "the result is : "+str);
for (int i = 0; i < str.length; i++ ) {
System.out.print(str[i]);
}
// HWPFDocument doc = new HWPFDocument(new FileInputStream(filename));
// System.out.println(doc);
// String str = doc.getDataStream().toString();
// System.out.println(str);
} catch (
FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (
IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
};
}
}
------解决方案--------------------mark
------解决方案--------------------web开发语言操作word的功能最好还是专业的控件,
SOAOFFICE--微软office文档中间件,
这个应该是你需要的,只需要编写几行代码就可以了
先看看效果 ,现在做办公开发的都用第三方控件,这个是最好的
http://www.kehansoft.com/soaoffice/doclist.asp
下载一个试试
下载地址:http://www.kehansoft.com/web/down.htm