public static uint Fill(this Fills fills, Fill fill) { var fillIndex = fills.MakeSame(fill); fills.Count = (uint)fills.Elements().Count(); return(fillIndex); }
public void SetCellBackgroundColor(Int32 rowIndex, Int32 columnIndex, String bgColor) { // ищем ячейку и её старый формат Row row = GetRow((UInt32)rowIndex); Cell cell = GetCell(row, (UInt32)columnIndex); CellFormats cellFormats = stylesheet.GetFirstChild <CellFormats>(); CellFormat cellFormat = cellFormats.Elements <CellFormat>().ElementAt <CellFormat>((Int32)cell.StyleIndex.Value); Int32 newCellFormatIndex = cellFormats.Elements <CellFormat>().Count <CellFormat>(); CellFormat newCellFormat = (CellFormat)cellFormat.Clone(); { // новый background ----------------------------------------- проверить существующий to do ... Fills fills = stylesheet.GetFirstChild <Fills>(); Int32 fillIndex = fills.Elements <Fill>().Count <Fill>(); fills.Append( new Fill { PatternFill = new PatternFill { PatternType = PatternValues.Solid, ForegroundColor = new ForegroundColor() { Rgb = "ff" + bgColor } } } ); newCellFormat.FillId = (UInt32)fillIndex; } cellFormats.Append(newCellFormat); cell.StyleIndex = (UInt32)newCellFormatIndex; }
public void AddAlignment(string name, HorizontalAlignmentValues hv, VerticalAlignmentValues vv, string c) { var filepath = getOutputDir() + name; using (SpreadsheetDocument spreadSheet = SpreadsheetDocument.Open(filepath, true)) { var iter = spreadSheet.WorkbookPart.WorksheetParts.GetEnumerator(); iter.MoveNext(); var workSheetPart = iter.Current; Cell cell = InsertCellInWorksheet(GetColumnName(c), GetRowIndex(c), workSheetPart); Fills fs = spreadSheet.WorkbookPart.WorkbookStylesPart.Stylesheet.Fills; var cf = spreadSheet.WorkbookPart.WorkbookStylesPart.Stylesheet.CellFormats; CellFormat cellFormat2 = new CellFormat() { NumberFormatId = 0, FontId = 0, FillId = (UInt32)(fs.Elements <Fill>().Count() - 1), BorderId = 0, FormatId = 0, Alignment = new Alignment() { Horizontal = hv, Vertical = vv }, ApplyFill = true }; cf.Append(cellFormat2); spreadSheet.WorkbookPart.WorkbookStylesPart.Stylesheet.Save(); cell.StyleIndex = (UInt32)(spreadSheet.WorkbookPart.WorkbookStylesPart.Stylesheet.CellFormats.Elements <CellFormat>().Count() - 1); workSheetPart.Worksheet.Save(); } }
public void AddFill(string name, string color, string c) { var filepath = getOutputDir() + name; using (SpreadsheetDocument spreadSheet = SpreadsheetDocument.Open(filepath, true)) { var iter = spreadSheet.WorkbookPart.WorksheetParts.GetEnumerator(); iter.MoveNext(); var workSheetPart = iter.Current; Cell cell = InsertCellInWorksheet(GetColumnName(c), GetRowIndex(c), workSheetPart); Fills fs = spreadSheet.WorkbookPart.WorkbookStylesPart.Stylesheet.Fills; var cf = spreadSheet.WorkbookPart.WorkbookStylesPart.Stylesheet.CellFormats; Fill fill1 = new Fill(); PatternFill patternFill1 = new PatternFill() { PatternType = PatternValues.Solid }; ForegroundColor foregroundColor1 = new ForegroundColor() { Rgb = color }; BackgroundColor backgroundColor1 = new BackgroundColor() { Indexed = (UInt32Value)64U }; patternFill1.Append(foregroundColor1); patternFill1.Append(backgroundColor1); fill1.Append(patternFill1); fs.Append(fill1); CellFormat cellFormat2 = new CellFormat() { NumberFormatId = 0, FontId = 0, FillId = (UInt32)(fs.Elements <Fill>().Count() - 1), BorderId = 0, FormatId = 0, ApplyFill = true }; cf.Append(cellFormat2); spreadSheet.WorkbookPart.WorkbookStylesPart.Stylesheet.Save(); cell.StyleIndex = (UInt32)(spreadSheet.WorkbookPart.WorkbookStylesPart.Stylesheet.CellFormats.Elements <CellFormat>().Count() - 1); workSheetPart.Worksheet.Save(); } }
/// <summary> /// Получить стиль заливку по ID стиля заливки /// </summary> /// <param name="fills">Таблица стилей заливки</param> /// <param name="fillIndex">Индекс стиля заливки</param> /// <returns>Объекь стиля заливки</returns> public static Fill Fill(this Fills fills, int fillIndex) { return(fills.Elements <Fill>().ElementAt(fillIndex)); }
private static void ResolveFillWithPattern(Fills fills, PatternValues patternValues) { if (fills.Elements<Fill>().Any(f => f.PatternFill.PatternType == patternValues && f.PatternFill.ForegroundColor == null && f.PatternFill.BackgroundColor == null )) return; var fill1 = new Fill(); var patternFill1 = new PatternFill {PatternType = patternValues}; fill1.AppendChild(patternFill1); fills.AppendChild(fill1); }