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

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

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

需求

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

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

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

方案:

  • windows server下用 jacob, windows上可完美運行, 但是!?。inux上不支持jacob?。。。ǚ浅V匾。。?/li>
  • poi + iText,貌似不錯
  • openoffice,跨平臺

本文選用openoffice,各位看官可自選用iText + poi,自己判別!當然也可以用jacob,前提是你不需要在Linux中運行項目。

安裝軟件:下載傳送門

win:

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

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

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

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


windows下安裝完畢!

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

linux:下載傳送門

根據(jù)自己的Linux系統(tǒng)選擇RPM包或者DEB,如果是Ubuntu則選DEB,其他就選RPM包,查看當前操作系統(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

[注釋:運行下面命令, 生成  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, 安裝完畢了! 啟動服務(wù)
進入到默認安裝目錄下

> cd /opt/openoffice4/program/
[注釋:臨時啟動]
> /opt/openoffice4/program/soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard
[注釋:或后臺啟動]
> /opt/openoffice4/program/soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard &

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

[注釋:查看是否啟動成功]
> 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
     *            遠程調(diào)用ip
     * @param connectPort
     *            遠程調(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默認是A4紙
//        DocumentConverter converter = getConverter(connectIp, connection);
//        converter.convert(new File(inputFilePath), new File(outputFilePath));

// 		  TODO Excel轉(zhuǎn)成PDF默認是A4紙, 如果現(xiàn)實折行,則自定義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)換文件的擴展名,例如: xls,doc
     * @param outputStream
     * @param outputFileExtension
     *            輸出文件擴展名,例如: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)換文件的擴展名,例如: xls,doc
     * @param outputStream
     * @param outputFileExtension
     *            輸出文件擴展名,例如:pdf
     * @param connectIp
     *            遠程調(diào)用ip
     * @param connectPort
     *            遠程調(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)圖片功能實現(xiàn)的文章就介紹到這了,更多相關(guān)java?openoffice?Excel轉(zhuǎn)PDF內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • springboot如何配置ssl支持https

    springboot如何配置ssl支持https

    在SpringBoot應(yīng)用中配置SSL支持HTTPS需要創(chuàng)建KeyStore并在application.yml中進行相應(yīng)配置,首先,使用java的keytool工具創(chuàng)建KeyStore,這涉及到設(shè)置密鑰對、指定密鑰算法(RSA)、密鑰大小(2048位)、密鑰庫名稱、證書有效期等,創(chuàng)建KeyStore后
    2024-10-10
  • Spring MVC項目中l(wèi)og4J和AOP使用詳解

    Spring MVC項目中l(wèi)og4J和AOP使用詳解

    項目日志記錄是項目開發(fā)、運營必不可少的內(nèi)容,有了它可以對系統(tǒng)有整體的把控,出現(xiàn)任何問題都有蹤跡可尋。下面這篇文章主要給大家介紹了關(guān)于Spring MVC項目中l(wèi)og4J和AOP使用的相關(guān)資料,需要的朋友可以參考下。
    2017-12-12
  • 詳解springboot解決CORS跨域的三種方式

    詳解springboot解決CORS跨域的三種方式

    本文主要介紹了詳解springboot解決CORS跨域的三種方式,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • Redis中高并發(fā)讀寫性能的深度解析與優(yōu)化

    Redis中高并發(fā)讀寫性能的深度解析與優(yōu)化

    Redis 作為一款高性能的內(nèi)存數(shù)據(jù)庫,廣泛應(yīng)用于緩存、消息隊列、實時統(tǒng)計等場景,本文將深入探討 Redis 的讀寫并發(fā)能力,感興趣的小伙伴可以了解下
    2025-03-03
  • 淺談SpringBoot 中關(guān)于自定義異常處理的套路

    淺談SpringBoot 中關(guān)于自定義異常處理的套路

    這篇文章主要介紹了淺談SpringBoot 中關(guān)于自定義異常處理的套路,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • 詳解Gradle安裝并配置到IDEA的方法

    詳解Gradle安裝并配置到IDEA的方法

    這篇文章主要介紹了Gradle安裝并配置到IDEA的方法,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-09-09
  • Spring Boot 實現(xiàn)敏感詞及特殊字符過濾處理

    Spring Boot 實現(xiàn)敏感詞及特殊字符過濾處理

    這篇文章主要介紹了SpringBoot實現(xiàn)敏感詞及特殊字符過濾處理方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • 基礎(chǔ)不牢,地動山搖,Java基礎(chǔ)速來刷刷

    基礎(chǔ)不牢,地動山搖,Java基礎(chǔ)速來刷刷

    基礎(chǔ)不牢,地動山搖,快來一起學(xué)習(xí)一下基礎(chǔ)吧,不斷地學(xué)習(xí)就算是基礎(chǔ)也會有新的認知和收獲,加油
    2021-08-08
  • Javafx實現(xiàn)國際象棋游戲

    Javafx實現(xiàn)國際象棋游戲

    這篇文章主要為大家詳細介紹了Javafx實現(xiàn)國際象棋游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-05-05
  • JavaWeb實現(xiàn)郵件發(fā)送接收功能

    JavaWeb實現(xiàn)郵件發(fā)送接收功能

    這篇文章主要為大家詳細介紹了JavaWeb郵件發(fā)送接收功能的實現(xiàn),郵件發(fā)送和接收功能是非常常用的功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2015-12-12

最新評論