TSOffice/funcext/TSOffice/worksheet/xlsxStyles.tsf

95 lines
2.8 KiB
Plaintext

Type xlsxStyles = Class
Function Create(sheetobj, file);
Begin
sheet_ := sheetobj;
file_ := file;
styleXmlFile_ := file_.WorkBook().GetXmlFileObj('xl/styles.xml');
End
Function GetStyleId(style);
Begin
node := styleXmlFile_.FirstChildElement('styleSheet');
font_id := insertNode(node, 'fonts', style.Font);
border_id := insertNode(node, 'borders', style.Border);
fill_id := insertNode(node, 'fills', style.Fill);
processNumFmtId(node, 'numFmts', style.NumberFormat);
numfmt_id := insertNode(node, 'numFmts', style.NumberFormat);
alignment_marshal := style.Alignment.Marshal();
if istable(alignment_marshal['children']) or istable(alignment_marshal['attributes']) then
alignment_flag := 1;
protection_marshal := style.Protection.Marshal();
if istable(protection_marshal['children']) or istable(protection_marshal['attributes']) then
protection_flag := 1;
xf := TOfficeObj('TXf');
xf.NumFmtId := style.NumberFormat.NumFmtId ? : 0;
xf.FontId := font_id;
xf.FillId := fill_id;
xf.BorderId := border_id;
xf.XfId := 0;
xf.ApplyFont := font_id <> '0' ? 1 : nil;
xf.ApplyFill := fill_id <> '0' ? 1 : nil;
xf.ApplyBorder := border_id <> '0' ? 1 : nil;
xf.ApplyAlignment := alignment_flag ?: nil;
xf.ApplyProtection := protection_flag ?: nil;
xf.Alignment := style.Alignment;
xf.Protection := style.Protection;
node := node.FirstChildElement('cellXfs');
count := node.GetAttribute('count');
node.InsertEndChild(xf.Marshal());
node.SetAttribute('count', strtoint(count) + 1);
return count;
End
class Function NewObject(sheetname, file);
Begin
o := file.WorkBook().GetSheetObj(sheetname);
if not ifObj(o) then return 0;
styles := new xlsxStyles(o, file);
return styles;
End;
private
Function insertNode(rootNode, childName, obj);
Begin
marshal := obj.Marshal();
if not istable(marshal['children']) and not istable(marshal['attributes']) then return '0';
node := rootNode.FirstChildElement(childName);
if not ifObj(node) then
begin
info := array('name': childName, 'type': 'element', 'attributes': ('count': '0'));
rootNode.InsertFirstChild(info);
node := rootNode.FirstChildElement(childName);
end
count := node.GetAttribute('count');
node.InsertEndChild(marshal);
node.SetAttribute('count', strtoint(count) + 1);
//node.Print;
return count;
End
Function processNumFmtId(rootNode, childName, obj);
Begin
if ifnil(obj.FormatCode) or not ifnil(obj.numFmtId) then return;
node := rootNode.FirstChildElement(childName);
if not ifObj(node) then obj.numFmtId := '1';
else begin
node := node.LastChildElement('numFmt');
if not ifObj(node) then obj.numFmtId := '1';
else begin
id := node.GetAttribute('numFmtId');
obj.numFmtId := strtoint(id) + 1;
end
end
End
private
sheet_; //XmlSheet对象
file_; //TSExcelFile对象
styleXmlFile_; //xmlFile对象
End;