10

我创建了一个应用程序JodConverterOpen-Office用于将 excel( .xlsx) 转换为PDF,该应用程序运行良好,但我面临两个问题

  1. 输出 PDF 的页面采用 A4 大小的形式,因为因此某些工作表内容已被切掉。因为我希望excel的每个工作表都像一页一样完整,无论大小。

  2. 缺少工作表的数量,比如如果我的 excel 有 8 个工作表,我在PDF输出中只得到两个或三个

即使我们尝试pdf直接从开放式办公室转换为,也会出现上述类似问题

Excel 文件- ss1.xlsx

输出 PDF - work.pdf

谁能告诉我一些解决方案

我的代码如下

public class MyConverter {

    public static void main(String[] args) throws ConnectException {
        File inputFile = new File("C:/Users/Work/Desktop/ss1.xlsx");
        File outputFile = new File("C:/Users/Work/Desktop/work.pdf");

        // connect to an OpenOffice.org instance running on port 8100
        OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
        connection.connect();

        // convert
        DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
        converter.convert(inputFile, outputFile);

        // close the connection
        connection.disconnect();
    }
4

4 回答 4

2

恐怕我之前的回答不够清楚。所以这里是精髓:

  • 除了第 3 个工作表之外,所有工作表的分辨率设置为 600。在第 3 个工作表中,分辨率留空
  • 将第三张纸的分辨率更改为 600
  • 现在将正常生成 PDF 文件,其中包含所有工作表。

显然 Excel 只能生成所有工作表的页面分辨率相同的 PDF。如果遇到具有不同(空白)分辨率的工作表,它将停止产生输出而不给出任何警告。恕我直言,这是 Excel 中的一个错误。幸运的是,解决方法很简单。

希望这能澄清我之前的回答。

于 2014-08-23T10:18:16.603 回答
2

我使用(免费)PrimoPDF 打印机驱动程序直接从 Excel 中创建 PDF。大量页面(20 多页)是由于其中一个工作表中缺少“适合页面”打印选项,如果我记得很好的话是第三个。更正此问题后,打印所有工作表的命令仍会生成 2 个 PDF 文件。PrimoPDF 两次询问文件名,而它应该只询问一次。我假设您的程序只生成与第一部分相对应的 PDF,因为通常应该只生成一个 PDF。我对两部分打印没有任何解释。这可能是由于其中一个工作表中的某些打印设置迫使打印在“两批”中执行。例如,不同的分辨率值可能会阻止一批打印。结论:解决方法是使用 PrimoPDF 打印并使用网络上免费提供的程序之一连接 2 个 PDF 文件。要获得持久的解决方案,您必须详细验证所有工作表的打印设置并确保它们相同。

于 2014-07-28T12:22:21.400 回答
1

您可以使用 Microsoft Excel 将任何 .xlsx 转换为 PDF,我目前正在开发一个使用 Jacob 的应用程序,(Java Com Bridge)一个用于连接到 Microsoft Office 程序的对象模型的薄包装器,afaik 它不支持打开-office,但它在将 .xlsx 文件转换为 PDF 文件方面做得很好。它需要一些设置。链接到雅各布

在查看我在 Excel -> 页面设置中发现的问题时,如果将适合更改为 1 页宽和 1 页高,它会挤压每个工作表以适合 PDF 中的每个页面。我遇到的另一个问题是 wrap text 属性,使用它时要小心,因为它会导致布局问题。

我对此做了一个小实现,在 Excel 2010 和 Jacob 1.18 上进行了测试

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComFailException;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

public class ExcelApplication {

    private final String APP_NAME = "Excel.Application";

    private final ActiveXComponent excelApplication;

    private Dispatch workbooks;//all active workbooks opened

    private Dispatch activeWorkbook;//active workbook

    private Dispatch activeWorksheets;//all worksheets in active workbook

    public ExcelApplication() {
        excelApplication = new ActiveXComponent(APP_NAME);
    }

    public void openExcelFileInvisible(String fileName) {
        //Opens Excel in the background
        String fileUrl;
        if (excelApplication != null) {
            excelApplication.setProperty("Visible", new Variant(false));//sets excel invisible            
            //file url relative to this class
            //or you can just give an absolute path
            fileUrl = getClass().getResource(fileName).toExternalForm();
            //get workbooks
            workbooks = Dispatch.call(excelApplication, "Workbooks").getDispatch();
            if (activeWorkbook == null) {
                try {
                    activeWorkbook = Dispatch.call(workbooks, "Open", fileUrl).getDispatch();
                } catch (ComFailException comFailEx) {
                    //error opening the Excel Document
                }
            }
        }
    }

    public void closeActiveWorkbookAndSave() {
        try {
            //close and save change's to active workbook
            //this only closes the workbook, not Excel
            Dispatch.call(activeWorkbook, "Close", new Variant(true));
            //if you want to exit the Excel App.            
            //excelApplication.invoke("Quit", new Variant[0]);            
        } catch (ComFailException cfe) {
            //problem closing the workbook
        }
    }

    public void convert_XLSX_TO_PDF(String pdfFileName) {
        if (activeWorkbook != null) {
            String workbookName = Dispatch.call(activeWorkbook, "Name").getString();
            activeWorksheets = Dispatch.call(activeWorkbook, "Worksheets").getDispatch();
            int workSheetCount = Dispatch.call(activeWorksheets, "Count").getInt();
            System.out.println("Workbook Name =" + workbookName);
            System.out.println("Total Worksheets In Active Document = " + workSheetCount);
            System.out.println("Converting to PDF....");
            try {                
                Dispatch currentWorksheet;
                String currentWorksheetName;
                //worksheets not zero based, starts at one
                for (int i = 1; i < workSheetCount+1; i++) {
                    //get each active work sheet and set up the page setup settings
                    currentWorksheet = Dispatch.call(activeWorksheets, "Item", new Variant(i)).getDispatch();
                    currentWorksheetName = Dispatch.call(currentWorksheet, "Name").getString();
                    System.out.println("Setting up page setup for Workbook Sheet ("+ i + ".) - " + currentWorksheetName);
                    //Get page setup for each worksheet
                    Dispatch pageSetup = Dispatch.get(currentWorksheet, "PageSetup").getDispatch();
                    /**** Zoom must be set to false for FitToPagesWide and FitToPagesTall
                       to take control of scaling
                    */
                    Dispatch.put(pageSetup, "Zoom", new Variant(false));                    
                    //Fit content on each worksheet to fit in a single page                                        
                    Dispatch.put(pageSetup, "FitToPagesWide", new Variant(1));
                    Dispatch.put(pageSetup, "FitToPagesTall", new Variant(1));                    
                    //set print area to not chop off content
                    Dispatch.put(pageSetup, "PrintArea", new Variant(false));                    
                    //set left margin small
                    Dispatch.put(pageSetup, "LeftMargin", new Variant(0));                                     
                }
                //[3rd param] = 0 specifies PDF document, 1 is XPS format
                //[4th param] = 0 specifies high quality, 1 is low quality
                //[5th param] = true to keep document properties, false to ommit
                //[6th param] = true to keep print areas set, false does not keep print areas set 
                Dispatch.call(activeWorkbook, "ExportAsFixedFormat", new Variant(0), new Variant(pdfFileName), new Variant(0), new Variant(false), new Variant(true));
                System.out.println("Export to PDF has been successful.");
                //close and save
                closeActiveWorkbookAndSave();
            } catch (ComFailException comFailEx) {
                //Export Failed
                System.out.println("Export to PDF has failed");
            }
        }
    }

}

public class TestExcel {

    public static void main(String[] args) {
        // TODO code application logic here
        ExcelApplication e = new ExcelApplication();
        e.openExcelFileInvisible("ss1.xlsx");
        //full path accepted here or if not it will be exported to current directory
        e.convert_XLSX_TO_PDF("covertedXLSXFile.pdf");
    }

}

这是从上述代码生成的 PDF 文件。请注意第 3 页,内容有点被截断,当您删除换行文本属性并合并单元格时,它会生成正常。转换后的 XLSX

于 2014-08-01T21:12:04.097 回答
0

这是为所有工作表设置相同页面参数的 Excel VBA 代码。抱歉,我不熟悉 Openoffice 编程,假设 API 类似:

Sub PageSetup_AllSheets()
    Dim ws As Worksheet
    For Each ws In ActiveWorkbook.Worksheets
        ws.Activate
        Setup_Page
    Next
End Sub

Sub Setup_Page()
'
' Setup_Page Macro
' Macro recorded 27/08/2014 by Paul
'
    With ActiveSheet.PageSetup
        .PrintTitleRows = ""
        .PrintTitleColumns = ""
        .LeftHeader = ""
        .CenterHeader = ""
        .RightHeader = ""
        .LeftFooter = ""
        .CenterFooter = ""
        .RightFooter = ""
        .LeftMargin = Application.InchesToPoints(0.7)
        .RightMargin = Application.InchesToPoints(0.7)
        .TopMargin = Application.InchesToPoints(0.75)
        .BottomMargin = Application.InchesToPoints(0.75)
        .HeaderMargin = Application.InchesToPoints(0.3)
        .FooterMargin = Application.InchesToPoints(0.3)
        .PrintHeadings = False
        .PrintGridlines = False
        .PrintComments = xlPrintNoComments
        .PrintQuality = -3
        .CenterHorizontally = False
        .CenterVertically = False
        .Orientation = xlPortrait
        .Draft = False
        .PaperSize = xlPaperA4
        .FirstPageNumber = xlAutomatic
        .Order = xlDownThenOver
        .BlackAndWhite = False
        .Zoom = False
        .FitToPagesWide = 1
        .FitToPagesTall = 1
        .PrintErrors = xlPrintErrorsDisplayed
    End With
End Sub
于 2014-08-24T14:47:24.943 回答