Java Word转Html
生活随笔
收集整理的這篇文章主要介紹了
Java Word转Html
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
最近轉換的需求比較多,最近做了一個Word轉Html的
這個要導一個包和配置一個文件
1.jacob.jar
2.與jacob.jar相對應的jacob.dll(放在windows/sys32下或者放在jre下面)
代碼 ??1?package?test;??2?
??3?import?java.io.File;
??4?
??5?import?com.jacob.activeX.ActiveXComponent;
??6?import?com.jacob.com.Dispatch;
??7?import?com.jacob.com.Variant;
??8?
??9?public?class?WordToHtml?{
?10?
?11?????int?WORD_HTML?=?8;
?12?????int?WORD_TXT?=?7;
?13?????int?EXCEL_HTML?=?44;
?14?
?15?????/**
?16??????*?WORD轉HTML
?17??????*?
?18??????*?@param?docfile
?19??????*????????????WORD文件全路徑
?20??????*?@param?htmlfile
?21??????*????????????轉換后HTML存放路徑
?22??????*/
?23?????public?void?wordToHtml(String?docfile,?String?htmlfile)?{
?24?????????ActiveXComponent?app?=?new?ActiveXComponent("Word.Application");?//?啟動word
?25?????????try?{
?26?????????????app.setProperty("Visible",?new?Variant(false));
?27?????????????Dispatch?docs?=?app.getProperty("Documents").toDispatch();
?28?????????????Dispatch?doc?=?Dispatch.invoke(
?29?????????????????????docs,
?30?????????????????????"Open",
?31?????????????????????Dispatch.Method,
?32?????????????????????new?Object[]?{?docfile,?new?Variant(false),
?33?????????????????????????????new?Variant(true)?},?new?int[1]).toDispatch();
?34?????????????Dispatch.invoke(doc,?"SaveAs",?Dispatch.Method,?new?Object[]?{
?35?????????????????????htmlfile,?new?Variant(WORD_HTML)?},?new?int[1]);
?36?????????????Variant?f?=?new?Variant(false);
?37?????????????Dispatch.call(doc,?"Close",?f);
?38?????????}?catch?(Exception?e)?{
?39?????????????e.printStackTrace();
?40?????????}?finally?{
?41?????????????app.invoke("Quit",?new?Variant[]?{});
?42?????????}
?43?????}
?44?
?45?????/**
?46??????*?EXCEL轉HTML
?47??????*?
?48??????*?@param?xlsfile
?49??????*????????????EXCEL文件全路徑
?50??????*?@param?htmlfile
?51??????*????????????轉換后HTML存放路徑
?52??????*/
?53?????public?void?excelToHtml(String?xlsfile,?String?htmlfile)?{
?54?????????ActiveXComponent?app?=?new?ActiveXComponent("Excel.Application");?//?啟動excel
?55?????????try?{
?56?????????????app.setProperty("Visible",?new?Variant(false));
?57?????????????Dispatch?excels?=?app.getProperty("Workbooks").toDispatch();
?58?????????????Dispatch?excel?=?Dispatch.invoke(
?59?????????????????????excels,
?60?????????????????????"Open",
?61?????????????????????Dispatch.Method,
?62?????????????????????new?Object[]?{?xlsfile,?new?Variant(false),
?63?????????????????????????????new?Variant(true)?},?new?int[1]).toDispatch();
?64?????????????Dispatch.invoke(excel,?"SaveAs",?Dispatch.Method,?new?Object[]?{
?65?????????????????????htmlfile,?new?Variant(EXCEL_HTML)?},?new?int[1]);
?66?????????????Variant?f?=?new?Variant(false);
?67?????????????Dispatch.call(excel,?"Close",?f);
?68?????????????System.out.println("wordtohtml轉換成功");
?69?????????}?catch?(Exception?e)?{
?70?????????????e.printStackTrace();
?71?????????}?finally?{
?72?????????????app.invoke("Quit",?new?Variant[]?{});
?73?????????}
?74?????}
?75?
?76?????/**
?77??????*?/刪除指定文件夾
?78??????*?
?79??????*?@param?folderPath
?80??????*????????????文件夾全路徑
?81??????*?@param?htmlfile
?82??????*????????????轉換后HTML存放路徑
?83??????*/
?84?????public?void?delFolder(String?folderPath)?{
?85?????????try?{
?86?????????????delAllFile(folderPath);?//?刪除完里面所有內容
?87?????????????String?filePath?=?folderPath;
?88?????????????filePath?=?filePath.toString();
?89?????????????java.io.File?myFilePath?=?new?java.io.File(filePath);
?90?????????????myFilePath.delete();?//?刪除空文件夾
?91?????????}?catch?(Exception?e)?{
?92?????????????e.printStackTrace();
?93?????????}
?94?????}
?95?
?96?????/**
?97??????*?/刪除指定文件夾下所有文件
?98??????*?
?99??????*?@param?path
100??????*????????????文件全路徑
101??????*/
102?????public?boolean?delAllFile(String?path)?{
103?????????boolean?flag?=?false;
104?????????File?file?=?new?File(path);
105?????????if?(!file.exists())?{
106?????????????return?flag;
107?????????}
108?????????if?(!file.isDirectory())?{
109?????????????return?flag;
110?????????}
111?????????String[]?tempList?=?file.list();
112?????????File?temp?=?null;
113?????????for?(int?i?=?0;?i?<?tempList.length;?i++)?{
114?????????????if?(path.endsWith(File.separator))?{
115?????????????????temp?=?new?File(path?+?tempList[i]);
116?????????????}?else?{
117?????????????????temp?=?new?File(path?+?File.separator?+?tempList[i]);
118?????????????}
119?????????????if?(temp.isFile())?{
120?????????????????temp.delete();
121?????????????}
122?????????????if?(temp.isDirectory())?{
123?????????????????delAllFile(path?+?"/"?+?tempList[i]);//?先刪除文件夾里面的文件
124?????????????????delFolder(path?+?"/"?+?tempList[i]);//?再刪除空文件夾
125?????????????????flag?=?true;
126?????????????}
127?????????}
128?????????return?flag;
129?????}
130?
131?????public?static?void?main(String[]?args)?{
132?????????//?TODO?Auto-generated?method?stub
133?????????WordToHtml?wordtohtml?=?new?WordToHtml();
134?????????wordtohtml.wordToHtml("D:\\test.doc",?"D:\\test.html");
135?????????System.out.println("word轉html成功");
136?????}
137?}
138?
??每種功能的實現方法有很多,希望各位可以交流不同的思想和方法。可以加QQ412546724。呵呵
?
轉載于:https://www.cnblogs.com/jiayi/archive/2010/06/02/1750220.html
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的Java Word转Html的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java如何定义三个圆_java – 以
- 下一篇: java中负数取整_Java取整,固定保