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