Importing (and exporting) from excel with formatting?

Is there a method to load from excel sheet with formatting and later save the table back to excel INCLUDING previously loaded formatting? I mean fonts, cell background color. I have tables with extensive color coding and I would like to load-modify text-write them automatically.

5 Comments

You need to use an ActiveX control or a .NET assembly to talk directly to Excel.
For MacOS or Linux you would probably need to install OfficeLibre or similar and use it's API.
I have been using activeX to set colors / fonts manually but haven't managed to get Matlab read the formatting from source. It is a win10 machine.
How can I make it to READ format from source and apply it on export?
Is the formatting of the Excel sheet consistent? If so, you could try just saving a template sheet with the formatting and copying the template before writing the data to it.
No, I got ~100 of different xlsx files and they are all different. What I CAN do is to make a script that will color the table according to content but I was hoping for some easier way like importing the formatting and then re-exporting it.
OK, I got to a solution that I do not need to extract the color data at all. I open the excel sheet first with XLSREAD and data goes to a cell variable. Then I start the ActiveX server and insert the data into the sheet (I don't need to delete / add any columns / rows) and save. Thus color coding is preserved but strings in cell can be changed with STRREP etc.
Thanks for everyone for thinking along!
Excel=actxserver('Excel.Application');
% start ActiveX
WB = Excel.Workbooks.Open(fullfile([Path File]),0,false);
% get workbook object
Sheets=Excel.ActiveWorkbook.Sheets;
% sheet handles
Sheet = Sheets.get('Item', SNr);
% acquires sheet handle
% #####################
% ### Modifications ###
% #####################
NetWork(:,2)=strrep(NetWork(:,2),'C0','X0');
% replacement
% #####################
% ### Modifications ###
% #####################
ActiveRange = get(Sheet,'Range',['A1:G' num2str(size(NetWork,1))]);
% inserts data range
set(ActiveRange, 'Value', NetWork);
% inserts cell to sheet
WB.Save();
% save Workbook
WB.Close();
% close Workbook
Excel.Quit();
% quit Excel

Sign in to comment.

Answers (0)

Products

Release

R2022b

Asked:

on 30 Nov 2023

Commented:

on 1 Dec 2023

Community Treasure Hunt

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

Start Hunting!