java调用jacob生成pdf,word,excel横向
/* * 传进一个office文件的byte[]以及后缀,生成一个pdf文件的byte[] */ public byte[] jacob_Office2Pdf(byte[] srcFileBytes, String postfix) { if (srcFileBytes == null || srcFileBytes.length == 0 || postfix.equals("") || postfix == null) { return null; } else { String officeTmplPath = Consts.getTempPath() + UUID.randomUUID().toString() + "." + postfix; System.out.println(officeTmplPath); String pdfTmplPath = generateDefaultOutputFilePath(officeTmplPath); System.out.println(pdfTmplPath); FileOutputStream outf = null; BufferedOutputStream bufferout = null; try { outf = new FileOutputStream(officeTmplPath); bufferout = new BufferedOutputStream(outf); bufferout.write(srcFileBytes); bufferout.flush(); bufferout.close(); outf.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } File f = new File(officeTmplPath); if (postfix.equalsIgnoreCase("doc") || postfix.equalsIgnoreCase("docx")) { ComThread.InitSTA(); ActiveXComponent app = new ActiveXComponent("Word.Application"); app.setProperty("Visible", false); Dispatch docs = app.getProperty("Documents").toDispatch(); Dispatch doc = Dispatch.call(docs,// "Open", // officeTmplPath,// FileName false,// ConfirmConversions true // ReadOnly ).toDispatch(); Dispatch.call(doc,// "SaveAs", // pdfTmplPath, // FileName wdFormatPDF); Dispatch.call(doc, "Close", false); if (app != null) { app.invoke("Quit", new Variant[] {}); app = null; } ComThread.Release(); if (f.exists()) { f.delete(); } byte[] content; try { BufferedInputStream in = new BufferedInputStream( new FileInputStream(pdfTmplPath)); ByteArrayOutputStream out = new ByteArrayOutputStream(1024); byte[] temp = new byte[1024]; int size = 0; while ((size = in.read(temp)) != -1) { out.write(temp, 0, size); } in.close(); content = out.toByteArray(); out.close(); File pdfFile = new File(pdfTmplPath); if (pdfFile.exists()) { pdfFile.delete(); } return content; } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } } else if (postfix.equalsIgnoreCase("xls") || postfix.equalsIgnoreCase("xlsx")) { ComThread.InitSTA(); ActiveXComponent app = new ActiveXComponent("Excel.Application"); app.setProperty("Visible", new Variant(false)); Object excels = app.getProperty("Workbooks").toDispatch(); Object excel = Dispatch.invoke( (Dispatch) excels, "Open", Dispatch.Method, new Object[] { officeTmplPath, new Variant(false), new Variant(true) }, new int[1]).toDispatch(); // new Object[] { officeTmplPath, new Variant(false), // new Variant(false) }, new int[9]).toDispatch(); // 横向打印(2013/05/24) // 获取activate表格 Dispatch currentSheet = Dispatch.get((Dispatch) excel, "ActiveSheet").toDispatch(); Dispatch pageSetup = Dispatch.get(currentSheet, "PageSetup") .toDispatch(); Dispatch.put(pageSetup, "Orientation", new Variant(2)); // Dispatch.invoke((Dispatch)excel, "SaveAs", Dispatch.Method, // new Object[] { // pdfTmplPath, new Variant(57), new Variant(false), // new Variant(57), new Variant(57), new Variant(false), // new Variant(true), new Variant(57), new Variant(false), // new Variant(true), new Variant(false) }, new int[1]); try { //如果第一个sheet为空则会抛出异常 Dispatch.call(currentSheet, "SaveAs", pdfTmplPath, new Variant(57)); } catch (Exception e1) { // TODO Auto-generated catch block //e1.printStackTrace(); //自动调用第二个sheet Dispatch sheets = Dispatch.get((Dispatch) excel, "Sheets") .toDispatch(); // 获得几个sheet // int count = Dispatch.get(sheets, "Count").getInt(); // System.out.println(count); Dispatch sheet = Dispatch.invoke(sheets, "Item", Dispatch.Get, new Object[] { new Integer(2) }, new int[1]).toDispatch(); pageSetup = Dispatch.get(sheet, "PageSetup").toDispatch(); Dispatch.put(pageSetup, "Orientation", new Variant(2)); Dispatch.call(sheet, "SaveAs", pdfTmplPath, new Variant(57)); Dispatch.call((Dispatch) excel, "Close", new Variant(false)); byte[] content; try { BufferedInputStream in = new BufferedInputStream( new FileInputStream(pdfTmplPath)); ByteArrayOutputStream out = new ByteArrayOutputStream( 1024); byte[] temp = new byte[1024]; int size = 0; while ((size = in.read(temp)) != -1) { out.write(temp, 0, size); } in.close(); content = out.toByteArray(); out.close(); File pdfFile = new File(pdfTmplPath); if (pdfFile.exists()) { pdfFile.delete(); } return content; } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } } finally { if (app != null) { app.invoke("Quit", new Variant[] {}); app = null; } ComThread.Release(); if (f.exists()) { f.delete(); } } Dispatch.call((Dispatch) excel, "Close", new Variant(false)); byte[] content; try { BufferedInputStream in = new BufferedInputStream( new FileInputStream(pdfTmplPath)); ByteArrayOutputStream out = new ByteArrayOutputStream(1024); byte[] temp = new byte[1024]; int size = 0; while ((size = in.read(temp)) != -1) { out.write(temp, 0, size); } in.close(); content = out.toByteArray(); out.close(); File pdfFile = new File(pdfTmplPath); if (pdfFile.exists()) { pdfFile.delete(); } return content; } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } } else { if (f.exists()) { f.delete(); } return null; } } }