2015-09-22 161 views
3

我想使用Matlab將Excel文件轉換爲PDF。 Excel文件充滿了表格和圖形。它們佔用的空間大於DIN A4頁面,因此當我使用以下代碼時,我的Excel文件在PDF中的多個頁面上被「分割」。我想問一下,是否可以在一頁上將PDF中的輸出縮放,或者忽略縮放比例,以便表格可能全部放在一頁上,而下方則是圖形。也許將打印屬性更改爲只有一個頁面會有所幫助。是否有可能在Matlab中做到這一點?使用Matlab將Excel文件轉換爲PDF,而忽略比例

代碼:

hExcel = actxserver('Excel.Application'); 
hWorkbook = hExcel.Workbooks.Open(sprintf('%s', 'C:\test.xls')); 
hWorksheet = hWorkbook.Sheets.Item('Test'); 
hWorksheet.ExportAsFixedFormat('xlTypePDF', 'C:\test_out.pdf'); 

我擡頭對微軟幫助ExportAsFixedFormat,但一切我試過改變,將無法正常工作,我得到錯誤信息。我試過例如:

hExcel = actxserver('Excel.Application'); 
hWorkbook = hExcel.Workbooks.Open(sprintf('%s', 'C:\test.xls')); 
hWorksheet = hWorkbook.Sheets.Item('Test'); 
hWorksheet.ExportAsFixedFormat('xlTypePDF', 'C:\test_out.pdf', 'xlQualityStandard', true, true);` 

在此先感謝您。

回答

0

嘗試使用宏記錄記錄下面的代碼。 如果這不能正常工作,只需在保存爲PDF之前先錄製自己的pagesetup宏即可。 enter image description here

頁面設置的所有頁面

Sheets(Array("Sheet1", "Sheet2", "Sheet3")).Select 
Application.PrintCommunication = False 
With ActiveSheet.PageSetup 
    .LeftHeader = "" 
    .CenterHeader = "" 
    .RightHeader = "" 
    .LeftFooter = "" 
    .CenterFooter = "" 
    .RightFooter = "" 
    .LeftMargin = Application.InchesToPoints(0.784313725490196) 
    .RightMargin = Application.InchesToPoints(0.784313725490196) 
    .TopMargin = Application.InchesToPoints(0.980392156862745) 
    .BottomMargin = Application.InchesToPoints(0.980392156862745) 
    .HeaderMargin = Application.InchesToPoints(0.509803921568628) 
    .FooterMargin = Application.InchesToPoints(0.509803921568628) 
    .PrintHeadings = False 
    .PrintGridlines = False 
    .PrintComments = xlPrintNoComments 
    .PrintQuality = 600 
    .CenterHorizontally = False 
    .CenterVertically = False 
    .Orientation = xlLandscape 
    .Draft = False 
    .PaperSize = xlPaperA4 
    .FirstPageNumber = xlAutomatic 
    .Order = xlDownThenOver 
    .BlackAndWhite = False 
    .Zoom = 100 
    .PrintErrors = xlPrintErrorsDisplayed 
    .OddAndEvenPagesHeaderFooter = False 
    .DifferentFirstPageHeaderFooter = False 
    .ScaleWithDocHeaderFooter = True 
    .AlignMarginsHeaderFooter = False 
    .EvenPage.LeftHeader.Text = "" 
    .EvenPage.CenterHeader.Text = "" 
    .EvenPage.RightHeader.Text = "" 
    .EvenPage.LeftFooter.Text = "" 
    .EvenPage.CenterFooter.Text = "" 
    .EvenPage.RightFooter.Text = "" 
    .FirstPage.LeftHeader.Text = "" 
    .FirstPage.CenterHeader.Text = "" 
    .FirstPage.RightHeader.Text = "" 
    .FirstPage.LeftFooter.Text = "" 
    .FirstPage.CenterFooter.Text = "" 
    .FirstPage.RightFooter.Text = "" 
End With 
Application.PrintCommunication = True 
Application.PrintCommunication = False 
With ActiveSheet.PageSetup 
    .LeftHeader = "" 
    .CenterHeader = "" 
    .RightHeader = "" 
    .LeftFooter = "" 
    .CenterFooter = "" 
    .RightFooter = "" 
    .LeftMargin = Application.InchesToPoints(0.25) 
    .RightMargin = Application.InchesToPoints(0.25) 
    .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 = 600 
    .CenterHorizontally = False 
    .CenterVertically = False 
    .Orientation = xlLandscape 
    .Draft = False 
    .PaperSize = xlPaperA4 
    .FirstPageNumber = xlAutomatic 
    .Order = xlDownThenOver 
    .BlackAndWhite = False 
    .Zoom = 100 
    .PrintErrors = xlPrintErrorsDisplayed 
    .OddAndEvenPagesHeaderFooter = False 
    .DifferentFirstPageHeaderFooter = False 
    .ScaleWithDocHeaderFooter = True 
    .AlignMarginsHeaderFooter = False 
    .EvenPage.LeftHeader.Text = "" 
    .EvenPage.CenterHeader.Text = "" 
    .EvenPage.RightHeader.Text = "" 
    .EvenPage.LeftFooter.Text = "" 
    .EvenPage.CenterFooter.Text = "" 
    .EvenPage.RightFooter.Text = "" 
    .FirstPage.LeftHeader.Text = "" 
    .FirstPage.CenterHeader.Text = "" 
    .FirstPage.RightHeader.Text = "" 
    .FirstPage.LeftFooter.Text = "" 
    .FirstPage.CenterFooter.Text = "" 
    .FirstPage.RightFooter.Text = "" 
End With 
Application.PrintCommunication = True 
Application.PrintCommunication = False 
With ActiveSheet.PageSetup 
    .LeftHeader = "" 
    .CenterHeader = "" 
    .RightHeader = "" 
    .LeftFooter = "" 
    .CenterFooter = "" 
    .RightFooter = "" 
    .LeftMargin = Application.InchesToPoints(0.236220472440945) 
    .RightMargin = Application.InchesToPoints(0.236220472440945) 
    .TopMargin = Application.InchesToPoints(0.748031496062992) 
    .BottomMargin = Application.InchesToPoints(0.748031496062992) 
    .HeaderMargin = Application.InchesToPoints(0.31496062992126) 
    .FooterMargin = Application.InchesToPoints(0.31496062992126) 
    .PrintHeadings = False 
    .PrintGridlines = False 
    .PrintComments = xlPrintNoComments 
    .PrintQuality = 600 
    .CenterHorizontally = False 
    .CenterVertically = False 
    .Orientation = xlLandscape 
    .Draft = False 
    .PaperSize = xlPaperA4 
    .FirstPageNumber = xlAutomatic 
    .Order = xlDownThenOver 
    .BlackAndWhite = False 
    .Zoom = False 
    .FitToPagesWide = 1 
    .FitToPagesTall = 0 
    .PrintErrors = xlPrintErrorsDisplayed 
    .OddAndEvenPagesHeaderFooter = False 
    .DifferentFirstPageHeaderFooter = False 
    .ScaleWithDocHeaderFooter = True 
    .AlignMarginsHeaderFooter = False 
    .EvenPage.LeftHeader.Text = "" 
    .EvenPage.CenterHeader.Text = "" 
    .EvenPage.RightHeader.Text = "" 
    .EvenPage.LeftFooter.Text = "" 
    .EvenPage.CenterFooter.Text = "" 
    .EvenPage.RightFooter.Text = "" 
    .FirstPage.LeftHeader.Text = "" 
    .FirstPage.CenterHeader.Text = "" 
    .FirstPage.RightHeader.Text = "" 
    .FirstPage.LeftFooter.Text = "" 
    .FirstPage.CenterFooter.Text = "" 
    .FirstPage.RightFooter.Text = "" 
End With 
Application.PrintCommunication = True 

要保存爲PDF

ActiveWorkbook.ExportAsFixedFormat xlTypePDF, fName & ".pdf", _ 
Quality:=xlQualityStandard, _ 
IncludeDocProperties:=True, _ 
IgnorePrintAreas:=False, _ 
OpenAfterPublish:=False 
+0

謝謝您的回答,預先定義頁面妝正常工作與我的願望。但我正在尋找一個在matlab中使用的代碼,它會自動執行這些步驟。也許我應該編輯一下我的問題。 –

+0

不是Matlab編碼的專家。對不起,我忍不住。 – Isu

相關問題