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);

		// 整理插入节点内容
		child_arr := array();
		alignment_marshal := style.Alignment.Marshal();
		if istable(alignment_marshal['children']) or istable(alignment_marshal['attributes']) then
		begin
			alignment_flag := 1;
			child_arr union= array(alignment_marshal);
		end
		protection_marshal := style.Protection.Marshal();
		if istable(protection_marshal['children']) or istable(protection_marshal['attributes']) then
		begin
			protection_flag := 1;
			child_arr union= array(protection_marshal);
		end

		attr_arr := array(
			'numFmtId': style.NumberFormat.NumFmtId ? : "0",
			'fontId': font_id,
			'fillId': fill_id,
			'borderId': border_id,
			'xfId': 0,
		);
		if font_id <> '0' then attr_arr['applyFont'] := '1';
		if border_id <> '0' then attr_arr['applyBorder'] := '1';
		if numfmt_id <> '0' then attr_arr['applyNumberFormat'] := '1';
		if fill_id <> '0' then attr_arr['applyFill'] := '1';
		if alignment_flag then attr_arr['applyAlignment'] := '1';
		if protection_flag then attr_arr['applyProtection'] := '1';

		arr := array('name': 'xf', 'type': 'element',
					 'attributes': attr_arr,
					 'children': child_arr,
		);
		node := node.FirstChildElement('cellXfs');
		count := node.GetAttribute('count');
		node.InsertEndChild(arr);
		node.SetAttribute('count', strtoint(count) + 1);
		//node.print;
		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;
