private XElement WriteStyles_dxfs_dxf_alignment(CellStyleXmlWrapper dxfStyle) { XElement alignmentXml = new XElement(ExcelCommon.Schema_WorkBook_Main + "alignment"); alignmentXml.Add(new XAttribute("horizontal", dxfStyle.HorizontalAlignment.SetAlignString())); alignmentXml.Add(new XAttribute("vertical", dxfStyle.VerticalAlignment.SetAlignString())); alignmentXml.Add(new XAttribute("textRotation", dxfStyle.TextRotation.ToString(CultureInfo.InvariantCulture))); alignmentXml.Add(new XAttribute("wrapText", dxfStyle.WrapText ? "1" : "0")); alignmentXml.Add(new XAttribute("shrinkToFit", dxfStyle.ShrinkToFit ? "1" : "0")); alignmentXml.Add(new XAttribute("readingOrder", dxfStyle.ShrinkToFit ? "1" : "0")); return alignmentXml; }
/// <summary> /// 返回样式ID /// </summary> /// <param name="Style"></param> /// <returns></returns> private string GetStyleId(CellStyleXmlWrapper Style) { if (this.Context.GlobalStyles.CellXfs.ExistsKey(Style.Id)) { return this.Context.GlobalStyles.CellXfs.FindIndexByID(Style.Id).ToString(CultureInfo.InvariantCulture); } else { #region “添加样数字格式化” NumberFormatXmlWrapper number = Style.NumberFormat; if (!this.Context.GlobalStyles.NumberFormats.FindByID(Style.NumberFormat.Id, ref number)) { number.NumFmtId = this.Context.GlobalStyles.NumberFormats.NextId++; this.Context.GlobalStyles.NumberFormats.Add(Style.NumberFormat.Format, Style.NumberFormat); } Style.NumberFormatId = number.NumFmtId; #endregion if (this.Context.GlobalStyles.Fonts.ExistsKey(Style.Font.Id) == false) { this.Context.GlobalStyles.Fonts.Add(Style.Font.Id, Style.Font); } Style.FontId = this.Context.GlobalStyles.Fonts.FindIndexByID(Style.Font.Id); if (this.Context.GlobalStyles.Fills.ExistsKey(Style.Fill.Id) == false) { this.Context.GlobalStyles.Fills.Add(Style.Fill.Id, Style.Fill); } Style.FillId = this.Context.GlobalStyles.Fills.FindIndexByID(Style.Fill.Id); if (this.Context.GlobalStyles.Borders.ExistsKey(Style.Border.Id) == false) { Style.ApplyBorder = true; this.Context.GlobalStyles.Borders.Add(Style.Border.Id, Style.Border); } Style.BorderId = this.Context.GlobalStyles.Borders.FindIndexByID(Style.Border.Id); this.Context.GlobalStyles.CellXfs.Add(Style.Id, Style); return this.Context.GlobalStyles.CellXfs.FindIndexByID(Style.Id).ToString(CultureInfo.InvariantCulture); } }
/// <summary> /// FileName:styles.xml /// <para>NodePath:styleSheet/cellXfs/xf</para> /// </summary> private XElement WriteStyles_cellXfs_xf(CellStyleXmlWrapper cellxfs) { XElement xfXml = new XElement(ExcelCommon.Schema_WorkBook_Main + "xf"); if (cellxfs.NumberFormatId > int.MinValue) { xfXml.Add(new XAttribute("numFmtId", cellxfs.NumberFormatId.ToString(CultureInfo.InvariantCulture))); } if (cellxfs.FontId > int.MinValue) { xfXml.Add(new XAttribute("fontId", cellxfs.FontId.ToString(CultureInfo.InvariantCulture))); } if (cellxfs.FillId > int.MinValue) { xfXml.Add(new XAttribute("fillId", cellxfs.FillId.ToString(CultureInfo.InvariantCulture))); } if (cellxfs.BorderId > int.MinValue) { xfXml.Add(new XAttribute("borderId", cellxfs.BorderId.ToString(CultureInfo.InvariantCulture))); } if (cellxfs.XfId > int.MinValue) { xfXml.Add(new XAttribute("xfId", cellxfs.XfId.ToString(CultureInfo.InvariantCulture))); } if (cellxfs.HorizontalAlignment != ExcelHorizontalAlignment.Left || cellxfs.VerticalAlignment != ExcelVerticalAlignment.Bottom || cellxfs.Indent > 0 || cellxfs.TextRotation > 0 || cellxfs.ShrinkToFit) { xfXml.Add(new XAttribute("applyAlignment", "1")); if (cellxfs.Locked == false || cellxfs.Hidden) { xfXml.Add(new XAttribute("applyProtection", "1")); } XElement alignmentXml = new XElement(ExcelCommon.Schema_WorkBook_Main + "alignment"); if (cellxfs.HorizontalAlignment != ExcelHorizontalAlignment.Left) { alignmentXml.Add(new XAttribute("horizontal", cellxfs.HorizontalAlignment.SetAlignString())); } if (cellxfs.VerticalAlignment != ExcelVerticalAlignment.Bottom) { alignmentXml.Add(new XAttribute("vertical", cellxfs.VerticalAlignment.SetAlignString())); } if (cellxfs.Indent > 0) { alignmentXml.Add(new XAttribute("indent", cellxfs.Indent.ToString(CultureInfo.InvariantCulture))); } if (cellxfs.TextRotation > 0) { alignmentXml.Add(new XAttribute("textRotation", cellxfs.TextRotation.ToString(CultureInfo.InvariantCulture))); } if (cellxfs.WrapText) { alignmentXml.Add(new XAttribute("wrapText", "1")); } if (cellxfs.ShrinkToFit) { alignmentXml.Add(new XAttribute("shrinkToFit", "1")); } if (cellxfs.ReadingOrder) { alignmentXml.Add(new XAttribute("readingOrder", "1")); } xfXml.Add(alignmentXml); } if (cellxfs.Locked == false || cellxfs.Hidden) { XElement protectionXml = new XElement(ExcelCommon.Schema_WorkBook_Main + "protection"); if (cellxfs.Locked == false) { protectionXml.Add(new XAttribute("locked", "0")); } if (cellxfs.Hidden) { protectionXml.Add(new XAttribute("hidden", "1")); } xfXml.Add(protectionXml); } return xfXml; }
private XElement WriteStyles_dxfs_dxf(CellStyleXmlWrapper dxfStyle) { XElement result = new XElement(ExcelCommon.Schema_WorkBook_Main + "dxf"); result.Add(WriteFont(dxfStyle.Font)); result.Add(WriteFill(dxfStyle.Fill)); result.Add(WriteStyles_dxfs_dxf_alignment(dxfStyle)); return result; }
/// <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/extLst</para> /// </summary> /// <param name="root"></param> /// <returns></returns> public void ReadStyles_cellStyleXfs_xf_extLst(CellStyleXmlWrapper target, XElement item) { if (item.Attribute("uri") != null) { //todo:有待完善 } }
/// <summary> /// FileName:styles.xml /// <para>NodePath:styleSheet/cellStyleXfs/xf/protection</para> /// </summary> /// <param name="root"></param> /// <returns></returns> public void ReadStyles_cellStyleXfs_xf_protection(CellStyleXmlWrapper target, XElement item) { if (item.Attribute("locked") != null) { target.Locked = item.Attribute("locked").Value.Equals("0") ? false : true; } if (item.Attribute("hidden") != null) { target.Hidden = item.Attribute("hidden").Value.Equals("1") ? true : false; } }
/// <summary> /// FileName:styles.xml /// <para>NodePath:styleSheet/cellStyleXfs/xf/alignment</para> /// </summary> /// <param name="root"></param> /// <returns></returns> public void ReadStyles_cellStyleXfs_xf_alignment(CellStyleXmlWrapper target, XElement item) { if (item.Attribute("horizontal") != null) { target.HorizontalAlignment = CellStyleXmlWrapper.GetHorizontalAlign(item.Attribute("horizontal").Value); } if (item.Attribute("vertical") != null) { target.VerticalAlignment = CellStyleXmlWrapper.GetVerticalAlign(item.Attribute("vertical").Value); } if (item.Attribute("wrapText") != null) { target.WrapText = item.Attribute("wrapText").Value.Equals("1") ? true : false; } }
/// <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; } } }