打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
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;            }        }    }
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
java 使用jacob+jacob工具类实现html页面导出word
javadoc
jacob
office转换成pdf
Excel转换成html
VC读写EXCEL文件类(已安装excel)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服