将数据导出到 Excel 并对其应用格式
Exporting data to Excel and applying a format to it
我想使用 xlswrite
导出 1xm-Cellarray
。元胞数组由 m
个元胞组成,每个元胞包含一个 ixn-Cellarray
,其中 i
主要是 2,但也可以是 3、4、5 或 6。这是数据外观的示例喜欢:
a=[{{{'a'},{'b'},{'c'},{'d'};{'a'},{'b'},{'c'},{'d'}}},{{{'a'},{'b'},{'c'},{'d'};{'a'},{'b'},{'c'},{'d'};{'a'},{'b'},{'c'},{'d'}}},{{{'a'},{'b'},{'c'},{'d'};{'a'},{'b'},{'c'},{'d'}}}]
a =
{2x4 cell} {3x4 cell} {2x4 cell}
我希望所有单元格都写在彼此下面,但我希望能够在 Excel 中看到哪些行属于一个单元格。我的想法是在数组单元格和另一个像这样的单元格之间放置一个空行
exportTable=[];
for jj=1:numel(a)
exportTable=[exportTable;a{jj};repmat({[]},1,18)];
end
然后使用 xlswrite
导出 exportTable
,但这在导出的 sheet 中看起来非常丑陋并且不容易阅读。
现在我正在寻找一种方法来直接使用 Matlab 中的导出函数或使用 Excel 将相应行的向量作为输入来使每个单元格的线条以相同的颜色着色。
我可以使用
为每个单元格实现结束索引
rows=cumsum(cellfun(@(x) size(x,1),a))
rows =
2 5 7
但我不知道如何根据行号为 Excel 中的行着色。
我的示例所需的输出如下所示:
感谢任何使用 Matlab 或 Excel 的帮助。
rows = cumsum(cellfun(@(x) size(x,1),a))
%Create an Excel object.
e = actxserver('Excel.Application');
%Add a workbook.
eWorkbook = e.Workbooks.Add;
e.Visible = 1;
%Make the first sheet active.
eSheets = e.ActiveWorkbook.Sheets;
eSheet1 = eSheets.get('Item',1);
eSheet1.Activate
for i = 1:length(a)
ai = table2array( cell2table( a{:,i} ) ); % sorry for this construction
if mod(i,2)
ai_color = 3;
else
ai_color = 4;
end
ai_range = ['A',num2str(rows(i)-size(ai,1)+1),':',char('A'-1+size(ai,2)),num2str(rows(i))]; % ... and this :)
% Set the color of cells
eSheet1.Range(ai_range).Interior.ColorIndex = ai_color;
%Put MATLAB data into the worksheet.
eActivesheetRange = get(e.Activesheet,'Range',ai_range);
eActivesheetRange.Value = ai;
end
SaveAs(eWorkbook,'myfile.xlsx')
%If the Excel program displays a dialog box about saving the file, select the appropriate response to continue.
%If you saved the file, then close the workbook.
eWorkbook.Saved = 1;
Close(eWorkbook)
%Quit the Excel program and delete the server object.
Quit(e)
delete(e)
%Note: Make sure that you close workbook objects you create to prevent potential memory leaks.
我想使用 xlswrite
导出 1xm-Cellarray
。元胞数组由 m
个元胞组成,每个元胞包含一个 ixn-Cellarray
,其中 i
主要是 2,但也可以是 3、4、5 或 6。这是数据外观的示例喜欢:
a=[{{{'a'},{'b'},{'c'},{'d'};{'a'},{'b'},{'c'},{'d'}}},{{{'a'},{'b'},{'c'},{'d'};{'a'},{'b'},{'c'},{'d'};{'a'},{'b'},{'c'},{'d'}}},{{{'a'},{'b'},{'c'},{'d'};{'a'},{'b'},{'c'},{'d'}}}]
a =
{2x4 cell} {3x4 cell} {2x4 cell}
我希望所有单元格都写在彼此下面,但我希望能够在 Excel 中看到哪些行属于一个单元格。我的想法是在数组单元格和另一个像这样的单元格之间放置一个空行
exportTable=[];
for jj=1:numel(a)
exportTable=[exportTable;a{jj};repmat({[]},1,18)];
end
然后使用 xlswrite
导出 exportTable
,但这在导出的 sheet 中看起来非常丑陋并且不容易阅读。
现在我正在寻找一种方法来直接使用 Matlab 中的导出函数或使用 Excel 将相应行的向量作为输入来使每个单元格的线条以相同的颜色着色。
我可以使用
rows=cumsum(cellfun(@(x) size(x,1),a))
rows =
2 5 7
但我不知道如何根据行号为 Excel 中的行着色。
我的示例所需的输出如下所示:
感谢任何使用 Matlab 或 Excel 的帮助。
rows = cumsum(cellfun(@(x) size(x,1),a))
%Create an Excel object.
e = actxserver('Excel.Application');
%Add a workbook.
eWorkbook = e.Workbooks.Add;
e.Visible = 1;
%Make the first sheet active.
eSheets = e.ActiveWorkbook.Sheets;
eSheet1 = eSheets.get('Item',1);
eSheet1.Activate
for i = 1:length(a)
ai = table2array( cell2table( a{:,i} ) ); % sorry for this construction
if mod(i,2)
ai_color = 3;
else
ai_color = 4;
end
ai_range = ['A',num2str(rows(i)-size(ai,1)+1),':',char('A'-1+size(ai,2)),num2str(rows(i))]; % ... and this :)
% Set the color of cells
eSheet1.Range(ai_range).Interior.ColorIndex = ai_color;
%Put MATLAB data into the worksheet.
eActivesheetRange = get(e.Activesheet,'Range',ai_range);
eActivesheetRange.Value = ai;
end
SaveAs(eWorkbook,'myfile.xlsx')
%If the Excel program displays a dialog box about saving the file, select the appropriate response to continue.
%If you saved the file, then close the workbook.
eWorkbook.Saved = 1;
Close(eWorkbook)
%Quit the Excel program and delete the server object.
Quit(e)
delete(e)
%Note: Make sure that you close workbook objects you create to prevent potential memory leaks.