How to save an Excel sheet as PDF through MATLAB?

I am using the following code:
hExcel = actxserver('Excel.Application');
hWorkbook = hExcel.Workbooks.Open(sprintf('%s','C:\test.xlsx'));
hWorksheet = hWorkbook.Sheets.Item(1);
hRange = hWorksheet.Range('A1:O10');
hRange.ExportAsFixedFormat('xlTypePDF','test_out.pdf');
The Excel ActiveX server allows me to do the usual stuff, but the last line doesn't do anything. Neither does it throw any error.
Does anyone know how to do this?

댓글 수: 1

Praveen
Praveen 2016년 12월 28일
hRange.ExportAsFixedFormat('xlTypePDF','test_out.pdf'); In this code give the path name, it works.. For example 'C:\test_out.pdf' instead of test

댓글을 달려면 로그인하십시오.

 채택된 답변

Cel Kulasekaran
Cel Kulasekaran 2015년 1월 13일
편집: Cel Kulasekaran 2015년 1월 13일

3 개 추천

First, set the print area within the workbook itself, then you'll need to call ExportAsFixedFormat on the worksheet or workbook object. You cannot call it on the range object. Hope this helps, see below:
hExcel = actxserver('Excel.Application');
hWorkbook = hExcel.Workbooks.Open(sprintf('%s','C:\test.xlsx'));
hWorksheet = hWorkbook.Sheets.Item(1);
% print this sheet to PDF
hWorksheet.ExportAsFixedFormat('xlTypePDF','C:\test_out.pdf');

댓글 수: 5

is there a way to do this in order to fit all columns?
Dear Cel, I want to edit a pdf composed of several pages from the same Excel document. Do you know if it's possible to use something like that ?
hWorksheet = hWorkbook.Sheets.Item({1,2,3,4,5});
I can't find a combination that works... Thanking you in advance for your help
Best regards,
Maxime
Hi Maxime,
I solved the issue calling the ExportAsFixedFormat function directly on the Workbooks so:
hExcel = actxserver('Excel.Application');
hWorkbook = hExcel.Workbooks.Open(sprintf('%s','C:\test.xlsx'));
% print this sheet to PDF
hWorkbook.ExportAsFixedFormat('xlTypePDF','C:\test_out.pdf');
best regards,
Domenico
Is there a way to make it save 'Entire workbook'? When I manualy save as PDF I can click 'Options' then save 'Entire workbook' this saves each sheet as a page in the PDF.
Answer my own question:
hExcel = actxserver('Excel.Application');
hWorkbook = hExcel.Workbooks.Open(sprintf('%s','C:\test'));
% Pring the workbook
hWorkbook.ExportAsFixedFormat('xlTypePDF','C:\test_out.pdf');

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Intissar El Bouanani
Intissar El Bouanani 2023년 8월 10일

0 개 추천

it is possible to print the selected field in one page

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by