jacob word转html(加上对word的一些格式处理)

本文介绍如何利用Jacob库将Word文档转换成HTML,并着重讲述了在转换过程中如何处理Word文档的竖排文字,使其变为横排。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/**
* WORD转HTML

* @param docfile
*            WORD文件全路径
* @param htmlfile
*            转换后HTML存放路径
*/
public boolean wordToHtml(String docfile, String htmlfile) {
// 启动word应用程序(Microsoft Office Word 2007)
ActiveXComponent app = new ActiveXComponent("Word.Application");
try {
// 设置word应用程序不可见
app.setProperty("Visible", new Variant(false));
// documents表示word程序的所有文档窗口,(word是多文档应用程序)
Dispatch docs = app.getProperty("Documents").toDispatch();
// 打开要转换的word文件
Dispatch doc = Dispatch.call(docs, "Open", docfile, false, true)
.toDispatch();
Dispatch selection = Dispatch.get(app, "Selection").toDispatch();
// 设置编码 65001----utf-8
Dispatch option = Dispatch.get(doc, "WebOptions").toDispatch();
Dispatch.put(option, "Encoding", 65001);
// 接受修订
Dispatch.call(doc, "AcceptAllRevisions");
/*if(htmlfile.indexOf("redHtm")==-1){*/
// 所有表格
Dispatch tables = Dispatch.get(doc, "Tables").toDispatch();
// 获取表格数目
int tablesCount = Dispatch.get(tables, "Count").toInt();
// 循环获取表格
for (int i = 1; i <= tablesCount; i++) {
// 获取第i个表格
Dispatch table = Dispatch.call(tables, "Item", new Variant(i))
.toDispatch();
Dispatch rows = Dispatch.call(table, "Rows").toDispatch();
// 设置表格不可环绕
Dispatch.put(rows, "WrapAroundText", new Variant(false));
//设置表格自动重调尺寸以适应内容
//Dispatch.put(table, "AllowAutoFit", new Variant(true));
// 设置表格居中
// Dispatch Range = Dispatch.call(table, "Range").toDispatch();
// Dispatch ParagraphFormat = Dispatch.call(Range,
// "ParagraphFormat").toDispatch(); 表格里内容居中1居中 2居右

//表格缩进0
//Dispatch.put(rows, "Alignment", 2);
Dispatch.put(rows, "LeftIndent",0.0);
Double sad = Dispatch.get(rows, "LeftIndent").toDouble();
System.out.println(sad);
Dispatch.put(rows, "LeftIndent",0.0);
//表格居中
Dispatch.put(rows, "Alignment", 1); 
Dispatch columns = Dispatch.call(table, "Columns").toDispatch();
int rowsCount = Dispatch.get(rows, "Count").toInt();

int columnsCount = Dispatch.get(columns, "Count").toInt();

//竖排文字变横排

for(int j=1;j<=rowsCount;j++){
for(int k=1;k<=columnsCount;k++){
try {
getTxtFromCell(selection,doc,i,j,k);
} catch (Exception e) {

}
}
}
}
Dispatch.call(doc, "AcceptAllRevisions");
// 另存htm
Dispatch.call(doc, "SaveAs", htmlfile, WORD_HTML);
Dispatch.call(doc, "Close", false);
app.invoke("Quit", 0);
boolean mvimage=mvImage(htmlfile);
if(!mvimage){
System.out.println("图片处理:"+htmlfile+"---出错");
return false;
}
return true;
} catch (Exception e) {
return false;
}finally {

// 关闭word应用程序
try {
app.invoke("Quit", new Variant[] {});
} catch (Exception e) {

}}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值