1. 程式人生 > >openoffice轉excel為pdf檔案,根據excel檔案大小設定pdf頁面大小,只適用一個sheet的情況

openoffice轉excel為pdf檔案,根據excel檔案大小設定pdf頁面大小,只適用一個sheet的情況

1、maven注入連線openoffice的Jar和poi

        <dependency>
            <groupId>com.artofsolving</groupId>
            <artifactId>jodconverter</artifactId>
            <version>2.2.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.15</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.15</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-scratchpad</artifactId>
            <version>3.15</version>
        </dependency>

2.根據檔案輸入流獲取excel檔案的寬度

public InputStream getPdfStream(String fileEnd, InputStream fileInput, String last) throws Exception{
        String fileType = "";
        Integer colWidth = 0;
        if("xlsx".equals(fileEnd) || "xls".equals(fileEnd)) { //xlsx格式的檔案轉成xls處理    
            ByteArrayOutputStream baos = getByteArrayStream(fileInput);   //把輸入流轉成輸出陣列的方法
            fileInput = new ByteArrayInputStream(baos.toByteArray());  
            InputStream streamClon = new ByteArrayInputStream(baos.toByteArray());  
            colWidth = getColumnWidth(streamClon,fileEnd);    //獲取excel有資料的列數
            fileType = "xls";
        }else if("docx".equals(fileEnd)){                     //docx格式的檔案轉成doc處理                   
            fileType = "doc";
        }else {                                                   
            fileType = fileEnd;
        }   
        try {   
            System.out.println("列寬為:"+colWidth);
            OpenOfficeConnection connection = OpenOfficeUtils.getInstance();
            ConverterDocument converter = new ConverterDocument(connection,fileType,colWidth);   //使用StreamOpenOfficeDocumentConverter可以轉07版的
  
            //DocumentConverter converter = new StreamOpenOfficeDocumentConverter(connection);   //使用StreamOpenOfficeDocumentConverter可以轉07版的
            DefaultDocumentFormatRegistry formatReg = new DefaultDocumentFormatRegistry();     //jar包裡的類
            DocumentFormat inputFormat = formatReg.getFormatByFileExtension(fileType);         //原始檔的格式
            DocumentFormat pdfFormat = formatReg.getFormatByFileExtension("pdf");              //轉成的格式
            ByteArrayOutputStream pdfstream = new ByteArrayOutputStream();                     //儲存轉成pdf的流的陣列
            converter.convert(fileInput, inputFormat, pdfstream, pdfFormat);                   //將檔案流轉換成pdf流
            InputStream pdfInput = new BufferedInputStream(new ByteArrayInputStream(pdfstream.toByteArray()));//把pdf流轉成輸入流
            pdfstream.flush();
            pdfstream.close();            
            if("true".equals(last)) {
                OpenOfficeUtils.closeConnection();
            }
            return pdfInput;
        } catch(Exception e) {  
            OpenOfficeUtils.closeConnection();
            e.printStackTrace();   
        }
        return null;
    }

3.重寫連線Openoffice的方法,重寫refreshDocument方法,根據excel檔案的大小設定頁面大小

import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
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  {
    private String fileType;
    private Integer colWidth;
    public ConverterDocument(OpenOfficeConnection connection, String fileType, Integer colWidth) {
        super(connection);
        this.colWidth = colWidth;
        this.fileType = fileType;
    }
 
    public final static Size A4, A3, A2,A1;
    public final static Size B4, B3, B2,B1;
    public final static Size KaoqinReport;
    
    static {
        A4 = new Size(21000, 29700);
        A3 = new Size(29700, 42000);
        A2 = new Size(42000, 59400);
        A1 = new Size(60000, 90000);
        
        B4 = new Size(25000, 35300);
        B3 = new Size(35300, 50000);
        B2 = new Size(50000, 70700);
        B1 = new Size(70700, 100000);
        
        
        KaoqinReport = new Size(42000, 54300);
        
    }
    
    /*
     * XComponent:xCalcComponent
     * 
     * @seecom.artofsolving.jodconverter.openoffice.converter.
     * AbstractOpenOfficeDocumentConverter
     * #refreshDocument(com.sun.star.lang.XComponent)
     */
    @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[3];
        printerDesc[0] = new PropertyValue();
        printerDesc[0].Name = "PaperFormat";
        printerDesc[0].Value = PaperFormat.USER;
    
        // Paper Size
        printerDesc[1] = new PropertyValue();
        printerDesc[1].Name = "PaperSize";
        if("xls".equals(fileType)) {
            if(colWidth <= 21000) {
                printerDesc[1].Value = A4;
            }else if(colWidth > 21000 && colWidth <= 29700) {
                printerDesc[1].Value = A3;
            }else if(colWidth > 29700 && colWidth <= 42000) {
                printerDesc[1].Value = A2;
            }else if(colWidth > 42000 && colWidth <= 60000) {
                printerDesc[1].Value = A1;
            }else {
                printerDesc[1].Value = A4;
            }
        }else {
            printerDesc[1].Value = A4;
        }
        printerDesc[2] = new PropertyValue();
        printerDesc[2].Name = "Pages";
        printerDesc[2].Value = 2;
        try {
            xPrintable.setPrinter(printerDesc);
        } catch (Exception e) {
            e.printStackTrace();
        }
    
    }

}

4.向儲存地址輸出pdf檔案流,此用minio

ByteArrayOutputStream arrayStream = new ByteArrayOutputStream();
        byte[] b = new byte[1024];
        int len;    
        while ((len = inputStream.read(b)) > 0) {
            arrayStream.write(b, 0, len);
        }
        arrayStream.flush();

    InputStream returnStream = new BufferedInputStream(new ByteArrayInputStream(arrayStream.toByteArray()));   //把output流陣列轉成input流  
               minioClient.putObject(fileDirectory,pathPdf, returnStream,returnStream.available(),"application/octet-stream");
            System.out.println("檔案"+pathPdf+"轉成功==========================");