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 个评论
heroe
2026-11-6,12:40
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 个)
Julian
2012-7-10
0 个投票
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.
类别
在 帮助中心 和 File Exchange 中查找有关 Spreadsheets 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!