谁做过把word2007转HTML的功能
rt,要怎么做,不要03的 --------------------编程问答-------------------- 用poi读取word文件然后转化成html。之前项目中做过,不过貌似样式有点问题 --------------------编程问答-------------------- 笨方法,读取excel 然后 append html 标签,最后输出html文件,应该是可以的,呵呵没有做过。。。 --------------------编程问答--------------------
--------------------编程问答-------------------- 楼上的方法是支持2003的,不支持2007的
/**
* 读取word文件的内容,并转为html
* @param fileName
* @param outPutFile
* @throws TransformerException
* @throws IOException
* @throws ParserConfigurationException
*/
public void convert2Html(String fileName, String outPutFile)throws TransformerException, IOException,ParserConfigurationException {
HWPFDocument wordDocument = new HWPFDocument(new FileInputStream(fileName));//WordToHtmlUtils.loadDoc(new FileInputStream(inputFile));
WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument());
wordToHtmlConverter.setPicturesManager( new PicturesManager(){
public String savePicture( byte[] content,PictureType pictureType, String suggestedName,float widthInches, float heightInches){
return suggestedName;
}
} );
wordToHtmlConverter.processDocument(wordDocument);
//save pictures保存图片
List pics=wordDocument.getPicturesTable().getAllPictures();
if(pics!=null){
for(int i=0;i<pics.size();i++){
Picture pic = (Picture)pics.get(i);
System.out.println();
try {
pic.writeImageContent(new FileOutputStream(filePath+ File.separator+ pic.suggestFullFileName()));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
org.w3c.dom.Document htmlDocument = wordToHtmlConverter.getDocument();
ByteArrayOutputStream out = new ByteArrayOutputStream();
if (out != null){
out.flush();
}
DOMSource domSource = new DOMSource(htmlDocument);
StreamResult streamResult = new StreamResult(out);
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);
out.close();
writeFile(new String(out.toByteArray()), outPutFile);
}
/**
* 输入流写入html
* @param content
* @param path
*/
public void writeFile(String content, String path) {
FileOutputStream fos = null;
BufferedWriter bw = null;
try {
File file = new File(path);
fos = new FileOutputStream(file,true);
bw = new BufferedWriter(new OutputStreamWriter(fos,"UTF-8"));
bw.write(content);
} catch (FileNotFoundException fnfe) {
fnfe.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {
if (bw != null)
bw.close();
if (fos != null)
fos.close();
} catch (IOException ie) {
}
}
}
补充:Java , Java EE