欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JAVA中使用openoffice將Excel轉(zhuǎn)PDF再轉(zhuǎn)圖片功能的實(shí)現(xiàn)代碼

 更新時(shí)間:2021年12月14日 10:55:20   作者:小漆同學(xué)  
這篇文章主要介紹了JAVA中使用openoffice將Excel轉(zhuǎn)PDF再轉(zhuǎn)圖片功能實(shí)現(xiàn),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

需求

公司一個(gè)小項(xiàng)目要結(jié)尾了, 有非常多的表格, 而且非常復(fù)雜, 例如

例如:(這表格, 有想死的心…)

看到這樣的表格,第一反應(yīng)是n臉懵逼,這得做到啥時(shí)候啊,最后想到用一個(gè)方法,讓客戶自己玩?。?br /> 平臺(tái)提供Excel模版下載,客戶下載填寫完再提交Excel,平臺(tái)再將Excel轉(zhuǎn)PDF,順便將PDF轉(zhuǎn)成圖片,給前端展示,想法簡單,方案可行,開工!

方案:

  • windows server下用 jacob, windows上可完美運(yùn)行, 但是!??!Linux上不支持jacob?。。。ǚ浅V匾。。?/li>
  • poi + iText,貌似不錯(cuò)
  • openoffice,跨平臺(tái)

本文選用openoffice,各位看官可自選用iText + poi,自己判別!當(dāng)然也可以用jacob,前提是你不需要在Linux中運(yùn)行項(xiàng)目。

安裝軟件:下載傳送門

win:

下載完成后,一路next,最好默認(rèn)路徑安裝,免得折騰
完成后 Win + R 填入 Dcomcnfg 打開組件服務(wù)

設(shè)置openoffice組件服務(wù), 都設(shè)成SYSTEM

再設(shè)置標(biāo)識(shí)為:交互式用戶

進(jìn)入到安裝目錄,如果路徑是默認(rèn)的,則在 C:\Program Files (x86)\OpenOffice 4\program 下面,
進(jìn)到這個(gè)路徑下,按住 shift 再點(diǎn)擊鼠標(biāo)右鍵,在此處打開命令窗口,輸入 soffice -headless-accept=“socket,host=127.0.0.1,port=8100;urp;”-nofirststartwizard, 查看是否啟動(dòng)成功, 黑窗口輸入 netstat -ano


windows下安裝完畢!

參考: http://www.dbjr.com.cn/article/142512.htm

linux:下載傳送門

根據(jù)自己的Linux系統(tǒng)選擇RPM包或者DEB,如果是Ubuntu則選DEB,其他就選RPM包,查看當(dāng)前操作系統(tǒng)是ubuntu還是centos

[root@iZ2zeg3etf08f6snef3lgrZ ~]#  lsb_release -a
LSB Version:	:(略)
Distributor ID:	CentOS
Description:	CentOS Linux release (略)
Release:	(略)
Codename:	Core

下載完畢后,利用ssh工具上傳到服務(wù)器上!

>  tar -zxvf Apache_OpenOffice_4.1.3_Linux_x86-64_install-rpm_zh-CN.tar.gz
(略)......

> ls 
zh-CN
> cd zh-CN/RPMS
> ls
openoffice-core01-4.1.3-9783.x86_64.rpm             openoffice-impress-4.1.3-9783.x86_64.rpm        openoffice-zh-CN-4.1.3-9783.x86_64.rpm
openoffice-4.1.3-9783.x86_64.rpm                openoffice-core02-4.1.3-9783.x86_64.rpm             openoffice-javafilter-4.1.3-9783.x86_64.rpm     openoffice-zh-CN-base-4.1.3-9783.x86_64.rpm
openoffice-base-4.1.3-9783.x86_64.rpm           openoffice-core03-4.1.3-9783.x86_64.rpm             openoffice-math-4.1.3-9783.x86_64.rpm           openoffice-zh-CN-calc-4.1.3-9783.x86_64.rpm
openoffice-brand-base-4.1.3-9783.x86_64.rpm     openoffice-core04-4.1.3-9783.x86_64.rpm             openoffice-ogltrans-4.1.3-9783.x86_64.rpm       openoffice-zh-CN-draw-4.1.3-9783.x86_64.rpm
openoffice-brand-calc-4.1.3-9783.x86_64.rpm     openoffice-core05-4.1.3-9783.x86_64.rpm             openoffice-onlineupdate-4.1.3-9783.x86_64.rpm   openoffice-zh-CN-help-4.1.3-9783.x86_64.rpm
openoffice-brand-draw-4.1.3-9783.x86_64.rpm     openoffice-core06-4.1.3-9783.x86_64.rpm             openoffice-ooofonts-4.1.3-9783.x86_64.rpm       openoffice-zh-CN-impress-4.1.3-9783.x86_64.rpm
openoffice-brand-impress-4.1.3-9783.x86_64.rpm  openoffice-core07-4.1.3-9783.x86_64.rpm             openoffice-ooolinguistic-4.1.3-9783.x86_64.rpm  openoffice-zh-CN-math-4.1.3-9783.x86_64.rpm
openoffice-brand-math-4.1.3-9783.x86_64.rpm     openoffice-draw-4.1.3-9783.x86_64.rpm               openoffice-pyuno-4.1.3-9783.x86_64.rpm          openoffice-zh-CN-res-4.1.3-9783.x86_64.rpm
openoffice-brand-writer-4.1.3-9783.x86_64.rpm   openoffice-gnome-integration-4.1.3-9783.x86_64.rpm  openoffice-ure-4.1.3-9783.x86_64.rpm            openoffice-zh-CN-writer-4.1.3-9783.x86_64.rpm
openoffice-brand-zh-CN-4.1.3-9783.x86_64.rpm    openoffice-graphicfilter-4.1.3-9783.x86_64.rpm      openoffice-writer-4.1.3-9783.x86_64.rpm
openoffice-calc-4.1.3-9783.x86_64.rpm           openoffice-images-4.1.3-9783.x86_64.rpm             openoffice-xsltfilter-4.1.3-9783.x86_64.rpm

[注釋:運(yùn)行下面命令, 生成  desktop-integration包]
>  yum localinstall *.rpm
> (略...)
> cd desktop-integration/
> ls
>  openoffice4.1.3-freedesktop-menus-4.1.3-9783.noarch.rpm  
   openoffice4.1.3-mandriva-menus-4.1.3-9783.noarch.rpm 
   openoffice4.1.3-redhat-menus-4.1.3-9783.noarch.rpm  
   openoffice4.1.3-suse-menus-4.1.3-9783.noarch.rpm

>  yum localinstall  openoffice4.1.3-redhat-menus-4.1.3-9783.noarch.rpm  

ok, 安裝完畢了! 啟動(dòng)服務(wù)
進(jìn)入到默認(rèn)安裝目錄下

> cd /opt/openoffice4/program/
[注釋:臨時(shí)啟動(dòng)]
> /opt/openoffice4/program/soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard
[注釋:或后臺(tái)啟動(dòng)]
> /opt/openoffice4/program/soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard &

[注釋:永久啟動(dòng)]
> nohup /opt/openoffice4/program/soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard &

[注釋:查看是否啟動(dòng)成功]
> ps -ef|grep openoffice
root      7541  7527  0 10:19 pts/0    00:00:12 /opt/openoffice4/program/soffice.bin -headless -accept=socket,host=127.0.0.1,port=8100;urp; -nofirststartwizard
root      8964  7066  0 18:18 pts/3    00:00:00 grep --color=auto openoffice

或者

> netstat -tunlp |grep 
tcp        0      0 127.0.0.1:8100          0.0.0.0:*               LISTEN      7541/soffice.bin

Linux下安裝成功!

開始擼碼:

pom.xml

		<!-- openoffice 相關(guān)依賴 -->
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>org.openoffice</groupId>
            <artifactId>juh</artifactId>
            <version>3.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.openoffice</groupId>
            <artifactId>ridl</artifactId>
            <version>3.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.openoffice</groupId>
            <artifactId>unoil</artifactId>
            <version>3.2.1</version>
        </dependency>

	    <!-- pdf2img -->
        <dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>pdfbox</artifactId>
            <version>2.0.6</version>
        </dependency>
        <dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>pdfbox-tools</artifactId>
            <version>2.0.6</version>
        </dependency>

OpenOfficeUtils

import java.awt.*;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ConnectException;

import org.apache.commons.lang.StringUtils;

import com.artofsolving.jodconverter.DefaultDocumentFormatRegistry;
import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.DocumentFormatRegistry;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
import com.artofsolving.jodconverter.openoffice.converter.StreamOpenOfficeDocumentConverter;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageTree;
import org.apache.pdfbox.rendering.ImageType;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.apache.pdfbox.tools.imageio.ImageIOUtil;

import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;

/**
 * 
 * @author josnow
 * @date 2017年5月9日 下午12:38:39
 * @version 1.0.0
 * @desc openoffice轉(zhuǎn)換工具
 */
public class OpenOfficeUtils {

    public static final String LOCAL_HOST = "localhost";
    public static final int LOCAL_PORT = 8100;

    // Format
    public static DocumentFormatRegistry formatFactory = new DefaultDocumentFormatRegistry();

    /**
     *
     * @desc
     * @auth josnow
     * @date 2017年6月9日 下午4:11:04
     * @param inputFilePath
     *            待轉(zhuǎn)換的文件路徑
     * @param outputFilePath
     *            輸出文件路徑
     */
    public static void convert(String inputFilePath, String outputFilePath) throws ConnectException {
        convert(inputFilePath, outputFilePath, LOCAL_HOST, LOCAL_PORT);
    }

    /**
     *
     * @desc
     * @auth josnow
     * @date 2017年6月9日 下午4:12:29
     * @param inputFilePath
     *            待轉(zhuǎn)換的文件路徑
     * @param outputFilePath
     *            輸出文件路徑
     * @param connectIp
     *            遠(yuǎn)程調(diào)用ip
     * @param connectPort
     *            遠(yuǎn)程調(diào)用端口
     */
    public static void convert(String inputFilePath, String outputFilePath, String connectIp, int connectPort)
            throws ConnectException {
        if (StringUtils.isEmpty(inputFilePath) || StringUtils.isEmpty(outputFilePath)
                || StringUtils.isEmpty(connectIp)) {
            throw new IllegalArgumentException("參數(shù)異常?。?);
        }
        OpenOfficeConnection connection = new SocketOpenOfficeConnection(connectIp, connectPort);
        connection.connect();
 
// 		  TODO Excel轉(zhuǎn)成PDF默認(rèn)是A4紙
//        DocumentConverter converter = getConverter(connectIp, connection);
//        converter.convert(new File(inputFilePath), new File(outputFilePath));

// 		  TODO Excel轉(zhuǎn)成PDF默認(rèn)是A4紙, 如果現(xiàn)實(shí)折行,則自定義ConverterDocument,改變紙張大小
        ConverterDocument converterDocument = new ConverterDocument(connection);
        converterDocument.convert(new File(inputFilePath), new File(outputFilePath));

        connection.disconnect();
    }

    /**
     *
     * @desc
     * @auth josnow
     * @date 2017年6月9日 下午4:08:26
     * @param inputStream
     * @param inputFileExtension
     *            待轉(zhuǎn)換文件的擴(kuò)展名,例如: xls,doc
     * @param outputStream
     * @param outputFileExtension
     *            輸出文件擴(kuò)展名,例如:pdf
     */
    public static void convert(InputStream inputStream, String inputFileExtension, OutputStream outputStream,
            String outputFileExtension) throws ConnectException {
        convert(inputStream, inputFileExtension, outputStream, outputFileExtension, LOCAL_HOST, LOCAL_PORT);
    }

    /**
     *
     * @desc
     * @auth josnow
     * @date 2017年6月9日 下午4:10:21
     * @param inputStream
     * @param inputFileExtension
     *            待轉(zhuǎn)換文件的擴(kuò)展名,例如: xls,doc
     * @param outputStream
     * @param outputFileExtension
     *            輸出文件擴(kuò)展名,例如:pdf
     * @param connectIp
     *            遠(yuǎn)程調(diào)用ip
     * @param connectPort
     *            遠(yuǎn)程調(diào)用端口
     */
    public static void convert(InputStream inputStream, String inputFileExtension, OutputStream outputStream,
            String outputFileExtension, String connectIp, int connectPort) throws ConnectException {

        if (inputStream == null || StringUtils.isEmpty(inputFileExtension) || outputStream == null
                || StringUtils.isEmpty(outputFileExtension) || StringUtils.isEmpty(connectIp)) {
            throw new IllegalArgumentException("參數(shù)異常??!");
        }

        OpenOfficeConnection connection = new SocketOpenOfficeConnection(connectIp, connectPort);
        connection.connect();
        DocumentConverter converter = getConverter(connectIp, connection);

        converter.convert(inputStream, formatFactory.getFormatByFileExtension(inputFileExtension), outputStream,
                formatFactory.getFormatByFileExtension(outputFileExtension));
        connection.disconnect();
    }

    private static DocumentConverter getConverter(String connectIp, OpenOfficeConnection connection) {
        DocumentConverter converter = "localhost".equals(connectIp) || "127.0.0.1".equals(connectIp)
                || "0:0:0:0:0:0:0:1".equals(connectIp) ? new OpenOfficeDocumentConverter(connection)
                        : new StreamOpenOfficeDocumentConverter(connection);
        return converter;
    }

    public static void main(String[] args) throws Exception {
        OpenOfficeUtils.convert("D:\\test.xls", "D:\\test.pdf");
        pdf2image(new File("D:\\test.pdf"));
    }

    /**
     * pdf to big image
     * github: https://github.com/mmtou/pdf2img
     * @param pdfFile pdf文件路徑
     * @return big image file
     * @throws Exception
     */
    public static File pdf2image(File pdfFile) throws Exception {
        // 拼成圖片后的寬度和高度
        int w = 0;
        int h = 0;
        List<BufferedImage> images = new ArrayList();
        // 生成圖片后的路徑
        String path = pdfFile.getParent() + File.separator;
        String fileName = pdfFile.getName().replace(".pdf", "");

        File destinationFile = new File(path);
        if (!destinationFile.exists()) {
            destinationFile.mkdir();
        }
        PDDocument document = PDDocument.load(pdfFile);
        PDPageTree list = document.getDocumentCatalog().getPages();
        int pageCounter = 0;
        for (PDPage page : list) {
            PDFRenderer pdfRenderer = new PDFRenderer(document);
            BufferedImage image = pdfRenderer.renderImageWithDPI(pageCounter, 100, ImageType.RGB);
            String target = path + fileName + "-" + (pageCounter++) + ".png";
            ImageIOUtil.writeImage(image, target, 100);

            w = image.getWidth();
            h += image.getHeight();
            images.add(image);
            new File(target).delete();
        }
        document.close();

        BufferedImage combined = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
        Graphics g = combined.getGraphics();

        int y = 0;
        for (BufferedImage image : images) {
            g.drawImage(image, 0, y, null);
            y += image.getHeight();
        }

        // Save as new image
        File image = new File(path, fileName + ".png");
        ImageIO.write(combined, "PNG", image);
        return image;
    }

} 

自定義 ConverterDocument

import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
import com.artofsolving.jodconverter.openoffice.converter.StreamOpenOfficeDocumentConverter;
import com.sun.star.awt.Size;
import com.sun.star.beans.PropertyValue;
import com.sun.star.lang.XComponent;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.view.PaperFormat;
import com.sun.star.view.XPrintable;

public class ConverterDocument extends StreamOpenOfficeDocumentConverter {


    public ConverterDocument(OpenOfficeConnection connection) {
        super(connection);
    }


    public final static Size A5, A4, A3;
    public final static Size B4, B5, B6;
    public final static Size KaoqinReport;

    static {
        A5 = new Size(14800, 21000);
        A4 = new Size(21000, 29700);
        A3 = new Size(29700, 42000);

        B4 = new Size(25000, 35300);
        B5 = new Size(17600, 25000);
        B6 = new Size(12500, 17600);

//        KaoqinReport = new Size(29700, 27940);  //最大限度  寬 1600000
        KaoqinReport = new Size(25000, 21000);;
    }


    @Override
    protected void refreshDocument(XComponent document) {
        super.refreshDocument(document);

        // The default paper format and orientation is A4 and portrait. To
        // change paper orientation
        // re set page size
        XPrintable xPrintable = (XPrintable) UnoRuntime.queryInterface(XPrintable.class, document);
        PropertyValue[] printerDesc = new PropertyValue[2];

        // Paper Orientation
//  printerDesc[0] = new PropertyValue();
//  printerDesc[0].Name = "PaperOrientation";
//  printerDesc[0].Value = PaperOrientation.PORTRAIT;

        // Paper Format
        printerDesc[0] = new PropertyValue();
        printerDesc[0].Name = "PaperFormat";
        printerDesc[0].Value = PaperFormat.USER;

        // Paper Size
        printerDesc[1] = new PropertyValue();
        printerDesc[1].Name = "PaperSize";
        printerDesc[1].Value = KaoqinReport;

        try {
            xPrintable.setPrinter(printerDesc);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
} 

使用:

	public static void main(String[] args) throws Exception {
        OpenOfficeUtils.convert("D:\\test.xls", "D:\\test.pdf");
        pdf2image(new File("D:\\test.pdf"));
	}

參考: https://blog.csdn.net/liuhualiang/article/details/14094019?utm_source=blogxgwz6

到此這篇關(guān)于JAVA中使用openoffice將Excel轉(zhuǎn)PDF再轉(zhuǎn)圖片功能實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)java?openoffice?Excel轉(zhuǎn)PDF內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • logback使用MDCFilter日志過濾源碼解讀

    logback使用MDCFilter日志過濾源碼解讀

    這篇文章主要介紹了logback使用MDCFilter日志過濾源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-11-11
  • 詳解SpringBoot項(xiàng)目的創(chuàng)建與單元測(cè)試

    詳解SpringBoot項(xiàng)目的創(chuàng)建與單元測(cè)試

    這篇文章主要介紹了詳解SpringBoot項(xiàng)目的創(chuàng)建與單元測(cè)試,幫助大家更好的理解和學(xué)習(xí)使用SpringBoot,感興趣的朋友可以了解下
    2021-03-03
  • Maven的概述及基本使用示例詳解

    Maven的概述及基本使用示例詳解

    MApache Maven是一個(gè)項(xiàng)目管理和構(gòu)建工具,它基于項(xiàng)目對(duì)象模型POM的概念,通過一小段描述信息來管理項(xiàng)目的構(gòu)建、報(bào)告和文檔,aven是專門用于管理和構(gòu)建Java項(xiàng)目的工具,本文給大家介紹Maven的概述及基本使用,感興趣的朋友一起看看吧
    2023-07-07
  • Spring中的父子容器原理解析

    Spring中的父子容器原理解析

    這篇文章主要為大家介紹了Spring中的父子容器原理解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-07-07
  • JavaWEB中Servlet的生命周期詳解

    JavaWEB中Servlet的生命周期詳解

    大家好,本篇文章主要講的是JavaWEB中Servlet的生命周期詳解,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下
    2022-01-01
  • Java List的remove()方法陷阱以及性能優(yōu)化

    Java List的remove()方法陷阱以及性能優(yōu)化

    Java List在進(jìn)行remove()方法是通常容易踩坑,本文就詳細(xì)的介紹一下陷阱以及性能優(yōu)化,感興趣的可以了解一下
    2021-10-10
  • 快速學(xué)會(huì)Dubbo的配置環(huán)境及相關(guān)配置

    快速學(xué)會(huì)Dubbo的配置環(huán)境及相關(guān)配置

    本文主要講解Dubbo的環(huán)境與配置,文中運(yùn)用大量代碼和圖片講解的非常詳細(xì),需要學(xué)習(xí)或用到相關(guān)知識(shí)的小伙伴可以參考這篇文章
    2021-09-09
  • Spring?component-scan?XML配置與@ComponentScan注解配置

    Spring?component-scan?XML配置與@ComponentScan注解配置

    這篇文章主要介紹了Spring?component-scan?XML配置與@ComponentScan注解配置,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-09-09
  • Java詳解表格的創(chuàng)建與使用流程

    Java詳解表格的創(chuàng)建與使用流程

    這篇文章主要介紹了怎么用Java來創(chuàng)建和使用表格,表格是我們經(jīng)常要用的工具,但是你有想過自己怎么去實(shí)現(xiàn)它嗎,感興趣的朋友跟隨文章往下看看吧
    2022-04-04
  • Java實(shí)現(xiàn)俄羅斯方塊游戲的示例代碼

    Java實(shí)現(xiàn)俄羅斯方塊游戲的示例代碼

    俄羅斯方塊是一個(gè)最初由阿列克謝帕吉特諾夫在蘇聯(lián)設(shè)計(jì)和編程的益智類視頻游戲。本文將利用Java實(shí)現(xiàn)這一經(jīng)典的小游戲,感興趣的可以動(dòng)手試一試
    2022-03-03

最新評(píng)論