使用jacob将word转成PDFWord文档格式.docx
- 文档编号:20839489
- 上传时间:2023-01-25
- 格式:DOCX
- 页数:20
- 大小:31.17KB
使用jacob将word转成PDFWord文档格式.docx
《使用jacob将word转成PDFWord文档格式.docx》由会员分享,可在线阅读,更多相关《使用jacob将word转成PDFWord文档格式.docx(20页珍藏版)》请在冰豆网上搜索。
14.public
class
TransformFiletoHtml
15.{
16.
int
WORD_HTML
=
8;
17.
WORD_TXT
7;
18.
EXCEL_HTML
44;
19.
20.
/**
21.
*
WORD转HTML
22.
@param
docfile
WORD文件全路径
23.
htmlfile
转换后HTML存放路径
24.
*/
25.
public
void
wordToHtml(String
docfile,
String
htmlfile)
26.
{
27.
ActiveXComponent
app
new
ActiveXComponent("
Word.Application"
);
//
启动word
28.
try
29.
30.
app.setProperty("
Visible"
Variant(false));
31.
Dispatch
docs
app.getProperty("
Documents"
).toDispatch();
32.
doc
Dispatch.invoke(docs,"
Open"
Dispatch.Method,new
Object[]
Variant(false),new
Variant(true)
},
int[1]).toDispatch();
33.
Dispatch.invoke(doc,
"
SaveAs"
Dispatch.Method,
{htmlfile,
Variant(WORD_HTML)
int[1]);
34.
Variant
f
Variant(false);
35.
Dispatch.call(doc,
Close"
f);
36.
}
37.
catch
(Exception
e)
38.
39.
e.printStackTrace();
40.
41.
finally
42.
43.
app.invoke("
Quit"
Variant[]
{});
44.
45.
46.
47.
48.
EXCEL转HTML
49.
xlsfile
EXCEL文件全路径
50.
51.
52.
excelToHtml(String
xlsfile,
53.
54.
Excel.Application"
启动excel
55.
56.
57.
58.
excels
Workbooks"
59.
excel
Dispatch.invoke(excels,"
60.
Dispatch.invoke(excel,
Variant(EXCEL_HTML)
61.
62.
Dispatch.call(excel,
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
/删除指定文件夹
76.
folderPath
文件夹全路径
77.
78.
79.
delFolder(String
folderPath)
80.
81.
82.
83.
delAllFile(folderPath);
//删除完里面所有内容
84.
filePath
folderPath;
85.
filePath.toString();
86.
java.io.File
myFilePath
java.io.File(filePath);
87.
myFilePath.delete();
//删除空文件夹
88.
{e.printStackTrace();
89.
90.
91.
92.
/删除指定文件夹下所有文件
93.
path
文件全路径
94.
95.
boolean
delAllFile(String
path)
96.
97.
flag
false;
98.
File
file
File(path);
99.
if
(!
file.exists())
100.
101.
return
flag;
102.
103.
file.isDirectory())
104.
105.
106.
107.
String[]
tempList
file.list();
108.
temp
null;
109.
for
(int
i
0;
<
tempList.length;
i++)
110.
111.
(path.endsWith(File.separator))
112.
113.
File(path
+
tempList[i]);
114.
115.
else
116.
117.
File.separator
118.
119.
(temp.isFile())
120.
121.
temp.delete();
122.
123.
(temp.isDirectory())
124.
125.
delAllFile(path
/"
//先删除文件夹里面的文件
126.
delFolder(path
//再删除空文件夹
127.
true;
128.
129.
130.
131.
132.}
调用JAVA代码:
1.public
Test1
2.
static
main(String[]
args)
3.
TODO
Auto-generated
method
stub
4.
trans
TransformFiletoHtml();
5.
trans.wordToHtml("
D:
\\sinye.doc"
\\sinye.html"
6.
7.
8.}
只写了一个测试word转html的,excel转html的同理,在TransformFiletoHtml类中,写了两个方法,一个是删除文件夹的方法(delFolder()),一个是删除文件夹下所有文件的方法(delAllFile())。
写这个的目的是出于:
在word或者excel转html的过程中,除了生成制定的html页面外,jacob组件会生成一些转换html页面时相关的其它页面,但是这些相关的其它页面不是我们所需要的,因此想把它删除,其实不删除也可以,只是看着不爽,在测试中,发现,word有时不会生成这样的文件,而且即使生成了,也能删除掉,但是excel生成的文件却不能删除,还望高手们给予解答。
另外,在你将excel转换html时,如果你的代码没问题,转换时,老提示什么存在用户区域的安全设置这什么的,会让你选择继续转换,还是取消。
这是因为你转换的那个excel中写了保护,解决办法是在excel的工具->
保护->
允许用户编辑区域,删除里面的所有保护。
整体思路参考
上面的这篇文章使用jacob将word转换成HTML的,利用的是Word的另存为功能,在Office2007SP2之后,Office就可以另存为PDF了,可以使用这个方法将office另存为PDF文档。
具体代码可以参考上文里面的,另存为哪种类型是由newvariant()里面的参数决定的。
newVariant(),这里面的根据传入的参数不同,可以另存为不同的类型,但是在网上搜索了一个并没有找到有关这个参数类型的一个说明,自己尝试了一下,结果如下:
Doc
1
Dot
2-5
Txt
6
Rtf
7
8、10
htm
11
Xml
12、16
Docx
13
Docm
14
Dotx
15
Dotm
17
我使用的是office2010,不同版本的对应的应该不一样,我是写了这一小段程序来测试另存为的类型的。
JacobTest
wordToPDF(String
toFile,int
type)
Dispatch.invoke(
8.
docs,
10.
11.
Variant(false),
12.
//new
Variant(type),这里面的type的决定另存为什么类型的文件
14.
15.
toFile,
Variant(type)
//源文件全路径
="
\\服务实施描述报告(企业门户).docx"
;
18;
//些路径test为实际存在的目录,s后面为要另存为的文件名
toFile="
d:
\\test\\s"
+i;
wordToPDF(docfile,
toFile,i);
34.}
EXCEL转PDF(JAVA自动运行)
基本思想是,通过office2007自带的插件,保存为PDF,然后用ActiveXComponent实现,核心代码如下:
1>
.转换类ExcelToPdf.java
packagecom.olive.util;
importjava.io.*;
importjava.util.Calendar;
importjava.util.Date;
importcom.jacob.activeX.ActiveXComponent;
import.ComThread;
import.Dispatch;
import.Variant;
publicclassExcelToPdf{
privateStringpath;
publicstaticbooleanrunFlag=false;
publicExcelToPdf(Stringpath){
this.path=path;
}
publicvoidsaveExcelAsPdf(StringfilePath,StringoutFile){
ComThread.InitSTA();
ActiveXComponentactcom=newActiveXComponent("
try{
System.out.println((newDate()).toString()+"
startconvertfrom:
"
+filePath+"
to"
+outFile);
actcom.setProperty("
newVariant(false));
Dispatchexcels=actcom.getProperty("
Dispatchexcel=Dispatch.invoke(excels,"
Dispatch.Method,
newObject[]{filePath,newVariant(false),newVariant(false)},
newint[9]).toDispatch();
Dispatch.invoke(excel,"
Dispatch.Method,newObject[]{outFile,newVariant(57),newVariant(false),
newVariant(57),newVariant(57),newVariant(false),newVariant(true),newVariant(57),newVariant(false),
ne
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 使用 jacob word 转成 PDF