jacob 另存word ,excel 为html,xml格式
2017-10-30 10:07
357 查看
import java.util.Random;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
public class JacobUtil {
public static final int WORD_HTML = 8;
public static final int WORD_TXT = 7;
public static final int EXCEL_HTML = 44;
public static final int EXCEL_XML = 46;
public static final int EXCEL_43 = 43;
// Excel 2003 测试可用
/**
* WORD转HTML
*
* @param docfile
* WORD文件全路径
* @param htmlfile
* 转换后HTML存放路径
*/
public static void wordToHtml(String docfile, String htmlfile) {
// 初始化
ComThread.InitSTA();
ActiveXComponent app = new ActiveXComponent("Word.Application"); // 启动word
try {
app.setProperty("Visible", new Variant(false));
Dispatch docs = app.getProperty("Documents").toDispatch();
Dispatch doc = Dispatch.invoke(
docs,
"Open",
Dispatch.Method,
new Object[] { docfile, new Variant(false),
new Variant(true) }, new int[1]).toDispatch();
Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] {
htmlfile, new Variant(WORD_HTML) }, new int[1]);
Variant f = new Variant(false);
Dispatch.call(doc, "Close", f);
} catch (Exception e) {
e.printStackTrace();
} finally {
app.invoke("Quit", new Variant[] {});
ComThread.Release();
}
}
/**
* EXCEL转HTML
*
* @param xlsfile
* EXCEL文件全路径
* @param htmlfile
* 转换后HTML存放路径
*/
public static void excelToHtml(String xlsfile, String htmlfile) {
// 初始化
ComThread.InitSTA();
ActiveXComponent app = new ActiveXComponent("Excel.Application"); // 启动Excel
try {
app.setProperty("Visible", new Variant(false));
Dispatch excels = app.getProperty("Workbooks").toDispatch();
Dispatch excel = Dispatch.invoke(
excels,
"Open",
Dispatch.Method,
new Object[] { xlsfile, new Variant(false),
new Variant(true) }, new int[1]).toDispatch();
Dispatch.invoke(excel, "SaveAs", Dispatch.Method, new Object[] {
htmlfile, new Variant(EXCEL_HTML) }, new int[1]);
Dispatch.call(excel, "Close", new Variant(false));
} catch (Exception e) {
e.printStackTrace();
} finally {
app.invoke("Quit", new Variant[] {});
ComThread.Release();
}
}
/**
* EXCEL转XML
*
* @param xlsfile
* EXCEL文件全路径
* @param xmlfile
* 转换后XML存放路径
*/
public static void excelToXml(String xlsfile, String xmlfile) {
// 初始化
ComThread.InitSTA();
ActiveXComponent app = new ActiveXComponent("Excel.Application"); // 启动Excel
try {
app.setProperty("Visible", new Variant(false));
Dispatch excels = app.getProperty("Workbooks").toDispatch();
Dispatch excel = Dispatch.invoke(
excels,
"Open",
Dispatch.Method,
new Object[] { xlsfile, new Variant(false),
new Variant(true) }, new int[1]).toDispatch();
Dispatch.invoke(excel, "SaveAs", Dispatch.Method, new Object[] {
xmlfile, new Variant(EXCEL_XML) }, new int[1]);
Variant f = new Variant(false);
Dispatch.call(excel, "Close", f);
} catch (Exception e) {
e.printStackTrace();
} finally {
app.invoke("Quit", new Variant[] {});
ComThread.Release();
}
}
public static void main(String[] args) {
excelToHtml(
"E:\\test.xls",
"E:\\" + new Random().nextInt(1000) + ".html");
}
}
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
public class JacobUtil {
public static final int WORD_HTML = 8;
public static final int WORD_TXT = 7;
public static final int EXCEL_HTML = 44;
public static final int EXCEL_XML = 46;
public static final int EXCEL_43 = 43;
// Excel 2003 测试可用
/**
* WORD转HTML
*
* @param docfile
* WORD文件全路径
* @param htmlfile
* 转换后HTML存放路径
*/
public static void wordToHtml(String docfile, String htmlfile) {
// 初始化
ComThread.InitSTA();
ActiveXComponent app = new ActiveXComponent("Word.Application"); // 启动word
try {
app.setProperty("Visible", new Variant(false));
Dispatch docs = app.getProperty("Documents").toDispatch();
Dispatch doc = Dispatch.invoke(
docs,
"Open",
Dispatch.Method,
new Object[] { docfile, new Variant(false),
new Variant(true) }, new int[1]).toDispatch();
Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] {
htmlfile, new Variant(WORD_HTML) }, new int[1]);
Variant f = new Variant(false);
Dispatch.call(doc, "Close", f);
} catch (Exception e) {
e.printStackTrace();
} finally {
app.invoke("Quit", new Variant[] {});
ComThread.Release();
}
}
/**
* EXCEL转HTML
*
* @param xlsfile
* EXCEL文件全路径
* @param htmlfile
* 转换后HTML存放路径
*/
public static void excelToHtml(String xlsfile, String htmlfile) {
// 初始化
ComThread.InitSTA();
ActiveXComponent app = new ActiveXComponent("Excel.Application"); // 启动Excel
try {
app.setProperty("Visible", new Variant(false));
Dispatch excels = app.getProperty("Workbooks").toDispatch();
Dispatch excel = Dispatch.invoke(
excels,
"Open",
Dispatch.Method,
new Object[] { xlsfile, new Variant(false),
new Variant(true) }, new int[1]).toDispatch();
Dispatch.invoke(excel, "SaveAs", Dispatch.Method, new Object[] {
htmlfile, new Variant(EXCEL_HTML) }, new int[1]);
Dispatch.call(excel, "Close", new Variant(false));
} catch (Exception e) {
e.printStackTrace();
} finally {
app.invoke("Quit", new Variant[] {});
ComThread.Release();
}
}
/**
* EXCEL转XML
*
* @param xlsfile
* EXCEL文件全路径
* @param xmlfile
* 转换后XML存放路径
*/
public static void excelToXml(String xlsfile, String xmlfile) {
// 初始化
ComThread.InitSTA();
ActiveXComponent app = new ActiveXComponent("Excel.Application"); // 启动Excel
try {
app.setProperty("Visible", new Variant(false));
Dispatch excels = app.getProperty("Workbooks").toDispatch();
Dispatch excel = Dispatch.invoke(
excels,
"Open",
Dispatch.Method,
new Object[] { xlsfile, new Variant(false),
new Variant(true) }, new int[1]).toDispatch();
Dispatch.invoke(excel, "SaveAs", Dispatch.Method, new Object[] {
xmlfile, new Variant(EXCEL_XML) }, new int[1]);
Variant f = new Variant(false);
Dispatch.call(excel, "Close", f);
} catch (Exception e) {
e.printStackTrace();
} finally {
app.invoke("Quit", new Variant[] {});
ComThread.Release();
}
}
public static void main(String[] args) {
excelToHtml(
"E:\\test.xls",
"E:\\" + new Random().nextInt(1000) + ".html");
}
}
相关文章推荐
- 通过jacob 把word转pdf ,excel转html,word转html
- jacob读取word excel,转换成html
- 利用jacob将excel另存为html格式文件,利用jacob将word另存为html格式文件
- 关于java使用jacob调用word,ppt,excel等的转化为html的问题
- [JAVA]: Jacob 转换Word,Excel为HTML
- 运用 Jacob转换Word,Excel为HTML
- java使用jacob将word,excel,ppt转成html
- Jacob 将word和excel转换成html
- jacob包进行word转html,excel转html
- word&excel转换成html
- asp.net导出excel-一行代码实现excel、xml、pdf、word、html、csv等7种格式文件导出功能而且美观-SNF快速开发平台
- Java利用OpenOffice进行将word,excel转化成html解析到界面(上)
- js将html中的内容导出word、或者excel文件的方法
- 导出excel,word,html
- JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法示例
- C# 将Word,Excel转换成Html
- 将PPT&WORD&EXCEL转换成html格式
- JACOB调用WORD书签修改WORD文档,并生成html显示到页面中
- 用jacob实现word到html的转换
- C#导出Word或Excel文件总显示Html标记