public static void WriteSellsJournal(int Rows, IEnumerable <Продажа_Поля> Ebumba) { string path = Environment.CurrentDirectory + "\\Отчет о продажах.docx"; List <Продажа_Поля> list = Ebumba.ToList(); using (DocX document = DocX.Create("Отчет о продажах.docx")) { document.InsertParagraph("Отчет о продажах").FontSize(15d); document.InsertParagraph($"Дата создания: \"{DateTime.Now}\""); #region WriteTable Xceed.Document.NET.Table table = document.AddTable(Rows, 4); for (int i = 0; i < list.Count + 1; i++) { if (i == 0) { table.Rows[i].Cells[0].Paragraphs[0].Append("Клиент").Bold().Alignment = Xceed.Document.NET.Alignment.center; table.Rows[i].Cells[1].Paragraphs[0].Append("Модель").Bold().Alignment = Xceed.Document.NET.Alignment.center; table.Rows[i].Cells[2].Paragraphs[0].Append("Дата приобретения").Bold().Alignment = Xceed.Document.NET.Alignment.center; table.Rows[i].Cells[3].Paragraphs[0].Append("Стоимость").Bold().Alignment = Xceed.Document.NET.Alignment.center; } else { table.InsertRow(); table.Rows[i].Cells[0].Paragraphs[0].Append(list[i - 1].ID_Клиента.ToString()); table.Rows[i].Cells[1].Paragraphs[0].Append(list[i - 1].ID_Модели.ToString()); table.Rows[i].Cells[2].Paragraphs[0].Append(list[i - 1].Дата_продажи_DataGridView.ToString()); table.Rows[i].Cells[3].Paragraphs[0].Append(list[i - 1].Сумма_продажи.ToString()); } } document.InsertParagraph().InsertTableAfterSelf(table); #endregion document.Save(); } System.Diagnostics.Process.Start(path); }
//############################################################################################################################################################################################## /// <summary> /// Convert the .csv files to documents /// </summary> private async void ConvertCsvToDoc() { // Check if template file exists if (!System.IO.File.Exists(TemplatePath)) { await _dialogCoordinator.ShowMessageAsync(this, "Fehler", "Das Template wurde nicht gefunden: " + Environment.NewLine + TemplatePath); return; } if (System.IO.Path.GetExtension(TemplatePath).ToLower() != ".docx") { await _dialogCoordinator.ShowMessageAsync(this, "Fehler", "Das Template hat das falsche Format (nur *.docx erlaubt): " + Environment.NewLine + TemplatePath); return; } if (!System.IO.File.Exists(MappingCsvFilePath)) { await _dialogCoordinator.ShowMessageAsync(this, "Fehler", "Die Mapping Datei wurde nicht gefunden: " + Environment.NewLine + TemplatePath); return; } int convertedFilesCnt = 0; // Loop over all .csv files foreach (string csvFilePath in CsvFilePaths) { // Check if .csv file exists if (!System.IO.File.Exists(csvFilePath)) { await _dialogCoordinator.ShowMessageAsync(this, "Fehler", "Die CSV Datei wurde nicht gefunden: " + Environment.NewLine + csvFilePath); continue; } CsvFile csv_file = new CsvFile(csvFilePath, DelimiterCharacter); // Accept all .csv file header names as placeholders CsvMappingFile MappingFile = new CsvMappingFile(MappingCsvFilePath, DelimiterCharacter); if (MappingFile.Mappings == null) { await _dialogCoordinator.ShowMessageAsync(this, "Fehler", "Es gibt Fehler in der Mapping Datei."); return; } MappingFile.AddMappingsForDataCsvHeader(csv_file); using (DocX templateDoc = DocX.Load(TemplatePath)) { Xceed.Document.NET.Table templateDocTable = templateDoc.Tables.FirstOrDefault(); if (templateDocTable == null) { await _dialogCoordinator.ShowMessageAsync(this, "Fehler", "Im Template wurde keine Tabelle gefunden."); return; } if (templateDocTable.RowCount <= 1) { await _dialogCoordinator.ShowMessageAsync(this, "Fehler", "Die Tabelle im Template muss 2 Zeilen haben."); return; } Xceed.Document.NET.Row rowPattern = templateDocTable.Rows[1]; // Get the row pattern of the second row (the first row contains the headers). // Add the general informations to the document. This are the rows above the real CSV table. foreach (KeyValuePair <string, string> generalInfoMapping in MappingFile.Mappings.Where(m => m.Value.StartsWith(CsvMappingFile.GENERAL_INFOS_MARKER_PART))) { string replacementText = ""; string generalInfoIndexStr = generalInfoMapping.Value.Replace(CsvMappingFile.GENERAL_INFOS_MARKER_PART, "").Replace("%", ""); if (string.IsNullOrEmpty(generalInfoIndexStr)) { replacementText = csv_file.AllCsvFileDescriptionRows; } else { int generalInfoIndex = -1; if (!int.TryParse(generalInfoIndexStr, out generalInfoIndex)) { await _dialogCoordinator.ShowMessageAsync(this, "Fehler", "Der Index (\"" + generalInfoIndexStr + "\") für die Beschreibungszeilen kann nicht geparst werden."); continue; } if (generalInfoIndex <= 0 || generalInfoIndex > csv_file.CsvFileDescriptionRows.Count) { await _dialogCoordinator.ShowMessageAsync(this, "Fehler", "Der Index (" + generalInfoIndex.ToString() + ") für die Beschreibungszeilen ist außerhalb des gültigen Bereiches (1.." + csv_file.CsvFileDescriptionRows.Count.ToString() + ")."); continue; } replacementText = csv_file.CsvFileDescriptionRows[generalInfoIndex - 1]; } templateDoc.ReplaceText(generalInfoMapping.Key, replacementText); } // Insert csv content to table foreach (CsvFileLine fileLine in csv_file.CsvFileLines) { Xceed.Document.NET.Row newRow = templateDocTable.InsertRow(rowPattern, templateDocTable.RowCount - 1); // Insert new row at the end of the table foreach (KeyValuePair <string, string> mappingPair in MappingFile.Mappings) { if (!mappingPair.Value.StartsWith(CsvMappingFile.GENERAL_INFOS_MARKER_PART)) { CsvFileLineElement lineElement = fileLine?.LineElements?.Where(element => element.CorrespondingHeader == mappingPair.Value)?.FirstOrDefault(); newRow.ReplaceText(mappingPair.Key, lineElement == null ? "" : lineElement.Value); } } } // Remove the pattern row. rowPattern.Remove(); templateDoc.SaveAs(System.IO.Path.ChangeExtension(csv_file.FilePath, null) + System.IO.Path.GetExtension(TemplatePath)); convertedFilesCnt++; } } await _dialogCoordinator.ShowMessageAsync(this, "Fertig", convertedFilesCnt.ToString() + " von " + CsvFilePaths.Count.ToString() + " CSV Datei(en) wurde(n) erfolgreich in Dokument(e) umgewandelt."); }