internal NamedStyleXmlWrapper(int styleXfId, string name, WorkBookStylesWrapper styles) { this.StyleXfId = styleXfId; this.Name = name; //todo: //this.BuildInId = GetXmlNodeInt(BuildInIdPath); this._Styles = styles; //this._Style = new StyleWrapper(styles, styles.NamedStylePropertyChange, -1, this.Name, this._StyleXfId); }
/// <summary> /// FileName:styles.xml /// <para>NodePath:styleSheet/cellStyles</para> /// </summary> private XElement WriteStyles_cellStyles(WorkBookStylesWrapper style) { XElement result = new XElement(ExcelCommon.Schema_WorkBook_Main + "cellStyles"); if (style.NamedStyles.Count > 0) { result.Add(new XAttribute("count", style.NamedStyles.Count.ToString(CultureInfo.InvariantCulture))); foreach (var nameStyle in style.NamedStyles) { XElement cellStyleXml = new XElement(ExcelCommon.Schema_WorkBook_Main + "cellStyle"); if (nameStyle.Name.IsNotEmpty()) { cellStyleXml.Add(new XAttribute("name", nameStyle.Name)); } if (nameStyle.BuildInId > int.MinValue) { cellStyleXml.Add(new XAttribute("builtinId", nameStyle.BuildInId.ToString(CultureInfo.InvariantCulture))); } if (nameStyle.XfId >= 0) { cellStyleXml.Add(new XAttribute("xfId", style.CellXfs[nameStyle.XfId].newID.ToString(CultureInfo.InvariantCulture))); } result.Add(cellStyleXml); } } else { result.Add(new XAttribute("count", "1"), new XElement(ExcelCommon.Schema_WorkBook_Main + "cellStyle", new XAttribute("name", "Normal"), new XAttribute("builtinId", "0"), new XAttribute("xfId", "0")) ); } return result; }
/// <summary> /// FileName:styles.xml /// <para>NodePath:styleSheet/cellStyleXfs</para> /// </summary> private XElement WriteStyles_cellStyleXfs(WorkBookStylesWrapper style) { XElement result = new XElement(ExcelCommon.Schema_WorkBook_Main + "cellStyleXfs"); if (style.CellStyleXfs.Count > 0) { result.Add(new XAttribute("count", style.CellStyleXfs.Count.ToString(CultureInfo.InvariantCulture))); foreach (var csxfs in style.CellStyleXfs) { XElement xfXml = new XElement(ExcelCommon.Schema_WorkBook_Main + "xf"); if (csxfs.NumberFormatId > 0) { xfXml.Add(new XAttribute("numFmtId", csxfs.NumberFormatId.ToString(CultureInfo.InvariantCulture))); } if (csxfs.FontId > int.MinValue) { xfXml.Add(new XAttribute("fontId", csxfs.FontId.ToString(CultureInfo.InvariantCulture))); } if (csxfs.FillId > int.MinValue) { xfXml.Add(new XAttribute("fillId", csxfs.FillId.ToString(CultureInfo.InvariantCulture))); } if (csxfs.BorderId > int.MinValue) { xfXml.Add(new XAttribute("borderId", csxfs.BorderId.ToString(CultureInfo.InvariantCulture))); } result.Add(xfXml); } } else { result.Add(new XAttribute("count", "1"), new XElement(ExcelCommon.Schema_WorkBook_Main + "xf", new XAttribute("numFmtId", "0"), new XAttribute("fontId", "0"), new XAttribute("fillId", "0"), new XAttribute("borderId", "0") ) ); } return result; }
/// <summary> /// FileName:styles.xml /// <para>NodePath:styleSheet/cellXfs</para> /// </summary> private XElement WriteStyles_cellXfs(WorkBookStylesWrapper style) { XElement result = new XElement(ExcelCommon.Schema_WorkBook_Main + "cellXfs"); if (style.CellXfs.Count > 0) { result.Add(new XAttribute("count", style.CellXfs.Count.ToString(CultureInfo.InvariantCulture))); foreach (var cellxfs in style.CellXfs) { result.Add(WriteStyles_cellXfs_xf(cellxfs)); } } else { result.Add(new XAttribute("count", "1"), new XElement(ExcelCommon.Schema_WorkBook_Main + "xf", new XAttribute("numFmtId", "0"), new XAttribute("fontId", "0"), new XAttribute("fillId", "0"), new XAttribute("borderId", "0"), new XAttribute("xfId", "0")) ); } return result; }
/// <summary> /// FileName:styles.xml /// <para>NodePath:styleSheet/fills</para> /// </summary> private XElement WriteStyles_fills(WorkBookStylesWrapper style) { XElement fillsXml = new XElement(ExcelCommon.Schema_WorkBook_Main + "fills"); int count = style.Fills.Count; if (count > 0) { fillsXml.Add(new XAttribute("count", count.ToString(CultureInfo.InvariantCulture))); foreach (var fill in style.Fills) { fillsXml.Add(WriteFill(fill)); } } else { fillsXml.Add(new XAttribute("count", "2"), new XElement(ExcelCommon.Schema_WorkBook_Main + "fill", new XElement(ExcelCommon.Schema_WorkBook_Main + "patternFill", new XAttribute("patternType", "none")), new XElement(ExcelCommon.Schema_WorkBook_Main + "patternFill", new XAttribute("patternType", "gray125")) ) ); } return fillsXml; }
/// <summary> /// FileName:styles.xml /// <para>NodePath:styleSheet/borders</para> /// </summary> private XElement WriteStyles_borders(WorkBookStylesWrapper style) { XElement bordersXml = new XElement(ExcelCommon.Schema_WorkBook_Main + "borders"); if (style.Borders.Count > 0) { bordersXml.Add(new XAttribute("count", style.Borders.Count.ToString(CultureInfo.InvariantCulture))); foreach (var border in style.Borders) { bordersXml.Add(WriteBorder(border)); } } else { bordersXml.Add(new XAttribute("count", "1"), new XElement(ExcelCommon.Schema_WorkBook_Main + "border", new XElement(ExcelCommon.Schema_WorkBook_Main + "left"), new XElement(ExcelCommon.Schema_WorkBook_Main + "right"), new XElement(ExcelCommon.Schema_WorkBook_Main + "top"), new XElement(ExcelCommon.Schema_WorkBook_Main + "bottom"), new XElement(ExcelCommon.Schema_WorkBook_Main + "diagonal") ) ); } return bordersXml; }
internal NamedStyleXmlWrapper(WorkBookStylesWrapper styles) { this._Styles = styles; this.BuildInId = int.MinValue; }
/// <summary> /// FileName:styles.xml /// <para>NodePath:styleSheet/fills</para> /// </summary> /// <param name="root"></param> /// <returns></returns> public void ReadStyles_fills(WorkBookStylesWrapper target, XElement item) { foreach (XElement fill in item.Nodes()) { FillXmlWrapper fillObj = new FillXmlWrapper(); ReadStyles_fill(fillObj, fill); target.Fills.Add(fillObj); } }
/// <summary> /// FileName:styles.xml /// <para>NodePath:styleSheet/colors</para> /// </summary> /// <param name="root"></param> /// <returns></returns> public void ReadStyles_colors(WorkBookStylesWrapper target, XElement item) { //todo:完善 }
/// <summary> /// FileName:styles.xml /// <para>NodePath:styleSheet</para> /// </summary> /// <param name="calcPrRoot"></param> public void WriteStyles(WorkBookStylesWrapper style) { PackagePart partSyles = this.Context.Package.CreatePart(ExcelCommon.Uri_Styles, @"application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml", CompressionOption.Normal); XElement styleXml = new XElement(ExcelCommon.Schema_WorkBook_Main + "styleSheet", new XAttribute(XNamespace.Xmlns + "d", ExcelCommon.Schema_WorkBook_Main.NamespaceName)); styleXml.Add(WriteStyles_numFmts(style)); styleXml.Add(WriteStyles_fonts(style)); styleXml.Add(WriteStyles_fills(style)); styleXml.Add(WriteStyles_borders(style)); styleXml.Add(WriteStyles_cellStyleXfs(style)); styleXml.Add(WriteStyles_cellXfs(style)); styleXml.Add(WriteStyles_cellStyles(style)); styleXml.Add(WriteStyles_dxfs(style)); styleXml.Add(WriteStyles_tableStyles(style)); XDocument doc = new XDocument(new XDeclaration("1.0", "utf-8", "yes"), styleXml); using (var stream = partSyles.GetStream(FileMode.Create, FileAccess.Write)) { doc.Save(stream); stream.Flush(); } this.Context.Package.GetPart(ExcelCommon.Uri_Workbook).CreateRelationship( PackUriHelper.GetRelativeUri(ExcelCommon.Uri_Workbook, ExcelCommon.Uri_Styles), TargetMode.Internal, ExcelCommon.Schema_Relationships + "/styles"); }
/// <summary> /// FileName:styles.xml /// <para>NodePath:styleSheet/cellStyles</para> /// </summary> /// <param name="root"></param> /// <returns></returns> public void ReadStyles_cellStyles(WorkBookStylesWrapper target, XElement item) { foreach (XElement node in item.Nodes()) { NamedStyleXmlWrapper nameStyle = new NamedStyleXmlWrapper(target); nameStyle.Name = node.Attribute("name").Value; if (node.Attribute("xfId") != null) { nameStyle.XfId = int.Parse(node.Attribute("xfId").Value); } if (node.Attribute("builtinId") != null) { nameStyle.BuildInId = int.Parse(node.Attribute("builtinId").Value); } target.NamedStyles.Add(nameStyle); } }
/// <summary> /// FileName:styles.xml /// <para>NodePath:styleSheet/tableStyles</para> /// </summary> /// <param name="root"></param> /// <returns></returns> public void ReadStyles_tableStyles(WorkBookStylesWrapper target, XElement item) { foreach (var attr in item.Attributes()) { target.TableStyles.Attributes.Clear(); target.TableStyles.Attributes.Add(attr.Name.LocalName, attr.Value); } }
/// <summary> /// FileName:styles.xml /// <para>NodePath:styleSheet/cellXfs</para> /// </summary> /// <param name="root"></param> /// <returns></returns> public void ReadStyles_cellXfs(WorkBookStylesWrapper target, XElement item) { foreach (XElement node in item.Nodes()) { CellStyleXmlWrapper cellStyle = new CellStyleXmlWrapper(); ReadStyles_cellStyleXfs_xf(cellStyle, node, target); target.CellXfs.Add(cellStyle); } }
/// <summary> /// FileName:styles.xml /// <para>NodePath:styleSheet/cellStyleXfs/xf</para> /// </summary> /// <param name="root"></param> /// <returns></returns> public void ReadStyles_cellStyleXfs_xf(CellStyleXmlWrapper target, XElement item, WorkBookStylesWrapper currentStyle) { if (item.Attribute("numFmtId") != null) { target.NumberFormatId = int.Parse(item.Attribute("numFmtId").Value); if (target.NumberFormatId != 0) { target.NumberFormat = currentStyle.NumberFormats.FirstOrDefault(n => n.NumFmtId == target.NumberFormatId); //target.NumberFormat = currentStyle.NumberFormats[target.NumberFormatId]; } } if (item.Attribute("fontId") != null) { target.FontId = int.Parse(item.Attribute("fontId").Value); target.Font = currentStyle.Fonts[target.FontId]; } if (item.Attribute("fillId") != null) { target.FillId = int.Parse(item.Attribute("fillId").Value); target.Fill = currentStyle.Fills[target.FillId]; } if (item.Attribute("borderId") != null) { target.BorderId = int.Parse(item.Attribute("borderId").Value); target.Border = currentStyle.Borders[target.BorderId]; } if (item.Attribute("applyBorder") != null) { target.ApplyBorder = string.Compare(item.Attribute("applyBorder").Value, "1") == 0 ? true : false; } if (item.Attribute("applyAlignment") != null) { target.ApplyAlignment = string.Compare(item.Attribute("applyAlignment").Value, "1") == 0 ? true : false; } foreach (XElement node in item.Nodes()) { switch (node.Name.LocalName) { case "alignment": ReadStyles_cellStyleXfs_xf_alignment(target, node); break; case "protection": ReadStyles_cellStyleXfs_xf_protection(target, node); break; case "extLst": ReadStyles_cellStyleXfs_xf_extLst(target, node); break; } } }
/// <summary> /// FileName:styles.xml /// <para>NodePath:styleSheet/borders</para> /// </summary> /// <param name="root"></param> /// <returns></returns> public void ReadStyles_borders(WorkBookStylesWrapper target, XElement item) { foreach (XElement border in item.Nodes()) { BorderXmlWrapper borderObj = new BorderXmlWrapper(); ReadStyles_border(borderObj, border); target.Borders.Add(borderObj); } }
/// <summary> /// FileName:styles.xml /// <para>NodePath:styleSheet/dxfs</para> /// </summary> private XElement WriteStyles_dxfs(WorkBookStylesWrapper style) { XElement result = new XElement(ExcelCommon.Schema_WorkBook_Main + "dxfs", new XAttribute("count", style.CellXfs.Count.ToString(CultureInfo.InvariantCulture))); foreach (CellStyleXmlWrapper item in style.CellXfs) { result.Add(WriteStyles_dxfs_dxf(item)); } return result; }
/// <summary> /// FileName:styles.xml /// <para>NodePath:styleSheet/tableStyles</para> /// </summary> private XElement WriteStyles_tableStyles(WorkBookStylesWrapper style) { XElement result = new XElement(ExcelCommon.Schema_WorkBook_Main + "tableStyles", new XAttribute("count", "0"), new XAttribute("defaultTableStyle", "TableStyleMedium9"), new XAttribute("defaultPivotStyle", "PivotStyleLight16") ); return result; }
/// <summary> /// FileName:styles.xml /// <para>NodePath:styleSheet/numFmts</para> /// </summary> /// <param name="root"></param> /// <returns></returns> public void ReadStyles_numFmts(WorkBookStylesWrapper target, XElement item) { foreach (XElement numFmt in item.Nodes()) { NumberFormatXmlWrapper nf = new NumberFormatXmlWrapper(false); foreach (var attr in numFmt.Attributes()) { if (attr.Name.LocalName == "numFmtId") { nf.NumFmtId = int.Parse(attr.Value); continue; } if (attr.Name.LocalName == "formatCode") { nf.Format = attr.Value; continue; } } if (nf.NumFmtId >= target.NumberFormats.NextId) { target.NumberFormats.NextId = nf.NumFmtId + 1; } target.NumberFormats.Add(nf.Id, nf); } }
/// <summary> /// FileName:styles.xml /// <para>NodePath:styleSheet/numFmts</para> /// </summary> private XElement WriteStyles_numFmts(WorkBookStylesWrapper style) { int count = style.NumberFormats.Count(n => n.BuildIn == false); if (count == 0) return null; XElement numFmts = new XElement(ExcelCommon.Schema_WorkBook_Main + "numFmts"); numFmts.Add(new XAttribute("count", count.ToString(CultureInfo.InvariantCulture))); foreach (var numberFormat in style.NumberFormats) { if (numberFormat.BuildIn == false && numberFormat.Format.IsNotEmpty()) { XElement numFmt = new XElement(ExcelCommon.Schema_WorkBook_Main + "numFmt"); numFmt.Add(new XAttribute("numFmtId", numberFormat.NumFmtId.ToString(CultureInfo.InvariantCulture))); numFmt.Add(new XAttribute("formatCode", numberFormat.Format)); numFmts.Add(numFmt); } } return numFmts; }
/// <summary> /// FileName:styles.xml /// <para>NodePath:styleSheet/fonts</para> /// </summary> /// <param name="root"></param> /// <returns></returns> public void ReadStyles_fonts(WorkBookStylesWrapper target, XElement item) { foreach (XElement fontElement in item.Nodes()) { FontXmlWrapper fontObj = new FontXmlWrapper(); foreach (XElement styleElement in fontElement.Nodes()) { switch (styleElement.Name.LocalName) { case "b": { fontObj.Bold = true; break; } case "i": { fontObj.Italic = true; break; } case "u": { fontObj.UnderLine = true; break; } case "shadow": { fontObj.Shadow = true; break; } case "vertAlign": { if (styleElement.Attribute("val") != null) { fontObj.VerticalAlign = styleElement.Attribute("val").Value; } break; } case "sz": { if (styleElement.Attribute("val") != null) { fontObj.Size = float.Parse(styleElement.Attribute("val").Value); } break; } case "color": { fontObj.Color = new ColorXmlWrapper(); this.ReadStyles_Color(styleElement, fontObj.Color); break; } case "rFont": { // this._ = reader.GetAttribute("val"); break; } case "name": { if (styleElement.Attribute("val") != null) { fontObj.Name = styleElement.Attribute("val").Value; } break; } case "family": { if (styleElement.Attribute("val") != null) { fontObj.Family = int.Parse(styleElement.Attribute("val").Value); } break; } case "charset": { if (styleElement.Attribute("val") != null) { fontObj.Charset = int.Parse(styleElement.Attribute("val").Value); } break; } case "scheme": { if (styleElement.Attribute("val") != null) { fontObj.Scheme = styleElement.Attribute("val").Value; } break; } } } target.Fonts.Add(fontObj.Id, fontObj); } }
/// <summary> /// FileName:styles.xml /// <para>NodePath:styleSheet/fonts</para> /// </summary> private XElement WriteStyles_fonts(WorkBookStylesWrapper style) { XElement fontsXml = new XElement(ExcelCommon.Schema_WorkBook_Main + "fonts"); int count = style.Fonts.Count; if (count > 0) { fontsXml.Add(new XAttribute("count", count.ToString(CultureInfo.InvariantCulture))); foreach (var font in style.Fonts) { fontsXml.Add(WriteFont(font)); } } else { count = 1; fontsXml.Add(); fontsXml.Add(new XAttribute("count", "1"), new XElement(ExcelCommon.Schema_WorkBook_Main + "font", new XElement(ExcelCommon.Schema_WorkBook_Main + "sz", new XAttribute("val", "11")), new XElement(ExcelCommon.Schema_WorkBook_Main + "name", new XAttribute("val", "Calibri")) ) ); } return fontsXml; }
/// <summary> /// FileName:styles.xml /// <para>NodePath:styleSheet</para> /// </summary> /// <param name="root"></param> /// <returns></returns> public void ReadStyles(XElement root, WorkBookStylesWrapper target) { foreach (XElement item in root.Nodes()) { switch (item.Name.LocalName) { case "numFmts": ReadStyles_numFmts(target, item); break; case "fonts": ReadStyles_fonts(target, item); break; case "fills": ReadStyles_fills(target, item); break; case "borders": ReadStyles_borders(target, item); break; case "cellStyleXfs": ReadStyles_cellStyleXfs(target, item); break; case "cellXfs": ReadStyles_cellXfs(target, item); break; case "cellStyles": ReadStyles_cellStyles(target, item); break; case "dxfs": ReadStyles_cellXfs(target, item); break; case "tableStyles": ReadStyles_tableStyles(target, item); break; case "colors": ReadStyles_colors(target, item); break; } } }