diff --git a/ProjectGSM/Documents/ExcelBuilder.cs b/ProjectGSM/Documents/ExcelBuilder.cs index db74429..bd328d1 100644 --- a/ProjectGSM/Documents/ExcelBuilder.cs +++ b/ProjectGSM/Documents/ExcelBuilder.cs @@ -4,12 +4,16 @@ using DocumentFormat.OpenXml.Spreadsheet; namespace ProjectGSM.Documents; -public class ExcelBuilder +internal class ExcelBuilder { private readonly string _filePath; + private readonly SheetData _sheetData; + private readonly MergeCells _mergeCells; + private readonly Columns _columns; + private uint _rowIndex = 0; public ExcelBuilder(string filePath) @@ -33,29 +37,25 @@ public class ExcelBuilder public ExcelBuilder AddHeader(string header, int startIndex, int count) { - CreateCell(startIndex, _rowIndex, header, - StyleIndex.BoldTextWithoutBorder); + CreateCell(startIndex, _rowIndex, header, StyleIndex.BoldTextWithoutBorder); for (int i = startIndex + 1; i < startIndex + count; ++i) { - CreateCell(i, _rowIndex, "", - StyleIndex.BoldTextWithoutBorder); + CreateCell(i, _rowIndex, "", StyleIndex.SimpleTextWithoutBorder); } _mergeCells.Append(new MergeCell() { Reference = - new - StringValue( - $"{GetExcelColumnName(startIndex)}{_rowIndex}:{GetExcelColumnName(startIndex + count - 1)}{_rowIndex}") + new StringValue($"{GetExcelColumnName(startIndex)}{_rowIndex}:{GetExcelColumnName(startIndex + count - 1)}{_rowIndex}") }); + _rowIndex++; return this; } public ExcelBuilder AddParagraph(string text, int columnIndex) { - CreateCell(columnIndex, _rowIndex++, text, - StyleIndex.SimpleTextWithoutBorder); + CreateCell(columnIndex, _rowIndex++, text, StyleIndex.SimpleTextWithoutBorder); return this; } @@ -73,7 +73,7 @@ public class ExcelBuilder if (data.Any(x => x.Length != columnsWidths.Length)) { - throw new InvalidOperationException("widths.Length != data.Length"); + throw new InvalidOperationException("columnsWidths.Length != data.Length"); } uint counter = 1; @@ -85,10 +85,10 @@ public class ExcelBuilder Width = x * coef, CustomWidth = true })); + for (var j = 0; j < data.First().Length; ++j) { - CreateCell(j, _rowIndex, data.First()[j], - StyleIndex.SimpleTextWithoutBorder); + CreateCell(j, _rowIndex, data.First()[j], StyleIndex.BoldTextWithBorder); } _rowIndex++; @@ -96,8 +96,7 @@ public class ExcelBuilder { for (var j = 0; j < data[i].Length; ++j) { - CreateCell(j, _rowIndex, data[i][j], - StyleIndex.SimpleTextWithoutBorder); + CreateCell(j, _rowIndex, data[i][j], StyleIndex.SimpleTextWithBorder); } _rowIndex++; @@ -105,8 +104,7 @@ public class ExcelBuilder for (var j = 0; j < data.Last().Length; ++j) { - CreateCell(j, _rowIndex, data.Last()[j], - StyleIndex.SimpleTextWithoutBorder); + CreateCell(j, _rowIndex, data.Last()[j], StyleIndex.BoldTextWithBorder); } _rowIndex++; @@ -115,8 +113,7 @@ public class ExcelBuilder public void Build() { - using var spreadsheetDocument = SpreadsheetDocument.Create(_filePath, - SpreadsheetDocumentType.Workbook); + using var spreadsheetDocument = SpreadsheetDocument.Create(_filePath, SpreadsheetDocumentType.Workbook); var workbookpart = spreadsheetDocument.AddWorkbookPart(); GenerateStyle(workbookpart); workbookpart.Workbook = new Workbook(); @@ -128,83 +125,83 @@ public class ExcelBuilder } worksheetPart.Worksheet.Append(_sheetData); - var sheets = - spreadsheetDocument.WorkbookPart!.Workbook.AppendChild(new Sheets()); + var sheets = spreadsheetDocument.WorkbookPart!.Workbook.AppendChild(new Sheets()); var sheet = new Sheet() { - Id = - spreadsheetDocument.WorkbookPart.GetIdOfPart(worksheetPart), + Id = spreadsheetDocument.WorkbookPart.GetIdOfPart(worksheetPart), SheetId = 1, Name = "Лист 1" }; + sheets.Append(sheet); if (_mergeCells.HasChildren) { - worksheetPart.Worksheet.InsertAfter(_mergeCells, - worksheetPart.Worksheet.Elements().First()); + worksheetPart.Worksheet.InsertAfter(_mergeCells, worksheetPart.Worksheet.Elements().First()); } } private static void GenerateStyle(WorkbookPart workbookPart) { - var workbookStylesPart = - workbookPart.AddNewPart(); + var workbookStylesPart = workbookPart.AddNewPart(); workbookStylesPart.Stylesheet = new Stylesheet(); - var fonts = new Fonts() - { - Count = 2, KnownFonts = - BooleanValue.FromBoolean(true) - }; + + var fonts = new Fonts() { Count = 2, KnownFonts = BooleanValue.FromBoolean(true) }; fonts.Append(new DocumentFormat.OpenXml.Spreadsheet.Font { FontSize = new FontSize() { Val = 11 }, FontName = new FontName() { Val = "Calibri" }, FontFamilyNumbering = new FontFamilyNumbering() { Val = 2 }, - FontScheme = new FontScheme() - { - Val = new - EnumValue(FontSchemeValues.Minor) - }, - Bold = new Bold() + FontScheme = new FontScheme() { Val = new EnumValue(FontSchemeValues.Minor) } + }); + + fonts.Append(new DocumentFormat.OpenXml.Spreadsheet.Font + { + FontSize = new FontSize() { Val = 11 }, + FontName = new FontName() { Val = "Calibri" }, + FontFamilyNumbering = new FontFamilyNumbering() { Val = 2 }, + FontScheme = new FontScheme() { Val = new EnumValue(FontSchemeValues.Minor) }, + Bold = new Bold() { Val = true } }); workbookStylesPart.Stylesheet.Append(fonts); + // Default Fill var fills = new Fills() { Count = 1 }; fills.Append(new Fill { - PatternFill = new PatternFill() - { - PatternType = new - EnumValue(PatternValues.None) - } + PatternFill = new PatternFill() { PatternType = new EnumValue(PatternValues.None) } }); workbookStylesPart.Stylesheet.Append(fills); + // Default Border var borders = new Borders() { Count = 2 }; - // Add a border with thin lines borders.Append(new Border { - LeftBorder = new LeftBorder { Style = BorderStyleValues.Thin }, - RightBorder = new RightBorder { Style = BorderStyleValues.Thin }, - TopBorder = new TopBorder { Style = BorderStyleValues.Thin }, - BottomBorder = new BottomBorder { Style = BorderStyleValues.Thin }, + LeftBorder = new LeftBorder(), + RightBorder = new RightBorder(), + TopBorder = new TopBorder(), + BottomBorder = new BottomBorder(), DiagonalBorder = new DiagonalBorder() }); + borders.Append(new Border + { + LeftBorder = new LeftBorder() { Style = BorderStyleValues.Thin }, + RightBorder = new RightBorder() { Style = BorderStyleValues.Thin }, + TopBorder = new TopBorder() { Style = BorderStyleValues.Thin }, + BottomBorder = new BottomBorder() { Style = BorderStyleValues.Thin }, + DiagonalBorder = new DiagonalBorder() + }); workbookStylesPart.Stylesheet.Append(borders); + // Default cell format and a date cell format var cellFormats = new CellFormats() { Count = 4 }; - - // Add additional cell formats - - // Add additional cell formats cellFormats.Append(new CellFormat { NumberFormatId = 0, FormatId = 0, - FontId = 1, // Use the bold font - BorderId = 1, // Use the thin border + FontId = 0, + BorderId = 0, FillId = 0, Alignment = new Alignment() { @@ -214,24 +211,66 @@ public class ExcelBuilder } }); + cellFormats.Append(new CellFormat + { + NumberFormatId = 0, + FormatId = 0, + FontId = 0, + BorderId = 1, + FillId = 0, + Alignment = new Alignment() + { + Horizontal = HorizontalAlignmentValues.Right, + Vertical = VerticalAlignmentValues.Center, + WrapText = true + } + }); + + cellFormats.Append(new CellFormat + { + NumberFormatId = 0, + FormatId = 0, + FontId = 1, + BorderId = 0, + FillId = 0, + Alignment = new Alignment() + { + Horizontal = HorizontalAlignmentValues.Center, + Vertical = VerticalAlignmentValues.Center, + WrapText = true + } + }); + + cellFormats.Append(new CellFormat + { + NumberFormatId = 0, + FormatId = 0, + FontId = 1, + BorderId = 1, + FillId = 0, + Alignment = new Alignment() + { + Horizontal = HorizontalAlignmentValues.Center, + Vertical = VerticalAlignmentValues.Center, + WrapText = true + } + }); workbookStylesPart.Stylesheet.Append(cellFormats); } private enum StyleIndex { SimpleTextWithoutBorder = 0, - BoldTextWithoutBorder = 1, - ItalicTextWithoutBorder = 2, - // Add more styles as needed + SimpleTextWithBorder = 1, + BoldTextWithoutBorder = 2, + BoldTextWithBorder = 3, } - private void CreateCell(int columnIndex, uint rowIndex, string text, - StyleIndex styleIndex) + private void CreateCell(int columnIndex, uint rowIndex, string text, StyleIndex styleIndex) { var columnName = GetExcelColumnName(columnIndex); var cellReference = columnName + rowIndex; - var row = _sheetData.Elements().FirstOrDefault(r => r.RowIndex! - == rowIndex); + var row = _sheetData.Elements().FirstOrDefault(r => r.RowIndex! == rowIndex); if (row == null) { row = new Row() { RowIndex = rowIndex }; @@ -239,18 +278,15 @@ public class ExcelBuilder } var newCell = row.Elements() - .FirstOrDefault(c => c.CellReference != null && - c.CellReference.Value == columnName + rowIndex); + .FirstOrDefault(c => c.CellReference != null && c.CellReference.Value == columnName + rowIndex); if (newCell == null) { Cell? refCell = null; foreach (Cell cell in row.Elements()) { - if (cell.CellReference?.Value != null && - cell.CellReference.Value.Length == cellReference.Length) + if (cell.CellReference?.Value != null && cell.CellReference.Value.Length == cellReference.Length) { - if (String.Compare(cell.CellReference.Value, - cellReference, StringComparison.OrdinalIgnoreCase) > 0) + if (string.Compare(cell.CellReference.Value, cellReference, true) > 0) { refCell = cell; break; @@ -273,11 +309,11 @@ public class ExcelBuilder int dividend = columnNumber; string columnName = string.Empty; int modulo; + while (dividend > 0) { modulo = (dividend - 1) % 26; - columnName = Convert.ToChar(65 + modulo).ToString() + - columnName; + columnName = Convert.ToChar(65 + modulo).ToString() + columnName; dividend = (dividend - modulo) / 26; }