directory report
This commit is contained in:
parent
adc93488c9
commit
8ef203ae66
100
ProjectHotel/ProjectHotel/Forms/FormDirectoryReport.Designer.cs
generated
Normal file
100
ProjectHotel/ProjectHotel/Forms/FormDirectoryReport.Designer.cs
generated
Normal file
@ -0,0 +1,100 @@
|
||||
namespace ProjectHotel.Forms
|
||||
{
|
||||
partial class FormDirectoryReport
|
||||
{
|
||||
/// <summary>
|
||||
/// Required designer variable.
|
||||
/// </summary>
|
||||
private System.ComponentModel.IContainer components = null;
|
||||
|
||||
/// <summary>
|
||||
/// Clean up any resources being used.
|
||||
/// </summary>
|
||||
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (disposing && (components != null))
|
||||
{
|
||||
components.Dispose();
|
||||
}
|
||||
base.Dispose(disposing);
|
||||
}
|
||||
|
||||
#region Windows Form Designer generated code
|
||||
|
||||
/// <summary>
|
||||
/// Required method for Designer support - do not modify
|
||||
/// the contents of this method with the code editor.
|
||||
/// </summary>
|
||||
private void InitializeComponent()
|
||||
{
|
||||
checkBoxAthletes = new CheckBox();
|
||||
checkBoxRooms = new CheckBox();
|
||||
checkBoxHotels = new CheckBox();
|
||||
buttonBuild = new Button();
|
||||
SuspendLayout();
|
||||
//
|
||||
// checkBoxAthletes
|
||||
//
|
||||
checkBoxAthletes.AutoSize = true;
|
||||
checkBoxAthletes.Location = new Point(12, 12);
|
||||
checkBoxAthletes.Name = "checkBoxAthletes";
|
||||
checkBoxAthletes.Size = new Size(97, 19);
|
||||
checkBoxAthletes.TabIndex = 0;
|
||||
checkBoxAthletes.Text = "Спортсмены";
|
||||
checkBoxAthletes.UseVisualStyleBackColor = true;
|
||||
//
|
||||
// checkBoxRooms
|
||||
//
|
||||
checkBoxRooms.AutoSize = true;
|
||||
checkBoxRooms.Location = new Point(12, 67);
|
||||
checkBoxRooms.Name = "checkBoxRooms";
|
||||
checkBoxRooms.Size = new Size(76, 19);
|
||||
checkBoxRooms.TabIndex = 1;
|
||||
checkBoxRooms.Text = "Комнаты";
|
||||
checkBoxRooms.UseVisualStyleBackColor = true;
|
||||
//
|
||||
// checkBoxHotels
|
||||
//
|
||||
checkBoxHotels.AutoSize = true;
|
||||
checkBoxHotels.Location = new Point(12, 123);
|
||||
checkBoxHotels.Name = "checkBoxHotels";
|
||||
checkBoxHotels.Size = new Size(87, 19);
|
||||
checkBoxHotels.TabIndex = 2;
|
||||
checkBoxHotels.Text = "Гостиницы";
|
||||
checkBoxHotels.UseVisualStyleBackColor = true;
|
||||
//
|
||||
// buttonBuild
|
||||
//
|
||||
buttonBuild.Location = new Point(194, 67);
|
||||
buttonBuild.Name = "buttonBuild";
|
||||
buttonBuild.Size = new Size(97, 24);
|
||||
buttonBuild.TabIndex = 3;
|
||||
buttonBuild.Text = "сформировать";
|
||||
buttonBuild.UseVisualStyleBackColor = true;
|
||||
buttonBuild.Click += ButtonBuild_Click;
|
||||
//
|
||||
// FormDirectoryReport
|
||||
//
|
||||
AutoScaleDimensions = new SizeF(7F, 15F);
|
||||
AutoScaleMode = AutoScaleMode.Font;
|
||||
ClientSize = new Size(357, 216);
|
||||
Controls.Add(buttonBuild);
|
||||
Controls.Add(checkBoxHotels);
|
||||
Controls.Add(checkBoxRooms);
|
||||
Controls.Add(checkBoxAthletes);
|
||||
Name = "FormDirectoryReport";
|
||||
StartPosition = FormStartPosition.CenterParent;
|
||||
Text = "Выгрузка справочников";
|
||||
ResumeLayout(false);
|
||||
PerformLayout();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
private CheckBox checkBoxAthletes;
|
||||
private CheckBox checkBoxRooms;
|
||||
private CheckBox checkBoxHotels;
|
||||
private Button buttonBuild;
|
||||
}
|
||||
}
|
62
ProjectHotel/ProjectHotel/Forms/FormDirectoryReport.cs
Normal file
62
ProjectHotel/ProjectHotel/Forms/FormDirectoryReport.cs
Normal file
@ -0,0 +1,62 @@
|
||||
using ProjectHotel.Reports;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Data;
|
||||
using System.Drawing;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
using Unity;
|
||||
|
||||
namespace ProjectHotel.Forms;
|
||||
|
||||
public partial class FormDirectoryReport : Form
|
||||
{
|
||||
private readonly IUnityContainer _container;
|
||||
public FormDirectoryReport(IUnityContainer container)
|
||||
{
|
||||
InitializeComponent();
|
||||
_container = container ??
|
||||
throw new ArgumentNullException(nameof(container));
|
||||
}
|
||||
private void ButtonBuild_Click(object sender, EventArgs e)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!checkBoxAthletes.Checked &&
|
||||
!checkBoxRooms.Checked && !checkBoxHotels.Checked)
|
||||
{
|
||||
throw new Exception("Не выбран ни один справочник для выгрузки");
|
||||
}
|
||||
var sfd = new SaveFileDialog()
|
||||
{
|
||||
Filter = "Docx Files | *.docx"
|
||||
};
|
||||
if (sfd.ShowDialog() != DialogResult.OK)
|
||||
{
|
||||
throw new Exception("Не выбран файла для отчета");
|
||||
}
|
||||
if
|
||||
(_container.Resolve<DocReport>().CreateDoc(sfd.FileName, checkBoxAthletes.Checked,
|
||||
checkBoxRooms.Checked, checkBoxHotels.Checked))
|
||||
{
|
||||
MessageBox.Show("Документ сформирован",
|
||||
"Формирование документа",
|
||||
MessageBoxButtons.OK,
|
||||
MessageBoxIcon.Information);
|
||||
}
|
||||
else
|
||||
{
|
||||
MessageBox.Show("Возникли ошибки при формировании документа.Подробности в логах",
|
||||
"Формирование документа",
|
||||
MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show(ex.Message, "Ошибка при созданииотчета", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
}
|
||||
}
|
||||
}
|
120
ProjectHotel/ProjectHotel/Forms/FormDirectoryReport.resx
Normal file
120
ProjectHotel/ProjectHotel/Forms/FormDirectoryReport.resx
Normal file
@ -0,0 +1,120 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<root>
|
||||
<!--
|
||||
Microsoft ResX Schema
|
||||
|
||||
Version 2.0
|
||||
|
||||
The primary goals of this format is to allow a simple XML format
|
||||
that is mostly human readable. The generation and parsing of the
|
||||
various data types are done through the TypeConverter classes
|
||||
associated with the data types.
|
||||
|
||||
Example:
|
||||
|
||||
... ado.net/XML headers & schema ...
|
||||
<resheader name="resmimetype">text/microsoft-resx</resheader>
|
||||
<resheader name="version">2.0</resheader>
|
||||
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
|
||||
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
|
||||
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
|
||||
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
|
||||
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
|
||||
<value>[base64 mime encoded serialized .NET Framework object]</value>
|
||||
</data>
|
||||
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
|
||||
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
|
||||
<comment>This is a comment</comment>
|
||||
</data>
|
||||
|
||||
There are any number of "resheader" rows that contain simple
|
||||
name/value pairs.
|
||||
|
||||
Each data row contains a name, and value. The row also contains a
|
||||
type or mimetype. Type corresponds to a .NET class that support
|
||||
text/value conversion through the TypeConverter architecture.
|
||||
Classes that don't support this are serialized and stored with the
|
||||
mimetype set.
|
||||
|
||||
The mimetype is used for serialized objects, and tells the
|
||||
ResXResourceReader how to depersist the object. This is currently not
|
||||
extensible. For a given mimetype the value must be set accordingly:
|
||||
|
||||
Note - application/x-microsoft.net.object.binary.base64 is the format
|
||||
that the ResXResourceWriter will generate, however the reader can
|
||||
read any of the formats listed below.
|
||||
|
||||
mimetype: application/x-microsoft.net.object.binary.base64
|
||||
value : The object must be serialized with
|
||||
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
|
||||
: and then encoded with base64 encoding.
|
||||
|
||||
mimetype: application/x-microsoft.net.object.soap.base64
|
||||
value : The object must be serialized with
|
||||
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
|
||||
: and then encoded with base64 encoding.
|
||||
|
||||
mimetype: application/x-microsoft.net.object.bytearray.base64
|
||||
value : The object must be serialized into a byte array
|
||||
: using a System.ComponentModel.TypeConverter
|
||||
: and then encoded with base64 encoding.
|
||||
-->
|
||||
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
|
||||
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
|
||||
<xsd:element name="root" msdata:IsDataSet="true">
|
||||
<xsd:complexType>
|
||||
<xsd:choice maxOccurs="unbounded">
|
||||
<xsd:element name="metadata">
|
||||
<xsd:complexType>
|
||||
<xsd:sequence>
|
||||
<xsd:element name="value" type="xsd:string" minOccurs="0" />
|
||||
</xsd:sequence>
|
||||
<xsd:attribute name="name" use="required" type="xsd:string" />
|
||||
<xsd:attribute name="type" type="xsd:string" />
|
||||
<xsd:attribute name="mimetype" type="xsd:string" />
|
||||
<xsd:attribute ref="xml:space" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
<xsd:element name="assembly">
|
||||
<xsd:complexType>
|
||||
<xsd:attribute name="alias" type="xsd:string" />
|
||||
<xsd:attribute name="name" type="xsd:string" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
<xsd:element name="data">
|
||||
<xsd:complexType>
|
||||
<xsd:sequence>
|
||||
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
|
||||
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
|
||||
</xsd:sequence>
|
||||
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
|
||||
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
|
||||
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
|
||||
<xsd:attribute ref="xml:space" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
<xsd:element name="resheader">
|
||||
<xsd:complexType>
|
||||
<xsd:sequence>
|
||||
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
|
||||
</xsd:sequence>
|
||||
<xsd:attribute name="name" type="xsd:string" use="required" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
</xsd:choice>
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
</xsd:schema>
|
||||
<resheader name="resmimetype">
|
||||
<value>text/microsoft-resx</value>
|
||||
</resheader>
|
||||
<resheader name="version">
|
||||
<value>2.0</value>
|
||||
</resheader>
|
||||
<resheader name="reader">
|
||||
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
||||
</resheader>
|
||||
<resheader name="writer">
|
||||
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
||||
</resheader>
|
||||
</root>
|
@ -37,6 +37,7 @@
|
||||
PlacingAthleteToolStripMenuItem = new ToolStripMenuItem();
|
||||
CleaningRoomToolStripMenuItem = new ToolStripMenuItem();
|
||||
ReportsToolStripMenuItem = new ToolStripMenuItem();
|
||||
DirectoryReportToolStripMenuItem = new ToolStripMenuItem();
|
||||
menuStrip1.SuspendLayout();
|
||||
SuspendLayout();
|
||||
//
|
||||
@ -59,21 +60,21 @@
|
||||
// RoomsToolStripMenuItem
|
||||
//
|
||||
RoomsToolStripMenuItem.Name = "RoomsToolStripMenuItem";
|
||||
RoomsToolStripMenuItem.Size = new Size(180, 22);
|
||||
RoomsToolStripMenuItem.Size = new Size(145, 22);
|
||||
RoomsToolStripMenuItem.Text = "Комнаты";
|
||||
RoomsToolStripMenuItem.Click += RoomsToolStripMenuItem_Click;
|
||||
//
|
||||
// HotelsToolStripMenuItem
|
||||
//
|
||||
HotelsToolStripMenuItem.Name = "HotelsToolStripMenuItem";
|
||||
HotelsToolStripMenuItem.Size = new Size(180, 22);
|
||||
HotelsToolStripMenuItem.Size = new Size(145, 22);
|
||||
HotelsToolStripMenuItem.Text = "Отелели";
|
||||
HotelsToolStripMenuItem.Click += HotelsToolStripMenuItem_Click;
|
||||
//
|
||||
// AthletesToolStripMenuItem
|
||||
//
|
||||
AthletesToolStripMenuItem.Name = "AthletesToolStripMenuItem";
|
||||
AthletesToolStripMenuItem.Size = new Size(180, 22);
|
||||
AthletesToolStripMenuItem.Size = new Size(145, 22);
|
||||
AthletesToolStripMenuItem.Text = "Спортсмены";
|
||||
AthletesToolStripMenuItem.Click += AthletesToolStripMenuItem_Click;
|
||||
//
|
||||
@ -100,10 +101,19 @@
|
||||
//
|
||||
// ReportsToolStripMenuItem
|
||||
//
|
||||
ReportsToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { DirectoryReportToolStripMenuItem });
|
||||
ReportsToolStripMenuItem.Name = "ReportsToolStripMenuItem";
|
||||
ReportsToolStripMenuItem.Size = new Size(60, 20);
|
||||
ReportsToolStripMenuItem.Text = "Отчеты";
|
||||
//
|
||||
// DirectoryReportToolStripMenuItem
|
||||
//
|
||||
DirectoryReportToolStripMenuItem.Name = "DirectoryReportToolStripMenuItem";
|
||||
DirectoryReportToolStripMenuItem.ShortcutKeys = Keys.Control | Keys.W;
|
||||
DirectoryReportToolStripMenuItem.Size = new Size(280, 22);
|
||||
DirectoryReportToolStripMenuItem.Text = "Документ со справочниками";
|
||||
DirectoryReportToolStripMenuItem.Click += DirectoryReportToolStripMenuItem_Click;
|
||||
//
|
||||
// FormHotelForAthletes
|
||||
//
|
||||
AutoScaleDimensions = new SizeF(7F, 15F);
|
||||
@ -133,5 +143,6 @@
|
||||
private ToolStripMenuItem PlacingAthleteToolStripMenuItem;
|
||||
private ToolStripMenuItem ReportsToolStripMenuItem;
|
||||
private ToolStripMenuItem CleaningRoomToolStripMenuItem;
|
||||
private ToolStripMenuItem DirectoryReportToolStripMenuItem;
|
||||
}
|
||||
}
|
||||
|
@ -80,4 +80,17 @@ public partial class FormHotelForAthletes : Form
|
||||
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
}
|
||||
}
|
||||
|
||||
private void DirectoryReportToolStripMenuItem_Click(object sender, EventArgs e)
|
||||
{
|
||||
try
|
||||
{
|
||||
_container.Resolve<FormDirectoryReport>().ShowDialog();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show(ex.Message, "Îøèáêà ïðè çàãðóçêå",
|
||||
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -25,11 +25,13 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Dapper" Version="2.1.35" />
|
||||
<PackageReference Include="DocumentFormat.OpenXml" Version="3.2.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="9.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="9.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.0" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||
<PackageReference Include="Npgsql" Version="9.0.1" />
|
||||
<PackageReference Include="PDFsharp-MigraDoc-GDI" Version="6.1.1" />
|
||||
<PackageReference Include="Serilog" Version="4.1.0" />
|
||||
<PackageReference Include="Serilog.Extensions.Logging" Version="8.0.0" />
|
||||
<PackageReference Include="Serilog.Settings.Configuration" Version="8.0.4" />
|
||||
|
83
ProjectHotel/ProjectHotel/Reports/DocReport.cs
Normal file
83
ProjectHotel/ProjectHotel/Reports/DocReport.cs
Normal file
@ -0,0 +1,83 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using ProjectHotel.Repositories;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ProjectHotel.Reports;
|
||||
|
||||
internal class DocReport
|
||||
{
|
||||
private readonly IAthleteRepository _athleteRepository;
|
||||
private readonly IRoomRepository _roomRepository;
|
||||
private readonly IHotelRepository _hotelRepository;
|
||||
private readonly ILogger<DocReport> _logger;
|
||||
public DocReport(IAthleteRepository athleteRepository, IRoomRepository roomRepository, IHotelRepository hotelRepository, ILogger<DocReport> logger)
|
||||
{
|
||||
_athleteRepository = athleteRepository ?? throw new ArgumentNullException(nameof(athleteRepository));
|
||||
_roomRepository = roomRepository ?? throw new ArgumentNullException(nameof(roomRepository));
|
||||
_hotelRepository = hotelRepository ?? throw new ArgumentNullException(nameof(hotelRepository));
|
||||
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
||||
}
|
||||
public bool CreateDoc(string filePath, bool includeAthletes, bool includeRooms, bool includeHotels)
|
||||
{
|
||||
try
|
||||
{
|
||||
var builder = new WordBuilder(filePath)
|
||||
.AddHeader("Документ со справочниками");
|
||||
if (includeAthletes)
|
||||
{
|
||||
builder.AddParagraph("Спортсмены")
|
||||
.AddTable([2400, 2400, 2400, 1200, 1200],
|
||||
GetAthletes());
|
||||
}
|
||||
if (includeRooms)
|
||||
{
|
||||
builder.AddParagraph("Комнаты")
|
||||
.AddTable([2400, 2400, 2400], GetRooms());
|
||||
}
|
||||
if (includeHotels)
|
||||
{
|
||||
builder.AddParagraph("Гостиницы")
|
||||
.AddTable([2400, 2400], GetHotels());
|
||||
}
|
||||
builder.Build();
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при формировании документа");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
private List<string[]> GetAthletes()
|
||||
{
|
||||
return [
|
||||
["Имя", "Фамилия", "Отчество", "Вид/Виды спорта", "Разряд"],
|
||||
.. _athleteRepository.ReadAthletes().Select(x => new string[] { x.FirstName,
|
||||
x.LastName,x.FatherName, x.Sport.ToString(), x.AthleteClass.ToString() }),
|
||||
];
|
||||
}
|
||||
private List<string[]> GetRooms()
|
||||
{
|
||||
return [
|
||||
["Отель", "Номер", "Вместимость"],
|
||||
.. _roomRepository
|
||||
.ReadRooms()
|
||||
.Select(x => new string[] { x.HotelId.ToString(), x.Name,
|
||||
x.Capacity.ToString() }),
|
||||
];
|
||||
}
|
||||
private List<string[]> GetHotels()
|
||||
{
|
||||
return [
|
||||
["Название", "Адрес"],
|
||||
.. _hotelRepository
|
||||
.ReadHotels()
|
||||
.Select(x => new string[] { x.Name,
|
||||
x.Address}),
|
||||
];
|
||||
}
|
||||
}
|
129
ProjectHotel/ProjectHotel/Reports/WordBuilder.cs
Normal file
129
ProjectHotel/ProjectHotel/Reports/WordBuilder.cs
Normal file
@ -0,0 +1,129 @@
|
||||
using DocumentFormat.OpenXml.Packaging;
|
||||
using DocumentFormat.OpenXml.Wordprocessing;
|
||||
using DocumentFormat.OpenXml;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ProjectHotel.Reports;
|
||||
|
||||
internal class WordBuilder
|
||||
{
|
||||
private readonly string _filePath;
|
||||
|
||||
private readonly Document _document;
|
||||
|
||||
private readonly Body _body;
|
||||
|
||||
public WordBuilder(string filePath)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(filePath)) throw new ArgumentNullException(nameof(filePath));
|
||||
|
||||
if (File.Exists(filePath)) File.Delete(filePath);
|
||||
|
||||
_filePath = filePath;
|
||||
_document = new Document();
|
||||
_body = _document.AppendChild(new Body());
|
||||
}
|
||||
|
||||
public WordBuilder AddHeader(string header)
|
||||
{
|
||||
var paragraph = _body.AppendChild(new Paragraph());
|
||||
var run = paragraph.AppendChild(new Run());
|
||||
var runProperties = run.AppendChild(new RunProperties());
|
||||
runProperties.AppendChild(new Bold());
|
||||
run.AppendChild(new Text(header));
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public WordBuilder AddParagraph(string text)
|
||||
{
|
||||
var paragraph = _body.AppendChild(new Paragraph());
|
||||
var run = paragraph.AppendChild(new Run());
|
||||
run.AppendChild(new Text(text));
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public WordBuilder AddTable(int[] widths, List<string[]> data)
|
||||
{
|
||||
if (widths == null || widths.Length == 0)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(widths));
|
||||
}
|
||||
|
||||
if (data == null || data.Count == 0)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(data));
|
||||
}
|
||||
|
||||
if (data.Any(x => x.Length != widths.Length))
|
||||
{
|
||||
throw new InvalidOperationException("widths.Length != data.Length");
|
||||
}
|
||||
|
||||
var table = new Table();
|
||||
table.AppendChild(new TableProperties(
|
||||
new TableBorders(
|
||||
new TopBorder()
|
||||
{
|
||||
Val = new EnumValue<BorderValues>(BorderValues.Single),
|
||||
Size = 12
|
||||
},
|
||||
new BottomBorder()
|
||||
{
|
||||
Val = new EnumValue<BorderValues>(BorderValues.Single),
|
||||
Size = 12
|
||||
},
|
||||
new LeftBorder()
|
||||
{
|
||||
Val = new EnumValue<BorderValues>(BorderValues.Single),
|
||||
Size = 12
|
||||
},
|
||||
new RightBorder()
|
||||
{
|
||||
Val = new EnumValue<BorderValues>(BorderValues.Single),
|
||||
Size = 12
|
||||
},
|
||||
new InsideHorizontalBorder()
|
||||
{
|
||||
Val = new EnumValue<BorderValues>(BorderValues.Single),
|
||||
Size = 12
|
||||
},
|
||||
new InsideVerticalBorder()
|
||||
{
|
||||
Val = new EnumValue<BorderValues>(BorderValues.Single),
|
||||
Size = 12
|
||||
}
|
||||
)
|
||||
));
|
||||
|
||||
// Заголовок
|
||||
var tr = new TableRow();
|
||||
for (var j = 0; j < widths.Length; ++j)
|
||||
{
|
||||
tr.Append(new TableCell(new TableCellProperties(
|
||||
new TableCellWidth() { Width = widths[j].ToString() }),
|
||||
new Paragraph(new Run(new RunProperties(new Bold()), new Text(data.First()[j])))));
|
||||
}
|
||||
table.Append(tr);
|
||||
|
||||
// Данные
|
||||
table.Append(data.Skip(1).Select(x => new TableRow(
|
||||
x.Select(y => new TableCell(
|
||||
new Paragraph(new Run(new Text(y))))))));
|
||||
_body.Append(table);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public void Build()
|
||||
{
|
||||
using var wordDocument = WordprocessingDocument.Create(_filePath, WordprocessingDocumentType.Document);
|
||||
var mainPart = wordDocument.AddMainDocumentPart();
|
||||
mainPart.Document = _document;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user