first edit
This commit is contained in:
124
XLIMS.CORE/Helpers/OpenPsv.cs
Normal file
124
XLIMS.CORE/Helpers/OpenPsv.cs
Normal file
@@ -0,0 +1,124 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace XLIMS.CORE.Helpers
|
||||
{
|
||||
public class OpenPsv
|
||||
{
|
||||
#region Fields
|
||||
|
||||
private readonly dynamic _wordApp;
|
||||
private readonly dynamic _wordDocument;
|
||||
private readonly dynamic _table;
|
||||
|
||||
private readonly DocViewModel _document;
|
||||
private readonly XLAB7Repository _repository;
|
||||
private readonly int _copies;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructor
|
||||
|
||||
public OpenPsv(DocViewModel document, XLAB7Repository repository, int copies = 1)
|
||||
{
|
||||
_document = document ?? throw new ArgumentNullException(nameof(document));
|
||||
_repository = repository ?? throw new ArgumentNullException(nameof(repository));
|
||||
_copies = copies < 1 ? 1 : copies;
|
||||
|
||||
var templatePath = Path.Combine("Resources", "DOCX", "OpenPsv.docx");
|
||||
|
||||
if (!File.Exists(templatePath))
|
||||
throw new FileNotFoundException("Шаблон Word не найден", templatePath);
|
||||
|
||||
var wordType = Type.GetTypeFromProgID("Word.Application");
|
||||
if (wordType == null)
|
||||
throw new InvalidOperationException("Microsoft Word не установлен");
|
||||
|
||||
_wordApp = Activator.CreateInstance(wordType);
|
||||
_wordApp.Visible = false;
|
||||
|
||||
_wordDocument = _wordApp.Documents.Open(templatePath);
|
||||
|
||||
if (_wordDocument.Tables.Count < 1)
|
||||
throw new InvalidOperationException("В документе отсутствует таблица");
|
||||
|
||||
_table = _wordDocument.Tables[1];
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
private void Replace(string placeholder, string value)
|
||||
{
|
||||
dynamic range = _wordDocument.Content;
|
||||
dynamic find = range.Find;
|
||||
|
||||
find.ClearFormatting();
|
||||
find.Text = placeholder;
|
||||
find.Replacement.ClearFormatting();
|
||||
find.Replacement.Text = value ?? string.Empty;
|
||||
|
||||
find.Execute(
|
||||
Replace: 2, // wdReplaceAll
|
||||
Wrap: 1 // wdFindContinue
|
||||
);
|
||||
}
|
||||
|
||||
public void Print()
|
||||
{
|
||||
Replace("number", _document.Number);
|
||||
Replace("div", _document.CurrentFRPD.NMFRPD);
|
||||
Replace("date", ((DateTime)_document.Date).ToLongDateString());
|
||||
Replace("count", _document.AllEKZMK.Count.ToString());
|
||||
Replace("next", ((DateTime)_document.Date).AddDays(30).ToLongDateString());
|
||||
Replace("person", _document.CurrentPRSNPR.PRFIO);
|
||||
|
||||
int rowIndex = 2;
|
||||
int index = 1;
|
||||
|
||||
foreach (var group in _document.AllGroupsEKZMK)
|
||||
{
|
||||
_table.Rows.Add();
|
||||
|
||||
_table.Cell(rowIndex, 1).Range.Text = index.ToString();
|
||||
_table.Cell(rowIndex, 2).Range.Text =
|
||||
group.CurrentEKZMK.CurrentEKZ.IDTPRZNavigation
|
||||
.IDTIPSNavigation.IDSPNMTPNavigation.NMTP;
|
||||
|
||||
_table.Cell(rowIndex, 3).Range.Text =
|
||||
group.CurrentEKZMK.CurrentEKZ.IDTPRZNavigation
|
||||
.IDTIPSNavigation.TP;
|
||||
|
||||
_table.Cell(rowIndex, 4).Range.Text =
|
||||
group.CurrentEKZMK.CurrentEKZ.IDTPRZNavigation.DPZN;
|
||||
|
||||
if (group.GroupEKZMK.Count() > 3)
|
||||
{
|
||||
_table.Cell(rowIndex, 5).Range.Text =
|
||||
group.GroupEKZMK.Count().ToString();
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (var item in group.GroupEKZMK)
|
||||
{
|
||||
_table.Cell(rowIndex, 5).Range.Text +=
|
||||
item.CurrentEKZ.NNZV + ", ";
|
||||
}
|
||||
}
|
||||
|
||||
_table.Cell(rowIndex, 6).Range.Text = group.Count.ToString();
|
||||
_table.Cell(rowIndex, 9).Range.Text = group.CurrentEKZMK.DSEKZMK;
|
||||
|
||||
rowIndex++;
|
||||
index++;
|
||||
}
|
||||
|
||||
_wordDocument.PrintOut(Copies: _copies);
|
||||
_wordDocument.Close(false);
|
||||
_wordApp.Quit();
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user