How to convert .xls file (Microsoft excel) to pdf using matlab

Hi, In my matlab application, I'm creating new excel file. I would like to convert this file to pdf format using matlab. Can I ??

1 个评论

Yes, you can easily convert an Excel file to PDF using MATLAB.
The most efficient and robust way to achieve this on Windows is by using MATLAB's ActiveX Control (COM Automation) to programmatically control Microsoft Excel and trigger its built-in PDF export feature.
% Define absolute paths (Excel requires full paths, not relative)
excelFile = 'C:\path\to\your\input_file.xlsx';
pdfFile = 'C:\path\to\your\output_file.pdf';
% 1. Start an Excel ActiveX server
hExcel = actxserver('Excel.Application');
% 2. Make Excel invisible so it runs quietly in the background
hExcel.Visible = 0;
% 3. Open the workbook
hWorkbook = hExcel.Workbooks.Open(excelFile);
try
% 4. Select the target worksheet (e.g., Sheet 1)
hWorksheet = hWorkbook.Sheets.Item(1);
% 5. Export the sheet to PDF format
% 'xlTypePDF' corresponds to the value 0
hWorksheet.ExportAsFixedFormat(0, pdfFile);
disp('Conversion successful!');
catch ME
warning('An error occurred during conversion: %s', ME.message);
end
% 6. Clean up: Close workbook and quit Excel to free memory
hWorkbook.Close(false);
hExcel.Quit();
delete(hExcel);
In Case, this process is not work for this kind of situation i prefer a dedicated tool like PDFChamp Excel to PDF.

请先登录,再进行评论。

回答(1 个)

You could publish to PDF direct from MATLAB. Also, if you have the possibility to Print to PDF on your system and printing from Excel to PDF gives the format you need, you could use actxserver in MATLAB to control the Excel print to PDF.

2 个评论

Can you help me with the properties of Excel.ActiveWorkbook.SaveAs funation. I tried: Excel.ActiveWorkbook.SaveAs(filenamexls,'PDF',[],[], 'False', 'False', 'xlNoChange', 'xlOtherSessionChanges','False',[],[],'False') but it didn't work...
Sorry, while I have used the SaveAs method in VBA, we still use Excel 2003 which does not natively support save to PDF. There are some tools that allow you to Print To PDF, which is another possibility.

请先登录,再进行评论。

标签

提问:

2012-7-10

评论:

2026-11-6,12:40

Community Treasure Hunt

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

Start Hunting!

Translated by