jacob包进行word转html,excel转html
2009-01-08 11:11
393 查看
最近要用到把word文档转成html形式。研究了一下
1.下载jacob包(我用的是1.9),包含jacob.jar,jacob.dll。在工程中引用jacob.jar,jacob.dll放在 系统盘:/windows/system32
附带jacob_1.9下载
2.代码
/*
* Jacob.java 2009-01-09
*
* Powered by HELEN
* All rights reserved
*/
import java.io.File;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
/**
* <code>Jacob</code>jacob包进行word转html,excel转html
*
* @author Helen
* @version 2009-01-09
* @see java.lang.Class
* @since JDK1.6.0_03
*/
public class Jacob {
public static void main(String[] args) throws Exception {
wordtoHTML("E:/w.doc","E:/w.html");
exceltoHTML("E:/b.xls","E:/b.html");
}
/**
* <code>wordtoHTML</code>word转换成html
* @param excelFilename 要转换的word文件 htmlFilename 目标文件
* @author Helen
* @version 2009-01-06
* @see java.lang.Class
* @since JDK1.6.0_03
*/
public static void wordtoHTML(String excelFilename, String htmlFilename) {
ActiveXComponent app = new ActiveXComponent("Word.Application");// 查找word组件
try {
app.setProperty("Visible", new Variant(false));// 设置word不可见
Dispatch docs = app.getProperty("Documents").toDispatch();
Dispatch doc = Dispatch.invoke(
docs,
"Open",
Dispatch.Method,
new Object[] { excelFilename, new Variant(false),
new Variant(true) }, new int[1]).toDispatch();// 打开word文件,注意这里第三个参数要设为true,这个参数表示是否以只读方式打开,因为我们不用保存原文件,所以以只读方式打开,如果你想进行读写,那么就得设为false。
Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] {
htmlFilename, new Variant(8) }, new int[1]);//作为html格式保存到目标文件(html-new Variant(8) txt-new Variant(2))
Variant f = new Variant(false);
Dispatch.call(doc, "Close", f); //关闭word文件
} catch (Exception e) {
e.printStackTrace();
} finally {
app.invoke("Quit", new Variant[] {});
}
}
/**
* <code>exceltoHTML</code>excel转换成html
* @param excelFilename 要转换的excel文件 htmlFilename 目标文件
* @author Helen
* @version 2009-01-06
* @see java.lang.Class
* @since JDK1.6.0_03
*/
public static void exceltoHTML(String excelFilename, String htmlFilename)
{
ComThread.InitSTA();
ActiveXComponent xl = new ActiveXComponent("Excel.Application");
try
{
File fo = new File(htmlFilename);
if (fo.exists()&&fo.canRead()){
fo.delete();
}
xl.setProperty("Visible", new Variant(false));
Dispatch workbooks = xl.getProperty("Workbooks").toDispatch();
Dispatch workbook = Dispatch.invoke(workbooks, "Open", Dispatch.Method, new Object[]
{excelFilename, new Variant(false), new Variant(true) },// 是否以只读方式打开
new int[1]).toDispatch();
Dispatch.invoke(workbook, "SaveAs", Dispatch.Method, new Object[]
{htmlFilename, new Variant(44) }, new int[1]); // 44,45
Dispatch.call((Dispatch) workbook, "Close", new Variant(false));
} catch (Exception e)
{
e.printStackTrace();
} finally
{
xl.invoke("Quit", new Variant[]
{});
ComThread.Release();
}
}
}
1.下载jacob包(我用的是1.9),包含jacob.jar,jacob.dll。在工程中引用jacob.jar,jacob.dll放在 系统盘:/windows/system32
附带jacob_1.9下载
2.代码
/*
* Jacob.java 2009-01-09
*
* Powered by HELEN
* All rights reserved
*/
import java.io.File;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
/**
* <code>Jacob</code>jacob包进行word转html,excel转html
*
* @author Helen
* @version 2009-01-09
* @see java.lang.Class
* @since JDK1.6.0_03
*/
public class Jacob {
public static void main(String[] args) throws Exception {
wordtoHTML("E:/w.doc","E:/w.html");
exceltoHTML("E:/b.xls","E:/b.html");
}
/**
* <code>wordtoHTML</code>word转换成html
* @param excelFilename 要转换的word文件 htmlFilename 目标文件
* @author Helen
* @version 2009-01-06
* @see java.lang.Class
* @since JDK1.6.0_03
*/
public static void wordtoHTML(String excelFilename, String htmlFilename) {
ActiveXComponent app = new ActiveXComponent("Word.Application");// 查找word组件
try {
app.setProperty("Visible", new Variant(false));// 设置word不可见
Dispatch docs = app.getProperty("Documents").toDispatch();
Dispatch doc = Dispatch.invoke(
docs,
"Open",
Dispatch.Method,
new Object[] { excelFilename, new Variant(false),
new Variant(true) }, new int[1]).toDispatch();// 打开word文件,注意这里第三个参数要设为true,这个参数表示是否以只读方式打开,因为我们不用保存原文件,所以以只读方式打开,如果你想进行读写,那么就得设为false。
Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] {
htmlFilename, new Variant(8) }, new int[1]);//作为html格式保存到目标文件(html-new Variant(8) txt-new Variant(2))
Variant f = new Variant(false);
Dispatch.call(doc, "Close", f); //关闭word文件
} catch (Exception e) {
e.printStackTrace();
} finally {
app.invoke("Quit", new Variant[] {});
}
}
/**
* <code>exceltoHTML</code>excel转换成html
* @param excelFilename 要转换的excel文件 htmlFilename 目标文件
* @author Helen
* @version 2009-01-06
* @see java.lang.Class
* @since JDK1.6.0_03
*/
public static void exceltoHTML(String excelFilename, String htmlFilename)
{
ComThread.InitSTA();
ActiveXComponent xl = new ActiveXComponent("Excel.Application");
try
{
File fo = new File(htmlFilename);
if (fo.exists()&&fo.canRead()){
fo.delete();
}
xl.setProperty("Visible", new Variant(false));
Dispatch workbooks = xl.getProperty("Workbooks").toDispatch();
Dispatch workbook = Dispatch.invoke(workbooks, "Open", Dispatch.Method, new Object[]
{excelFilename, new Variant(false), new Variant(true) },// 是否以只读方式打开
new int[1]).toDispatch();
Dispatch.invoke(workbook, "SaveAs", Dispatch.Method, new Object[]
{htmlFilename, new Variant(44) }, new int[1]); // 44,45
Dispatch.call((Dispatch) workbook, "Close", new Variant(false));
} catch (Exception e)
{
e.printStackTrace();
} finally
{
xl.invoke("Quit", new Variant[]
{});
ComThread.Release();
}
}
}
相关文章推荐
- jacob 另存word ,excel 为html,xml格式
- [JAVA]: Jacob 转换Word,Excel为HTML
- 使用jacob对word.excel文件进行完美解析(缺点是不支持linux)
- Java利用OpenOffice进行将word,excel转化成html解析到界面(下)
- 关于java使用jacob调用word,ppt,excel等的转化为html的问题
- 运用 Jacob转换Word,Excel为HTML
- java平台下通过jacob对excel,word进行打印等操作
- 通过jacob 把word转pdf ,excel转html,word转html
- java平台下通过jacob对excel,word进行打印等操作
- 利用jacob将excel另存为html格式文件,利用jacob将word另存为html格式文件
- Java利用OpenOffice进行将word,excel转化成html解析到界面(上)
- jacob读取word excel,转换成html
- 使用maven管理jacob.jar对excel、word文档进行转换为html细节描述
- java平台下通过jacob对excel,word进行打印等操作
- java使用jacob将word,excel,ppt转成html
- Jacob 将word和excel转换成html
- datatable导出到Word / Excel / PDF / HTML .NET
- JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法示例 .
- Word/Excel/PDF文件转换成HTML整理
- html 导入到word、excel中