Report Generator Table Formatting
이전 댓글 표시
I have the following function that works to add the table into a defined hole in an MS Word template; the content is all as expected excepting the formatting isn't quite as would like and have not been able to figure out how to get the last couple of details as desired.
Specifically, the
headerStyle ={ Bold(true), ...
};
...
t.Header.Style=headerStyle;
doesn't work to set the header to a bold font, nor does the similar code work for the footer.
Secondly, the entire table is outlined but the outline of the header and footer is missing...there are two rows in the table content section for which I do not want a row separator line; but do want the header and footer outlines. I don't see why that isn't what is instructed to do with
headerStyle ={ Bold(true), ...
Border("single"), ...
};
tableStyle = { Width("80%"), ...
Border("single"), ...
RowSep("none"), ...
ColSep("none") ...
};
footerStyle = { Bold(true), ...
Border("single"), ...
ColSep("none") ...
};
The full function follows...I didn't want to put the actual data out for public view so didn't attach the output document.
Hopefully, somebody understands this well enough to be able to know what magic incantations to take; found a formal table example in the doc from which this was copied, but none of the examples I could find ever don't show every row separator.
function addReportSummaryTable(donordata,yr)
% Annual summary table
corpus=donordata.corpus;
expendables=donordata.expends;
headerStyle ={ Bold(true), ...
};
tableStyle = { Width("80%"), ...
Border("single"), ...
RowSep("none"), ...
ColSep("none") ...
};
footerStyle = { Bold(true), ...
Border("single"), ...
ColSep("none") ...
};
entriesStyle = {FontFamily("Calibri"), ...
FontSize("11pt")};
headerContent=[{'Fund Summary'} compose('Fiscal Year %d',[yr-1 yr])];
bodyContent =[[{'Endowment (principal)'} cellstr(num2currency(corpus))];
[{'Expendable (income and contributions'} cellstr(num2currency(expendables))]];
totals=corpus+expendables;
footerContent = [{'Total fund balance'} cellstr(num2currency(totals))];
tableContent = [headerContent; bodyContent; footerContent];
t= FormalTable(tableContent);
t.Style=tableStyle;
t.Header.Style=headerStyle;
t.TableEntriesHAlign = "right";
t.TableEntriesStyle = [t.TableEntriesStyle, entriesStyle];
g=TableColSpecGroup();
s=TableColSpec();
s.Style={HAlign('left')};
g.ColSpecs=s;
t.ColSpecGroups=g;
t.HAlign = "center";
append(rpt, t);
end
댓글 수: 2
Kevin Holly
2025년 12월 9일
I just used some fake data. I will look into this. I need to take care of some things and then I will look at this soon.

donordata = table;
donordata.corpus = rand(2,1);
donordata.expends = rand(2,1);
yr = 2012;
addReportSummaryTable(donordata,yr)
function addReportSummaryTable(donordata,yr)
import mlreportgen.report.*
import mlreportgen.dom.*
rpt = Report('Annual Report', 'pdf');
open(rpt);
% Annual summary table
corpus=donordata.corpus;
expendables=donordata.expends;
headerStyle ={ Bold(true), ...
};
tableStyle = { Width("80%"), ...
Border("single"), ...
RowSep("none"), ...
ColSep("none") ...
};
footerStyle = { Bold(true), ...
Border("single"), ...
ColSep("none") ...
};
entriesStyle = {FontFamily("Calibri"), ...
FontSize("11pt")};
headerContent=[{'Fund Summary'} compose('Fiscal Year %d',[yr-1 yr])];
bodyContent =[[{'Endowment (principal)'} cellstr(num2currency(corpus))];
[{'Expendable (income and contributions'} cellstr(num2currency(expendables))]];
totals=corpus+expendables;
footerContent = [{'Total fund balance'} cellstr(num2currency(totals))];
tableContent = [headerContent; bodyContent; footerContent];
t= FormalTable(tableContent);
t.Style=tableStyle;
t.Header.Style=headerStyle;
t.TableEntriesHAlign = "right";
t.TableEntriesStyle = [t.TableEntriesStyle, entriesStyle];
g=TableColSpecGroup();
s=TableColSpec();
s.Style={HAlign('left')};
g.ColSpecs=s;
t.ColSpecGroups=g;
t.HAlign = "center";
append(rpt, t);
close(rpt);
end
function output = num2currency(number)
for ii = 1:length(number)
output{ii} = horzcat('$',num2str(number(ii)));
end
end
채택된 답변
추가 답변 (1개)
Taylor
2025년 12월 9일
0 개 추천
Your “header” and “footer” rows are actually part of the body. FormalTable(tableContent) treats the whole array as the body. The Header and Footer objects exist, but they’re empty—so styling t.Header.Style (or t.Footer.Style) doesn’t affect the first/last rows you assembled. Build the table with separate sections:t = FormalTable(headerContent, bodyContent, footerContent);
Bold needs to be applied to the entries, not just the section container. For a TableHeader/TableFooter, use TableEntriesStyle (e.g., {Bold(true)}) to style the text inside the cells; Header.Style/Footer.Style applies to the section, and formats that don’t apply there are ignored.
RowSep('none') at the table level suppresses separators everywhere—including the rules under the header and above the footer. Use section‑level RowSep so the body has no separator while the header/footer still draw theirs.
With ColSep('none') and no row separators, you won’t see a box around those sections unless you add borders to the header/footer entries themselves (entry‑level borders override table/section separators).
댓글 수: 11
dpb
2025년 12월 9일
Kevin Holly
2025년 12월 9일
@dpb See my answer.
You may have been missing the following line (I just added this with the latest edit):
t.Footer.Style=[t.Footer.Style,footerStyle];
dpb
2025년 12월 9일
dpb
2025년 12월 9일
Kevin Holly
2025년 12월 10일
@dpb for the docx, the header/footer of a FormalTable does not actually use the style Border for their internal lines. Instead, Word looks at the RowSep/ColSep properties of the TableHeader and TableFooter objects.
I added the changes below.
donordata = table;
donordata.corpus = rand(2,1);
donordata.expends = rand(2,1);
yr = 2012;
addReportSummaryTable(donordata,yr)
function addReportSummaryTable(donordata,yr)
import mlreportgen.report.*
import mlreportgen.dom.*
rpt = Report('Annual Report', 'docx');
open(rpt);
% Annual summary table
corpus=donordata.corpus;
expendables=donordata.expends;
headerStyle ={ Bold(true), ...
Border("single"), ...
ColSep("none") ...
};
tableStyle = { Width("80%"), ...
Border("single"), ...
RowSep("none"), ...
ColSep("none") ...
};
footerStyle = { Bold(true), ...
Border("single"), ...
ColSep("none") ...
};
entriesStyle = {FontFamily("Calibri"), ...
FontSize("11pt")};
headerContent=[{'Fund Summary'} compose('Fiscal Year %d',[yr-1 yr])];
bodyContent =[[{'Endowment (principal)'} cellstr(num2currency(corpus))];
[{'Expendable (income and contributions'} cellstr(num2currency(expendables))]];
totals=corpus+expendables;
footerContent = [{'Total fund balance'} cellstr(num2currency(totals))];
% tableContent = [headerContent; bodyContent; footerContent];
t= FormalTable(headerContent, bodyContent, footerContent);
t.Style=tableStyle;
t.Header.Style=[t.Header.Style,headerStyle];
t.Footer.Style=[t.Footer.Style,footerStyle];
% make header/footer borders visible in Word (.docx)
t.Header.RowSep = "single";
t.Header.RowSepWidth = "1pt";
t.Footer.RowSep = "single";
t.Footer.RowSepWidth = "1pt";
t.TableEntriesHAlign = "right";
t.TableEntriesStyle = [t.TableEntriesStyle, entriesStyle];
g=TableColSpecGroup();
s=TableColSpec();
s.Style={HAlign('left')};
g.ColSpecs=s;
t.ColSpecGroups=g;
t.HAlign = "center";
append(rpt, t);
close(rpt);
end
function output = num2currency(number)
for ii = 1:length(number)
output{ii} = horzcat('$',num2str(number(ii)));
end
end
dpb
2025년 12월 10일
Kevin Holly
2025년 12월 10일
- I am having trouble with the vertical alignment. I will need to get back to you on this.
- To eliminate the white space you can do:
targetHeight = "0.2in"; % tweak as needed
% Header rows
for i = 1:t.Header.NRows
r = t.Header.row(i);
r.Height = targetHeight; % sets RowHeight.Type = "exact"
end
% Body rows
for i = 1:t.Body.NRows
r = t.Body.row(i);
r.Height = targetHeight;
end
% Footer rows
for i = 1:t.Footer.NRows
r = t.Footer.row(i);
r.Height = targetHeight;
end
3. I can report to development to improve the documentation. I agree with you here.
Kevin Holly
2025년 12월 11일
@dpb So, I discover that the below code does work. It actually changes the vertical alignment - I verified in Word, although it did not look like it when I did it yesterday. The problem was that there was a space underneath the text due to line spacing being multiple instead of single. I was able to fix this by adding a OuterMargin Style
Previous:
for i = 1:t.Header.NRows
r = t.Header.row(i);
for j = 1:r.NEntries
e = r.Entries(j);
e.Style = [e.Style {VAlign("bottom")}]; % Ensure vertical alignment is set to bottom
end
end
Fixed linespace with OuterMargin:
for i = 1:t.Header.NRows
r = t.Header.row(i);
for j = 1:r.NEntries
e = r.Entries(j);
e.Style = [e.Style {VAlign("bottom"), OuterMargin("0pt", "0pt","0pt","0pt")}];
end
end
카테고리
도움말 센터 및 File Exchange에서 Tables에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
