Правки по пакету 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(); var pageMargin = new PageMargin();
if (model.PageMarginBottom.HasValue) if (model.PageMarginBottom.HasValue)
{ {
pageMargin.Bottom = model.PageMarginBottom.Value; pageMargin.Bottom = Convert.ToInt32(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));
} }
if (model.PageMarginLeft != null) if (model.PageMarginLeft != null)
{ {
@ -59,7 +51,7 @@ namespace ToolsOffice.Implements.WordOpenXML.Extensions
} }
if (model.PageMarginTop.HasValue) if (model.PageMarginTop.HasValue)
{ {
pageMargin.Top = model.PageMarginTop.Value; pageMargin.Top = Convert.ToInt32(model.PageMarginTop.Value);
} }
properties.AppendChild(pageMargin); properties.AppendChild(pageMargin);

View File

@ -1,6 +1,7 @@
using DocumentFormat.OpenXml; using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Wordprocessing; using DocumentFormat.OpenXml.Wordprocessing;
using System; using System;
using ToolsModule.ManagmentExtension;
using ToolsOffice.Interfaces.Word.Models; using ToolsOffice.Interfaces.Word.Models;
namespace ToolsOffice.Implements.WordOpenXML.Extensions namespace ToolsOffice.Implements.WordOpenXML.Extensions
@ -20,7 +21,11 @@ namespace ToolsOffice.Implements.WordOpenXML.Extensions
} }
var paragraph = new Paragraph(); var paragraph = new Paragraph();
paragraph.AddParagraphProperties(model); 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) foreach (var text in model.WordTexts)
{ {
@ -43,7 +48,11 @@ namespace ToolsOffice.Implements.WordOpenXML.Extensions
} }
var paragraph = new Paragraph(); var paragraph = new Paragraph();
paragraph.AddParagraphProperties(model); 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) foreach (var text in model.WordTexts)
{ {

View File

@ -1,5 +1,6 @@
using DocumentFormat.OpenXml; using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Wordprocessing; using DocumentFormat.OpenXml.Wordprocessing;
using ToolsModule.ManagmentExtension;
using ToolsOffice.Interfaces.Word.Models; using ToolsOffice.Interfaces.Word.Models;
namespace ToolsOffice.Implements.WordOpenXML.Extensions namespace ToolsOffice.Implements.WordOpenXML.Extensions
@ -34,6 +35,24 @@ namespace ToolsOffice.Implements.WordOpenXML.Extensions
paragraph.AppendChild(docRun); 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>
/// Добавление свойств текста /// Добавление свойств текста
/// </summary> /// </summary>

View File

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

View File

@ -1,10 +1,23 @@
using System.IO; using System.IO;
using ToolsOffice.Interfaces.Word.Models; using ToolsOffice.Interfaces.Word.Models;
using ToolsOffice.Interfaces.Word.Models.Cases;
namespace ToolsOffice.Interfaces.Word namespace ToolsOffice.Interfaces.Word
{ {
public abstract class BuilderWordDocument 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>
/// Создание документа /// Создание документа
/// </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> /// </summary>
public class ModelWordDocument public class ModelWordDocument
{ {
/// <summary>
/// Высота страницы
/// </summary>
public int? PageSizeHeight { get; set; }
/// <summary>
/// Ширина страницы
/// </summary>
public int? PageSizeWidth { get; set; }
/// <summary> /// <summary>
/// Ориентация страницы /// Ориентация страницы
/// </summary> /// </summary>
public TypeWordPageOrientation? WordPageOrientation { get; set; } 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 double? PageMarginRight { get; set; }
public int? PageMarginRight { get; set; }
} }
} }

View File

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

View File

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

View File

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