1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > java 使用 jacob 实现 将 freemarker 导出的 XML 格式的 excel 转 xls xlsx 格式

java 使用 jacob 实现 将 freemarker 导出的 XML 格式的 excel 转 xls xlsx 格式

时间:2020-10-23 01:38:31

相关推荐

java 使用 jacob 实现 将 freemarker 导出的 XML 格式的 excel 转 xls xlsx 格式

最近项目上有一个导出复杂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 .Dispatch;import .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"); // 启动wordtry { 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"); // 启动wordtry {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"); // 启动wordtry {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[] {});}}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。