客户要求上传的doc和xls附件能直接打开预览,不要下载后才能打开的。想到将如果将doc转成HTML不就OK 了吗?网上还真找到了转换之法:jacob。
此例演示在jdk6下将word、excel和ppt文件转成html文件。
1.首先需要jacob包。可以从http://danadler.com/jacob/上取的。
2.解压得到两个文件jacob.jar(直接放到classpath下去就行了),还有一个jacob.dll放到 system32或者
jdk安装目录的jdk/jre/bin目录中就OK了。
3.传说如果是win2003还需要安装一个东西。http://www.microsoft.com/downloads/details.aspx?familyid=200B2FD9-AE1A-4A14-984D-389C36F85647&displaylang=en&displaylang=en
不过我的是xp,不需要。
4.貌似需要安装offices,因为jacob是调用微软offices去转化文件的。(不知道有什么替代的办法没有)
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
public class Jacob2Html {
private static final Log log = LogFactory.getLog(Jacob2Html.class);
/**
* PowerPoint转成HTML
*
* @param pptPath
* PowerPoint文件全路径
* @param htmlfile
* 转换后HTML存放路径
*/
public static Boolean pptToHtml(String pptPath, String htmlPath) {
Boolean b = false;
ActiveXComponent offCom = new ActiveXComponent("PowerPoint.Application");
try {
offCom.setProperty("Visible", new Variant(true));
Dispatch excels = offCom.getProperty("Presentations").toDispatch();
Dispatch excel = Dispatch.invoke(
excels,
"Open",
Dispatch.Method,
new Object[] { pptPath, new Variant(false),
new Variant(false) }, new int[1]).toDispatch();
Dispatch.invoke(excel, "SaveAs", Dispatch.Method, new Object[] {
htmlPath, new Variant(12) }, new int[1]);
Variant f = new Variant(false);
Dispatch.call(excel, "Close");
b=true;
} catch (Exception e) {
e.printStackTrace();
} finally {
offCom.invoke("Quit", new Variant[] {});
ComThread.Release();
}
return b;
}
/**
* WORD转成HTML
*
* @param wordPath
* WORD文件全路径
* @param htmlPath
* 生成的HTML存放路径
*/
public static Boolean wordToHtml(String wordPath, String htmlPath) {
Boolean b = false;
ActiveXComponent offCom = new ActiveXComponent("Word.Application");
try {
offCom.setProperty("Visible", new Variant(false));
Dispatch wordDis = offCom.getProperty("Documents").toDispatch();
Dispatch doc = Dispatch.invoke(
wordDis,
"Open",
Dispatch.Method,
new Object[] { wordPath, new Variant(true),
new Variant(true) }, new int[1]).toDispatch();
Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] {
htmlPath, new Variant(8) }, new int[1]);
Variant f = new Variant(false);
Dispatch.call(doc, "Close", f);
b=true;
} catch (Exception e) {
e.printStackTrace();
} finally {
offCom.invoke("Quit", new Variant[] {});
}
return b;
}
/**
* EXCEL转成HTML
*
* @param xlsfile
* EXCEL文件全路径
* @param htmlfile
* 转换后HTML存放路径
*/
public static Boolean excelToHtml(String excelPath, String htmlPath) {
Boolean b = false;
ActiveXComponent offCom = new ActiveXComponent("Excel.Application");
try {
offCom.setProperty("Visible", new Variant(false));
Dispatch excels = offCom.getProperty("Workbooks").toDispatch();
Dispatch excel = Dispatch.invoke(
excels,
"Open",
Dispatch.Method,
new Object[] { excelPath, new Variant(false),
new Variant(true) }, new int[1]).toDispatch();
Dispatch.invoke(excel, "SaveAs", Dispatch.Method, new Object[] {
htmlPath, new Variant(44) }, new int[1]);
Variant f = new Variant(false);
Dispatch.call(excel, "Close", f);
b=true;
} catch (Exception e) {
e.printStackTrace();
} finally {
offCom.invoke("Quit", new Variant[] {});
ComThread.Release();
}
return b;
}
public static void main(String[] args) {
wordToHtml("c:/a.doc","c:/a.html");
// pptToHtml("D:/test/1ppt.ppt", "D:/test/1ppt.html");
}
}