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 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[] {});
        }
    }
}  

 

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页