最近項目需要導出一個複雜的excel,發現無論是使用 poi 還是阿里巴巴的 easyexcel 這種編程式的方式實現起來代碼很麻煩,於是採用 freemarker 的方式,做法是先將 真正的excel 另存爲 xml格式,然後 編輯此xml文件 寫 freemarker 標籤綁定數據,但是導出的 excel 文件其實還是文本格式的 xml文件,知識 office 可以識別而已,怎麼生成 真正意義上的 二進制的 xls、xlsx文件呢?我們發現用 excel 打開 上述的xml ,另存爲 xls、xlsx,生成的確實是真生的excel文件,那怎麼用java調用呢,可以使用 jacob,下面就是具體步驟:
1. 引入依賴:
<dependency> <groupId>net.sf.jacob-project</groupId> <artifactId>jacob</artifactId> <version>1.14.3</version> </dependency>
2. 將 jacob-1.14.3-x64.dll 複製到 ${ JAVA_HOME }/bin 目錄下
3. 代碼示例:
import com.jacob.activeX.ActiveXComponent;
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_XML_2_XLS = 1;
public static final int EXCEL_XML_2_XLSX = 51;
public static void main(String[] args) {
// excelToHtml( "E:\\xxx\\任務審覈信息 (74).xlsx","E:\\xxx\\任務審覈信息 (74).html" );
xml2xls( "E:\\xxx\\任務審覈信息 (74).xml","E:\\xxx\\任務審覈信息 (74).xls" );
xml2xlsx( "E:\\xxx\\任務審覈信息 (74).xml","E:\\xxx\\任務審覈信息 (74).xlsx" );
}
/**
* WORD轉HTML
* @param docfile WORD文件全路徑
* @param htmlfile 轉換後HTML存放路徑
*/
public static void wordToHtml(String docfile, String htmlfile){
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[] {});
}
}
/**
* EXCEL轉HTML
* @param xlsfile EXCEL文件全路徑
* @param htmlfile 轉換後HTML存放路徑
*/
public static void excelToHtml(String xlsfile, String htmlfile) {
ActiveXComponent app = new ActiveXComponent("Excel.Application"); // 啓動word
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]);
Variant f = new Variant(false);
Dispatch.call(excel, "Close", f);
} catch (Exception e) {
e.printStackTrace();
} finally {
app.invoke("Quit", new Variant[] {});
}
}
public static void xml2xls(String xmlfile, String xlsfile) {
ActiveXComponent app = new ActiveXComponent("Excel.Application"); // 啓動word
try {
app.setProperty("Visible", new Variant(false));
Dispatch excels = app.getProperty("Workbooks").toDispatch();
Dispatch excel = Dispatch.invoke( excels,
"Open",
Dispatch.Method,
new Object[] { xmlfile,
new Variant(false),
new Variant(true)
},
new int[1])
.toDispatch();
Dispatch.invoke( excel,
"SaveAs",
Dispatch.Method,
new Object[] { xlsfile,
new Variant( EXCEL_XML_2_XLS )
},
new int[1]);
Variant f = new Variant(false);
Dispatch.call(excel, "Close", f);
} catch (Exception e) {
e.printStackTrace();
} finally {
app.invoke("Quit", new Variant[] {});
}
}
public static void xml2xlsx(String xmlfile, String xlsxfile) {
ActiveXComponent app = new ActiveXComponent("Excel.Application"); // 啓動word
try {
app.setProperty("Visible", new Variant(false));
Dispatch excels = app.getProperty("Workbooks").toDispatch();
Dispatch excel = Dispatch.invoke( excels,
"Open",
Dispatch.Method,
new Object[] { xmlfile,
new Variant(false),
new Variant(true)
},
new int[1])
.toDispatch();
Dispatch.invoke( excel,
"SaveAs",
Dispatch.Method,
new Object[] { xlsxfile,
new Variant( EXCEL_XML_2_XLSX )
},
new int[1]);
Variant f = new Variant(false);
Dispatch.call(excel, "Close", f);
} catch (Exception e) {
e.printStackTrace();
} finally {
app.invoke("Quit", new Variant[] {});
}
}
}