Правки по пакету word

This commit is contained in:
kotcheshir73 2022-12-19 12:17:45 +04:00
parent a17e888901
commit faa59fbd6e
11 changed files with 132 additions and 37 deletions

View File

@ -39,15 +39,7 @@ namespace ToolsOffice.Implements.WordOpenXML.Extensions
var pageMargin = new PageMargin();
if (model.PageMarginBottom.HasValue)
{
pageMargin.Bottom = model.PageMarginBottom.Value;
}
if (model.PageMarginFooter != null)
{
pageMargin.Footer = new UInt32Value(Convert.ToUInt32(model.PageMarginFooter.Value));
}
if (model.PageMarginGutter != null)
{
pageMargin.Gutter = new UInt32Value(Convert.ToUInt32(model.PageMarginGutter.Value));
pageMargin.Bottom = Convert.ToInt32(model.PageMarginBottom.Value);
}
if (model.PageMarginLeft != null)
{
@ -59,7 +51,7 @@ namespace ToolsOffice.Implements.WordOpenXML.Extensions
}
if (model.PageMarginTop.HasValue)
{
pageMargin.Top = model.PageMarginTop.Value;
pageMargin.Top = Convert.ToInt32(model.PageMarginTop.Value);
}
properties.AppendChild(pageMargin);

View File

@ -1,6 +1,7 @@
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Wordprocessing;
using System;
using ToolsModule.ManagmentExtension;
using ToolsOffice.Interfaces.Word.Models;
namespace ToolsOffice.Implements.WordOpenXML.Extensions
@ -20,7 +21,11 @@ namespace ToolsOffice.Implements.WordOpenXML.Extensions
}
var paragraph = new Paragraph();
paragraph.AddParagraphProperties(model);
if (model.WordTexts != null)
if (model.Text.IsNotEmpty())
{
paragraph.AddRun(model.Text);
}
else if (model.WordTexts != null)
{
foreach (var text in model.WordTexts)
{
@ -43,7 +48,11 @@ namespace ToolsOffice.Implements.WordOpenXML.Extensions
}
var paragraph = new Paragraph();
paragraph.AddParagraphProperties(model);
if (model.WordTexts != null)
if (model.Text.IsNotEmpty())
{
paragraph.AddRun(model.Text);
}
else if (model.WordTexts != null)
{
foreach (var text in model.WordTexts)
{

View File

@ -1,5 +1,6 @@
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Wordprocessing;
using ToolsModule.ManagmentExtension;
using ToolsOffice.Interfaces.Word.Models;
namespace ToolsOffice.Implements.WordOpenXML.Extensions
@ -34,6 +35,24 @@ namespace ToolsOffice.Implements.WordOpenXML.Extensions
paragraph.AppendChild(docRun);
}
/// <summary>
/// Добавление текста в абзац
/// </summary>
/// <param name="paragraph"></param>
/// <param name="text"></param>
public static void AddRun(this Paragraph paragraph, string text)
{
if (text.IsEmpty())
{
return;
}
var docRun = new Run();
{
docRun.AppendChild(new Text { Text = text, Space = SpaceProcessingModeValues.Preserve });
}
paragraph.AppendChild(docRun);
}
/// <summary>
/// Добавление свойств текста
/// </summary>

View File

@ -37,6 +37,10 @@ namespace ToolsOffice.Implements.WordOpenXML.Extensions
/// <param name="model"></param>
private static void CreateRows(this ModelWordTable model)
{
if (model == null || model.Data == null || model.Data.Count == 0)
{
return;
}
model.Rows = new Queue<ModelWordTableRow>();
var headerRowsCount = model.Headers.Select(x => x.RowIndex).Distinct().Count();
@ -48,20 +52,20 @@ namespace ToolsOffice.Implements.WordOpenXML.Extensions
{
header.Height = model.RowsHeight[rowIndex];
}
for (int i = 0; i < model.Data.GetLength(1); ++i)
for (int i = 0; i < model.Data[0].Length; ++i)
{
header.Cells.Enqueue(CreateHeaderCell(model, rowIndex, i));
}
model.Rows.Enqueue(header);
}
for (int i = 0; i < model.Data.Length; ++i)
for (int i = 0; i < model.Data.Count; ++i)
{
var row = new ModelWordTableRow();
if (model.RowsHeight.ContainsKey(i + headerRowsCount))
{
row.Height = model.RowsHeight[i + headerRowsCount];
}
for (int j = 0; j < model.Data.GetLength(1); ++j)
for (int j = 0; j < model.Data[0].Length; ++j)
{
row.Cells.Enqueue(CreateCell(model, i, j));
}
@ -241,7 +245,7 @@ namespace ToolsOffice.Implements.WordOpenXML.Extensions
};
paragraph.WordTexts.Enqueue(new ModelWordText
{
Text = model.Data[rowIndex, columnIndex]
Text = model.Data[rowIndex][columnIndex]
});
cell.Texts.Enqueue(paragraph);
}

View File

@ -1,10 +1,23 @@
using System.IO;
using ToolsOffice.Interfaces.Word.Models;
using ToolsOffice.Interfaces.Word.Models.Cases;
namespace ToolsOffice.Interfaces.Word
{
public abstract class BuilderWordDocument
{
public Stream CreateDocumentWithTable(ModelWordDocumentWithHeaderAndTable model)
{
if (model == null || model.Document == null || model.Header == null || model.Table == null)
{
return null;
}
CreateDocument(model.Document);
CreateParagraph(model.Header);
CreateTable(model.Table);
return SaveDocument(model.Document);
}
/// <summary>
/// Создание документа
/// </summary>

View File

@ -0,0 +1,14 @@
namespace ToolsOffice.Interfaces.Word.Models.Cases
{
/// <summary>
/// Модель для создания простого документа с шапкой и таблицей
/// </summary>
public class ModelWordDocumentWithHeaderAndTable
{
public ModelWordDocument Document { get; set; }
public ModelWordParagraph Header { get; set; }
public ModelWordTable Table { get; set; }
}
}

View File

@ -5,32 +5,27 @@
/// </summary>
public class ModelWordDocument
{
/// <summary>
/// Высота страницы
/// </summary>
public int? PageSizeHeight { get; set; }
/// <summary>
/// Ширина страницы
/// </summary>
public int? PageSizeWidth { get; set; }
/// <summary>
/// Ориентация страницы
/// </summary>
public TypeWordPageOrientation? WordPageOrientation { get; set; }
/// <summary>
/// Высота страницы
/// </summary>
public double? PageSizeHeight { get; set; }
public int? PageMarginFooter { get; set; }
/// <summary>
/// Ширина страницы
/// </summary>
public double? PageSizeWidth { get; set; }
public int? PageMarginGutter { get; set; }
public double? PageMarginBottom { get; set; }
public int? PageMarginBottom { get; set; }
public double? PageMarginTop { get; set; }
public int? PageMarginTop { get; set; }
public double? PageMarginLeft { get; set; }
public int? PageMarginLeft { get; set; }
public int? PageMarginRight { get; set; }
public double? PageMarginRight { get; set; }
}
}

View File

@ -3,5 +3,5 @@
/// <summary>
/// Модель данных при создании элемента документа (абзац, таблица)
/// </summary>
public interface IWordDocumentPartModel { }
public class ModelWordDocumentPart { }
}

View File

@ -5,8 +5,13 @@ namespace ToolsOffice.Interfaces.Word.Models
/// <summary>
/// Модель описания абзаца документа
/// </summary>
public class ModelWordParagraph : IWordDocumentPartModel
public class ModelWordParagraph : ModelWordDocumentPart
{
/// <summary>
/// Текст одним стилем форматирования
/// </summary>
public string Text { get; set; }
/// <summary>
/// Набор текстов
/// </summary>

View File

@ -5,7 +5,7 @@ namespace ToolsOffice.Interfaces.Word.Models
/// <summary>
/// Модель описания таблицы документа
/// </summary>
public class ModelWordTable : IWordDocumentPartModel
public class ModelWordTable : ModelWordDocumentPart
{
/// <summary>
/// Информация по ширине колонок (номер колонки, ширина колонки), отсчет с 0
@ -30,7 +30,7 @@ namespace ToolsOffice.Interfaces.Word.Models
/// <summary>
/// Данные
/// </summary>
public string[,] Data { get; set; }
public List<string[]> Data { get; set; }
/// <summary>
/// Иной вывод первой строки

View File

@ -0,0 +1,44 @@
using System.Collections.Generic;
using System.Linq;
namespace ToolsOffice.Interfaces.Word
{
public class PageSizes
{
private static readonly
Dictionary<string, (double Height, double Width, double MarginBottom, double MarginTop, double MarginLeft, double MarginRigth)> _sizes = new()
{
{ "А4", (27.94, 21.59, 2, 2, 3, 1.5) }
};
public static List<string> Sizes => _sizes.Keys.ToList();
private readonly bool _hasKey;
private readonly string _key;
public PageSizes(string key)
{
_hasKey = _sizes.ContainsKey(key);
_key = key;
}
/// <summary>
/// Высота страницы
/// </summary>
public double? PageSizeHeight => _hasKey ? _sizes[_key].Height : null;
/// <summary>
/// Ширина страницы
/// </summary>
public double? PageSizeWidth => _hasKey ? _sizes[_key].Width : null;
public double? PageMarginBottom => _hasKey ? _sizes[_key].MarginBottom : null;
public double? PageMarginTop => _hasKey ? _sizes[_key].MarginTop : null;
public double? PageMarginLeft => _hasKey ? _sizes[_key].MarginLeft : null;
public double? PageMarginRight => _hasKey ? _sizes[_key].MarginRigth : null;
}
}