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;
    }

    private ByteArrayOutputStream getByteArrayStream(InputStream fileInput) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();  
        byte[] buffer = new byte[1024];  
        int len;  
        while ((len = fileInput.read(buffer)) > -1 ) {  
            baos.write(buffer, 0, len);  
        }  
        baos.flush(); 
        return baos;
    }


    private Integer getColumnWidth(InputStream fileInput,String fileEnd) throws Exception {
         Workbook workBook = getWorkBook(fileInput,fileEnd);
         int colWidth = 0;
         if(workBook!=null) {
                 Sheet sheet = workBook.getSheetAt(0);
              if(sheet != null){
                 int colNum = sheet.getRow(1).getPhysicalNumberOfCells(); 
                 for(int i = 0;i < colNum;i++) {
                     colWidth += sheet.getColumnWidth(i);
                 }
              }
            }
         workBook.close();
         return colWidth;
    }

    public Workbook getWorkBook(InputStream in,String fileEnd) throws Exception  {
        Workbook workbook = null;
        try {
            if(fileEnd.equals("xls")){ //根据文件后缀名不同(xls和xlsx)获得不同的Workbook实现类对象
                workbook = new HSSFWorkbook(in);
            }else if(fileEnd.equals("xlsx")){ //2007 及2007以上
                workbook = new XSSFWorkbook(in); //2003
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return workbook;
    }

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+"转成功==========================");

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 36
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 36
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值