java jacob調用列印,word,excel橫向列印

來源:互聯網
上載者:User

標籤:jacob

public static boolean printOfficeFile(File f) {  if (f != null && f.exists()) {   String fileNameString = f.getName();   String postfixString = Utils.getPostfix(fileNameString);   if (postfixString.equalsIgnoreCase("xls")     || postfixString.equalsIgnoreCase("xlsx")) {    /**     * 功能:實現excel列印工作     */    ComThread.InitSTA();    ActiveXComponent xl = new ActiveXComponent("Excel.Application");    try {     // System.out.println("version=" +     // xl.getProperty("Version"));     // 不開啟文檔     Dispatch.put(xl, "Visible", new Variant(false));     Dispatch workbooks = xl.getProperty("Workbooks")       .toDispatch();     // 開啟文檔     Dispatch excel = Dispatch.call(workbooks, "Open",       f.getAbsolutePath()).toDispatch();     // 橫向列印(2013/05/24)//     Dispatch currentSheet = Dispatch.get(excel, "ActiveSheet")//       .toDispatch();//     Dispatch pageSetup = Dispatch//       .get(currentSheet, "PageSetup").toDispatch();//     Dispatch.put(pageSetup, "Orientation", new Variant(2));     //每張表都橫向列印2013-10-31     Dispatch sheets = Dispatch.get((Dispatch) excel, "Sheets")       .toDispatch();     // 獲得幾個sheet     int count = Dispatch.get(sheets, "Count").getInt();//     System.out.println(count);     for (int j = 1; j <=count; j++) {      Dispatch sheet = Dispatch.invoke(sheets, "Item",        Dispatch.Get, new Object[] { new Integer(j) },        new int[1]).toDispatch();      Dispatch pageSetup = Dispatch.get(sheet, "PageSetup").toDispatch();      Dispatch.put(pageSetup, "Orientation", new Variant(2));      Dispatch.call(sheet, "PrintOut");     }     // 開始列印     if (excel != null) {      //Dispatch.call(excel, "PrintOut");      //增加以下三行代碼解決檔案無法刪除bug      Dispatch.call(excel, "save");      Dispatch.call(excel,  "Close" ,  new  Variant(true));      excel=null;     }     xl.invoke("Quit", new Variant[] {});     xl=null;     return true;    } catch (Exception e) {     e.printStackTrace();     return false;    } finally {     // 始終釋放資源     ComThread.Release();    }   } else if (postfixString.equalsIgnoreCase("doc")     || postfixString.equalsIgnoreCase("docx")) {    ComThread.InitSTA();    ActiveXComponent wd = new ActiveXComponent("Word.Application");    try {     // 不開啟文檔     Dispatch.put(wd, "Visible", new Variant(false));     Dispatch document = wd.getProperty("Documents")       .toDispatch();     // 開啟文檔     Dispatch doc = Dispatch.invoke(document, "Open",       Dispatch.Method, new Object[] { f.getAbsolutePath() },       new int[1]).toDispatch();     // 開始列印     if (doc != null) {      Dispatch.call(doc, "PrintOut");      //增加以下三行代碼解決檔案無法刪除bug      Dispatch.call(doc, "save");      Dispatch.call(doc,  "Close" ,  new  Variant(true));      doc=null;     }     wd.invoke("Quit", new Variant[] {});     wd=null;     return true;    } catch (Exception e) {     e.printStackTrace();     return false;    } finally {     // 始終釋放資源     ComThread.Release();    }   } else {    return false;   }  } else {   return false;  } }

java jacob調用列印,word,excel橫向列印

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.