From 900c5f1fb499f2a5ce462f188d89eb34d9e9c38b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=90=D0=BB=D0=B5=D0=B9?= =?UTF-8?q?=D0=BA=D0=B8=D0=BD?= Date: Fri, 19 May 2023 22:34:15 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=B8=D1=88=D1=83=20=D0=BE=D1=82=D1=87?= =?UTF-8?q?=D0=B5=D1=82=D1=8B=20=E2=84=961?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CaseAccountingBusinessLogic.csproj | 2 +- .../AbstractSaveToExcelCustomer.cs | 12 - .../AbstractSaveToExcelProvider.cs | 12 - .../AbstractSaveToPdfCustomer.cs | 12 - .../AbstractSaveToPdfProvider.cs | 12 - .../AbstractSaveToWordCustomer.cs | 12 - .../AbstractSaveToWordProvider.cs | 12 - .../OfficePackage/ExcelBuilderCustomer.cs | 362 ++++++++++++++++++ .../HelperModels/ExcelCellData.cs | 21 + .../OfficePackage/HelperModels/PdfData.cs | 19 + .../HelperModels/WordTableData.cs | 14 + 11 files changed, 417 insertions(+), 73 deletions(-) delete mode 100644 CaseAccounting/CaseAccountingBusinessLogics/OfficePackage/AbstractSaveToExcelCustomer.cs delete mode 100644 CaseAccounting/CaseAccountingBusinessLogics/OfficePackage/AbstractSaveToExcelProvider.cs delete mode 100644 CaseAccounting/CaseAccountingBusinessLogics/OfficePackage/AbstractSaveToPdfCustomer.cs delete mode 100644 CaseAccounting/CaseAccountingBusinessLogics/OfficePackage/AbstractSaveToPdfProvider.cs delete mode 100644 CaseAccounting/CaseAccountingBusinessLogics/OfficePackage/AbstractSaveToWordCustomer.cs delete mode 100644 CaseAccounting/CaseAccountingBusinessLogics/OfficePackage/AbstractSaveToWordProvider.cs create mode 100644 CaseAccounting/CaseAccountingBusinessLogics/OfficePackage/ExcelBuilderCustomer.cs create mode 100644 CaseAccounting/CaseAccountingBusinessLogics/OfficePackage/HelperModels/ExcelCellData.cs create mode 100644 CaseAccounting/CaseAccountingBusinessLogics/OfficePackage/HelperModels/PdfData.cs create mode 100644 CaseAccounting/CaseAccountingBusinessLogics/OfficePackage/HelperModels/WordTableData.cs diff --git a/CaseAccounting/CaseAccountingBusinessLogics/CaseAccountingBusinessLogic.csproj b/CaseAccounting/CaseAccountingBusinessLogics/CaseAccountingBusinessLogic.csproj index d0f81fd..01840d1 100644 --- a/CaseAccounting/CaseAccountingBusinessLogics/CaseAccountingBusinessLogic.csproj +++ b/CaseAccounting/CaseAccountingBusinessLogics/CaseAccountingBusinessLogic.csproj @@ -7,6 +7,7 @@ + @@ -17,7 +18,6 @@ - diff --git a/CaseAccounting/CaseAccountingBusinessLogics/OfficePackage/AbstractSaveToExcelCustomer.cs b/CaseAccounting/CaseAccountingBusinessLogics/OfficePackage/AbstractSaveToExcelCustomer.cs deleted file mode 100644 index ad98b9c..0000000 --- a/CaseAccounting/CaseAccountingBusinessLogics/OfficePackage/AbstractSaveToExcelCustomer.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace CaseAccountingBusinessLogic.OfficePackage -{ - public class AbstractSaveToExcelCustomer - { - } -} diff --git a/CaseAccounting/CaseAccountingBusinessLogics/OfficePackage/AbstractSaveToExcelProvider.cs b/CaseAccounting/CaseAccountingBusinessLogics/OfficePackage/AbstractSaveToExcelProvider.cs deleted file mode 100644 index 681ddf1..0000000 --- a/CaseAccounting/CaseAccountingBusinessLogics/OfficePackage/AbstractSaveToExcelProvider.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace CaseAccountingBusinessLogic.OfficePackage -{ - public class AbstractSaveToExcelProvider - { - } -} diff --git a/CaseAccounting/CaseAccountingBusinessLogics/OfficePackage/AbstractSaveToPdfCustomer.cs b/CaseAccounting/CaseAccountingBusinessLogics/OfficePackage/AbstractSaveToPdfCustomer.cs deleted file mode 100644 index e5619ee..0000000 --- a/CaseAccounting/CaseAccountingBusinessLogics/OfficePackage/AbstractSaveToPdfCustomer.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace CaseAccountingBusinessLogic.OfficePackage -{ - public class AbstractSaveToPdfCustomer - { - } -} diff --git a/CaseAccounting/CaseAccountingBusinessLogics/OfficePackage/AbstractSaveToPdfProvider.cs b/CaseAccounting/CaseAccountingBusinessLogics/OfficePackage/AbstractSaveToPdfProvider.cs deleted file mode 100644 index d8bc181..0000000 --- a/CaseAccounting/CaseAccountingBusinessLogics/OfficePackage/AbstractSaveToPdfProvider.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace CaseAccountingBusinessLogic.OfficePackage -{ - public class AbstractSaveToPdfProvider - { - } -} diff --git a/CaseAccounting/CaseAccountingBusinessLogics/OfficePackage/AbstractSaveToWordCustomer.cs b/CaseAccounting/CaseAccountingBusinessLogics/OfficePackage/AbstractSaveToWordCustomer.cs deleted file mode 100644 index f9bc408..0000000 --- a/CaseAccounting/CaseAccountingBusinessLogics/OfficePackage/AbstractSaveToWordCustomer.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace CaseAccountingBusinessLogic.OfficePackage -{ - public class AbstractSaveToWordCustomer - { - } -} diff --git a/CaseAccounting/CaseAccountingBusinessLogics/OfficePackage/AbstractSaveToWordProvider.cs b/CaseAccounting/CaseAccountingBusinessLogics/OfficePackage/AbstractSaveToWordProvider.cs deleted file mode 100644 index ff4597c..0000000 --- a/CaseAccounting/CaseAccountingBusinessLogics/OfficePackage/AbstractSaveToWordProvider.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace CaseAccountingBusinessLogic.OfficePackage -{ - public class AbstractSaveToWordProvider - { - } -} diff --git a/CaseAccounting/CaseAccountingBusinessLogics/OfficePackage/ExcelBuilderCustomer.cs b/CaseAccounting/CaseAccountingBusinessLogics/OfficePackage/ExcelBuilderCustomer.cs new file mode 100644 index 0000000..e29f4a5 --- /dev/null +++ b/CaseAccounting/CaseAccountingBusinessLogics/OfficePackage/ExcelBuilderCustomer.cs @@ -0,0 +1,362 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using CaseAccountingBusinessLogic.OfficePackage.HelperModels; +using CaseAccountingContracts.ViewModels; +using ComputersShopBusinessLogic.OfficePackage.HelperModels; +using DocumentFormat.OpenXml; +using DocumentFormat.OpenXml.Packaging; +using DocumentFormat.OpenXml.Spreadsheet; + +namespace CaseAccountingBusinessLogic.OfficePackage +{ + public class ExcelBuilderCustomer + { + private readonly string tempFileName = "temp.xlsx"; + private SpreadsheetDocument? spreadsheetDocument; + private SharedStringTablePart? shareStringPart; + private Worksheet? worksheet; + + private void CreateStyles(WorkbookPart workbookpart) + { + var sp = workbookpart.AddNewPart(); + sp.Stylesheet = new Stylesheet(); + + var fonts = new Fonts() { Count = 2U, KnownFonts = true }; + + var fontUsual = new Font(); + fontUsual.Append(new FontSize() { Val = 12D }); + fonts.Append(fontUsual); + + var fontTitle = new Font(); + fontTitle.Append(new Bold()); + fontTitle.Append(new FontSize() { Val = 12D }); + fonts.Append(fontTitle); + + var fills = new Fills() { Count = 3U }; + + var fill1 = new Fill(); + fill1.Append(new PatternFill() + { + PatternType = PatternValues.None + }); + + var fill2 = new Fill(); + fill2.Append(new PatternFill() + { + PatternType = PatternValues.Gray125 + }); + + var fill3 = new Fill(); + fill3.Append(new PatternFill() + { + PatternType = PatternValues.Solid, + ForegroundColor = new() + { + Rgb = "e0e8ff" + } + }); + + var fill4 = new Fill(); + fill1.Append(new PatternFill() + { + PatternType = PatternValues.None + }); + + fills.Append(fill1); + fills.Append(fill2); + fills.Append(fill3); + fills.Append(fill4); + + var borders = new Borders() { Count = 2U }; + + var borderNoBorder = new Border(); + borderNoBorder.Append(new LeftBorder()); + borderNoBorder.Append(new RightBorder()); + borderNoBorder.Append(new TopBorder()); + borderNoBorder.Append(new BottomBorder()); + borderNoBorder.Append(new DiagonalBorder()); + + var borderThin = new Border(); + + var leftBorder = new LeftBorder() { Style = BorderStyleValues.Thin }; + leftBorder.Append(new DocumentFormat.OpenXml.Office2010.Excel.Color() { Indexed = 64U }); + var rightBorder = new RightBorder() { Style = BorderStyleValues.Thin }; + rightBorder.Append(new DocumentFormat.OpenXml.Office2010.Excel.Color() { Indexed = 64U }); + var topBorder = new TopBorder() { Style = BorderStyleValues.Thin }; + topBorder.Append(new DocumentFormat.OpenXml.Office2010.Excel.Color() { Indexed = 64U }); + var bottomBorder = new BottomBorder() { Style = BorderStyleValues.Thin }; + bottomBorder.Append(new DocumentFormat.OpenXml.Office2010.Excel.Color() { Indexed = 64U }); + + borderThin.Append(leftBorder); + borderThin.Append(rightBorder); + borderThin.Append(topBorder); + borderThin.Append(bottomBorder); + borderThin.Append(new DiagonalBorder()); + + borders.Append(borderNoBorder); + borders.Append(borderThin); + + CellFormats cellFormats = new() { Count = 4U }; + CellFormat cellFormatEmpty = new() + { + FontId = 0U, + FillId = 0U, + BorderId = 1U, + ApplyFont = true + }; + CellFormat cellFormatDefault = new() + { + FontId = 0U, + FillId = 3U, + BorderId = 1U, + ApplyFont = true + }; + CellFormat cellFormatTitle = new() + { + FontId = 1U, + FillId = 2U, + BorderId = 1U, + ApplyFont = true, + ApplyBorder = true, + Alignment = new Alignment() + { + Vertical = VerticalAlignmentValues.Center, + Horizontal = HorizontalAlignmentValues.Center, + WrapText = true + } + }; + + cellFormats.Append(cellFormatEmpty); + cellFormats.Append(cellFormatDefault); + cellFormats.Append(cellFormatTitle); + + sp.Stylesheet.Append(fonts); + sp.Stylesheet.Append(fills); + sp.Stylesheet.Append(borders); + sp.Stylesheet.Append(cellFormats); + } + + public void CreateDocument() + { + spreadsheetDocument = SpreadsheetDocument.Create(tempFileName, + SpreadsheetDocumentType.Workbook); + WorkbookPart workbookpart = spreadsheetDocument.AddWorkbookPart(); + workbookpart.Workbook = new Workbook(); + + CreateStyles(workbookpart); + + shareStringPart = spreadsheetDocument.WorkbookPart! + .GetPartsOfType() + .Any() + ? spreadsheetDocument.WorkbookPart + .GetPartsOfType() + .First() + : spreadsheetDocument.WorkbookPart + .AddNewPart(); + + if (shareStringPart.SharedStringTable == null) + { + shareStringPart.SharedStringTable = new SharedStringTable(); + } + + WorksheetPart worksheetPart = workbookpart.AddNewPart(); + worksheetPart.Worksheet = new Worksheet(new SheetData()); + + Sheets sheets = spreadsheetDocument.WorkbookPart.Workbook.AppendChild(new Sheets()); + Sheet sheet = new() + { + Id = spreadsheetDocument.WorkbookPart.GetIdOfPart(worksheetPart), + SheetId = 1, + Name = "Лист" + }; + sheets.Append(sheet); + + worksheet = worksheetPart.Worksheet; + } + + public void InsertCellInWorksheet(ExcelCellData cellData) + { + if (worksheet == null || shareStringPart == null) + { + return; + } + var sheetData = worksheet.GetFirstChild(); + if (sheetData == null) + { + return; + } + + Row? row = sheetData.Elements() + .Where(r => r.RowIndex! == cellData.RowIndex) + .FirstOrDefault(); + if (row == null) + { + row = new Row() { RowIndex = cellData.RowIndex }; + sheetData.Append(row); + } + + Cell? cell = row.Elements() + .Where(c => c.CellReference!.Value == cellData.CellReference) + .FirstOrDefault(); + if (cell == null) + { + Cell? refCell = null; + foreach (Cell rowCell in row.Elements()) + { + if (string.Compare(rowCell.CellReference!.Value, cellData.CellReference, true) > 0) + { + refCell = rowCell; + break; + } + } + var newCell = new Cell() + { + CellReference = cellData.CellReference + }; + row.InsertBefore(newCell, refCell); + cell = newCell; + } + + shareStringPart.SharedStringTable.AppendChild(new SharedStringItem(new Text(cellData.Text))); + shareStringPart.SharedStringTable.Save(); + cell.CellValue = new CellValue((shareStringPart.SharedStringTable.Elements().Count() - 1).ToString()); + cell.DataType = new EnumValue(CellValues.SharedString); + cell.StyleIndex = cellData.StyleIndex; + } + + private void MergeCells(ExcelMergeParameters excelParams) + { + if (worksheet == null) + { + return; + } + MergeCells mergeCells; + if (worksheet.Elements().Any()) + { + mergeCells = worksheet.Elements().First(); + } + else + { + mergeCells = new MergeCells(); + if (worksheet.Elements().Any()) + { + worksheet.InsertAfter(mergeCells, worksheet.Elements().First()); + } + else + { + worksheet.InsertAfter(mergeCells, worksheet.Elements().First()); + } + } + var mergeCell = new MergeCell() + { + Reference = new StringValue(excelParams.Merge) + }; + mergeCells.Append(mergeCell); + } + + private void Save() + { + if (spreadsheetDocument == null) + { + return; + } + spreadsheetDocument.WorkbookPart!.Workbook.Save(); + spreadsheetDocument.Dispose(); + } + + public byte[] GetFile() + { + Save(); + byte[] file = File.ReadAllBytes(tempFileName); + File.Delete(tempFileName); + return file; + } + + public void CreateTitle(string text) + { + MergeCells(new ExcelMergeParameters + { + CellFromName = "A1", + CellToName = "B1" + }); + InsertCellInWorksheet(new ExcelCellData + { + ColumnName = "A", + RowIndex = 1, + Text = text, + StyleIndex = 0 + }); + } + + public void CreateLawyersHearingsTable(List data) + { + if (worksheet == null || shareStringPart == null) + { + return; + } + + Columns columns = new(); + Column columnA = new() { Min = 1, Max = 1, Width = 30, CustomWidth = true }; + columns.Append(columnA); + Column columnB = new() { Min = 2, Max = 2, Width = 30, CustomWidth = true }; + columns.Append(columnB); + worksheet.InsertAt(columns, 0); + + InsertCellInWorksheet(new ExcelCellData + { + ColumnName = "A", + RowIndex = 2, + Text = "Юрист", + StyleIndex = 2 + }); + InsertCellInWorksheet(new ExcelCellData + { + ColumnName = "B", + RowIndex = 2, + Text = "Слушание", + StyleIndex = 2 + }); + + uint currentRow = 3; + foreach (ReportLawyerHearingViewModel lawyer in data) + { + InsertCellInWorksheet(new ExcelCellData + { + ColumnName = "A", + RowIndex = currentRow, + Text = lawyer.Lawyer, + StyleIndex = 1 + }); + InsertCellInWorksheet(new ExcelCellData + { + ColumnName = "B", + RowIndex = currentRow, + Text = "", + StyleIndex = 1 + }); + currentRow++; + foreach (string discipline in student.Disciplines) + { + InsertCellInWorksheet(new ExcelCellData + { + ColumnName = "A", + RowIndex = currentRow, + Text = "", + StyleIndex = 1 + }); + InsertCellInWorksheet(new ExcelCellData + { + ColumnName = "B", + RowIndex = currentRow, + Text = discipline, + StyleIndex = 1 + }); + currentRow++; + } + } + } + } +} diff --git a/CaseAccounting/CaseAccountingBusinessLogics/OfficePackage/HelperModels/ExcelCellData.cs b/CaseAccounting/CaseAccountingBusinessLogics/OfficePackage/HelperModels/ExcelCellData.cs new file mode 100644 index 0000000..3b36825 --- /dev/null +++ b/CaseAccounting/CaseAccountingBusinessLogics/OfficePackage/HelperModels/ExcelCellData.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CaseAccountingBusinessLogic.OfficePackage.HelperModels +{ + public class ExcelCellData + { + public string ColumnName { get; set; } = string.Empty; + + public uint RowIndex { get; set; } + + public string Text { get; set; } = string.Empty; + + public string CellReference => $"{ColumnName}{RowIndex}"; + + public uint StyleIndex { get; set; } = 0; + } +} diff --git a/CaseAccounting/CaseAccountingBusinessLogics/OfficePackage/HelperModels/PdfData.cs b/CaseAccounting/CaseAccountingBusinessLogics/OfficePackage/HelperModels/PdfData.cs new file mode 100644 index 0000000..c02818c --- /dev/null +++ b/CaseAccounting/CaseAccountingBusinessLogics/OfficePackage/HelperModels/PdfData.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CaseAccountingBusinessLogic.OfficePackage.HelperModels +{ + public class PdfData + { + public string Title { get; set; } = string.Empty; + + public DateTime DateFrom { get; set; } + + public DateTime DateTo { get; set; } + + public List Records { get; set; } = new(); + } +} diff --git a/CaseAccounting/CaseAccountingBusinessLogics/OfficePackage/HelperModels/WordTableData.cs b/CaseAccounting/CaseAccountingBusinessLogics/OfficePackage/HelperModels/WordTableData.cs new file mode 100644 index 0000000..2aec825 --- /dev/null +++ b/CaseAccounting/CaseAccountingBusinessLogics/OfficePackage/HelperModels/WordTableData.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CaseAccountingBusinessLogic.OfficePackage.HelperModels +{ + public class WordTableData + { + public List<(string, int)> Columns { get; set; } = new(); + public List> Rows { get; set; } = new(); + } +}