лаб2 - готово
This commit is contained in:
parent
7bdd5ea8fb
commit
bdf15f37cd
4
WinFormsLibrary1/WinFormsApp1/Form1.Designer.cs
generated
4
WinFormsLibrary1/WinFormsApp1/Form1.Designer.cs
generated
@ -1,5 +1,4 @@
|
||||
using System.Windows.Forms;
|
||||
using WinFormsLibrary1;
|
||||
using WinFormsLibrary1;
|
||||
|
||||
namespace WinFormsApp1
|
||||
{
|
||||
@ -41,7 +40,6 @@ namespace WinFormsApp1
|
||||
//
|
||||
// dateInputControl1
|
||||
//
|
||||
dateInputControl1.Date = "";
|
||||
dateInputControl1.Location = new Point(13, 107);
|
||||
dateInputControl1.Margin = new Padding(4, 5, 4, 5);
|
||||
dateInputControl1.Name = "dateInputControl1";
|
||||
|
81
WinFormsLibrary1/WinFormsApp1/Form2.Designer.cs
generated
Normal file
81
WinFormsLibrary1/WinFormsApp1/Form2.Designer.cs
generated
Normal file
@ -0,0 +1,81 @@
|
||||
using WinFormsLibrary1;
|
||||
|
||||
namespace WinFormsApp1
|
||||
{
|
||||
partial class Form2
|
||||
{
|
||||
private System.ComponentModel.IContainer components = null;
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (disposing && (components != null))
|
||||
{
|
||||
components.Dispose();
|
||||
}
|
||||
base.Dispose(disposing);
|
||||
}
|
||||
|
||||
#region Windows Form Designer generated code
|
||||
private void InitializeComponent()
|
||||
{
|
||||
components = new System.ComponentModel.Container();
|
||||
buttonCreateTextDocument = new Button();
|
||||
buttonCreateTableDocument = new Button();
|
||||
buttonCreateDiagramDocument = new Button();
|
||||
bigTextComponent1 = new ComponentWithBigText(components);
|
||||
tableComponent1 = new ComponentTable(components);
|
||||
diagramComponent1 = new ComponentDiagram(components);
|
||||
SuspendLayout();
|
||||
//
|
||||
// buttonCreateTextDocument
|
||||
//
|
||||
buttonCreateTextDocument.Location = new Point(12, 30);
|
||||
buttonCreateTextDocument.Name = "buttonCreateTextDocument";
|
||||
buttonCreateTextDocument.Size = new Size(125, 50);
|
||||
buttonCreateTextDocument.TabIndex = 0;
|
||||
buttonCreateTextDocument.Text = "Создать документ с текстом";
|
||||
buttonCreateTextDocument.UseVisualStyleBackColor = true;
|
||||
buttonCreateTextDocument.Click += buttonCreateTextDocument_Click;
|
||||
//
|
||||
// buttonCreateTableDocument
|
||||
//
|
||||
buttonCreateTableDocument.Location = new Point(180, 30);
|
||||
buttonCreateTableDocument.Name = "buttonCreateTableDocument";
|
||||
buttonCreateTableDocument.Size = new Size(125, 50);
|
||||
buttonCreateTableDocument.TabIndex = 1;
|
||||
buttonCreateTableDocument.Text = "Создать документ с таблицей";
|
||||
buttonCreateTableDocument.UseVisualStyleBackColor = true;
|
||||
buttonCreateTableDocument.Click += buttonCreateTableDocument_Click;
|
||||
//
|
||||
// buttonCreateDiagramDocument
|
||||
//
|
||||
buttonCreateDiagramDocument.Location = new Point(347, 30);
|
||||
buttonCreateDiagramDocument.Name = "buttonCreateDiagramDocument";
|
||||
buttonCreateDiagramDocument.Size = new Size(125, 50);
|
||||
buttonCreateDiagramDocument.TabIndex = 2;
|
||||
buttonCreateDiagramDocument.Text = "Создать документ с диаграммой";
|
||||
buttonCreateDiagramDocument.UseVisualStyleBackColor = true;
|
||||
buttonCreateDiagramDocument.Click += buttonCreateDiagramDocument_Click;
|
||||
//
|
||||
// FormNonVisualComponents
|
||||
//
|
||||
AutoScaleDimensions = new SizeF(7F, 15F);
|
||||
AutoScaleMode = AutoScaleMode.Font;
|
||||
ClientSize = new Size(484, 111);
|
||||
Controls.Add(buttonCreateDiagramDocument);
|
||||
Controls.Add(buttonCreateTableDocument);
|
||||
Controls.Add(buttonCreateTextDocument);
|
||||
Name = "FormNonVisualComponents";
|
||||
Text = "Не визуальные компоненты";
|
||||
ResumeLayout(false);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
private Button buttonCreateTextDocument;
|
||||
private Button buttonCreateTableDocument;
|
||||
private Button buttonCreateDiagramDocument;
|
||||
private ComponentWithBigText bigTextComponent1;
|
||||
private ComponentTable tableComponent1;
|
||||
private ComponentDiagram diagramComponent1;
|
||||
}
|
||||
}
|
94
WinFormsLibrary1/WinFormsApp1/Form2.cs
Normal file
94
WinFormsLibrary1/WinFormsApp1/Form2.cs
Normal file
@ -0,0 +1,94 @@
|
||||
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 WinFormsLibrary1.Models;
|
||||
|
||||
namespace WinFormsApp1
|
||||
{
|
||||
public partial class Form2: Form
|
||||
{
|
||||
public Form2()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
private void buttonCreateTextDocument_Click(object sender, EventArgs e)
|
||||
{
|
||||
string filepath = "C:\\Users\\Владимир\\Desktop\\BigTextDocumentExcel.xlsx";
|
||||
string title = "Документ с большим текстом";
|
||||
|
||||
string[] rows =
|
||||
{
|
||||
"Строка1",
|
||||
"Строка2",
|
||||
"Строка3",
|
||||
"Строка4"
|
||||
};
|
||||
|
||||
bigTextComponent1.CreateDocument(filepath, title, rows);
|
||||
}
|
||||
private void buttonCreateTableDocument_Click(object sender, EventArgs e)
|
||||
{
|
||||
string filepath = "C:\\Users\\Владимир\\Desktop\\TableDocumentExcel.xlsx";
|
||||
string title = "Документ с таблицей";
|
||||
|
||||
List<MergeCell> mergeCells = new List<MergeCell>()
|
||||
{
|
||||
new MergeCell("Личные данные", new int[] { 2, 3, 4 }),
|
||||
};
|
||||
List<Column> columns = new List<Column>()
|
||||
{
|
||||
new Column("Id", "Id", 10),
|
||||
new Column("Status", "Статус", 10),
|
||||
new Column("Name", "Имя", 20),
|
||||
new Column("Familia", "Фамилия", 20),
|
||||
new Column("Age", "Возраст", 20),
|
||||
new Column("Department", "Подразделение", 30),
|
||||
new Column("Salary", "зарплата", 10)
|
||||
};
|
||||
|
||||
List<User> data = new List<User>()
|
||||
{
|
||||
new User(1, "нет", "Владимир", "Строев", 34, "Департамент 1", 2000.1),
|
||||
new User(2, "нет", "Михаил", "Патрушев", 23, "Департамент 2", 192.9),
|
||||
new User(3, "да", "Евгений", "Борисов", 19, "Департамент 4", 566),
|
||||
new User(4, "да", "Иван", "Иванов", 41, "Департамент 5", 3990.5),
|
||||
new User(5, "нет", "Владимир", "Строев", 39, "Департамент 3", 1596.0),
|
||||
};
|
||||
|
||||
tableComponent1.CreateDocument(filepath, title,
|
||||
mergeCells, columns,
|
||||
data);
|
||||
}
|
||||
private void buttonCreateDiagramDocument_Click(object sender, EventArgs e)
|
||||
{
|
||||
LineChartConfig config = new LineChartConfig();
|
||||
config.Filepath = "C:\\Users\\Владимир\\Desktop\\DiagramDocumentExcel.xlsx";
|
||||
config.Header = "Документ с диаграммой";
|
||||
config.ChartTitle = "Моя диаграмма";
|
||||
string[] charts = { "График 1", "График 2" };
|
||||
|
||||
var data = new Dictionary<string, List<int>>();
|
||||
for (int i = 0; i < 2; i++)
|
||||
{
|
||||
var row = new List<int>();
|
||||
for (var j = 0; j < 5; j++)
|
||||
{
|
||||
row.Add(5 * i + j + 1);
|
||||
}
|
||||
|
||||
data.Add(charts[i], row);
|
||||
}
|
||||
config.Values = data;
|
||||
|
||||
config.LegendPosition = LegendPosition.Bottom;
|
||||
|
||||
diagramComponent1.CreateDocument(config);
|
||||
}
|
||||
}
|
||||
}
|
120
WinFormsLibrary1/WinFormsApp1/Form2.resx
Normal file
120
WinFormsLibrary1/WinFormsApp1/Form2.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>
|
@ -11,7 +11,7 @@ namespace WinFormsApp1
|
||||
// To customize application configuration such as set high DPI settings or default font,
|
||||
// see https://aka.ms/applicationconfiguration.
|
||||
ApplicationConfiguration.Initialize();
|
||||
Application.Run(new Form1());
|
||||
Application.Run(new Form2());
|
||||
}
|
||||
}
|
||||
}
|
32
WinFormsLibrary1/WinFormsApp1/User.cs
Normal file
32
WinFormsLibrary1/WinFormsApp1/User.cs
Normal file
@ -0,0 +1,32 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace WinFormsApp1
|
||||
{
|
||||
internal class User
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public string Status { get; set; }
|
||||
public string Name { get; set; }
|
||||
public string Familia { get; set; }
|
||||
public int Age { get; set; }
|
||||
public string Department { get; set; }
|
||||
public double Salary { get; set; }
|
||||
public User() { }
|
||||
|
||||
public User(int id, string status, string name, string familia, int age, string department, double salary)
|
||||
{
|
||||
Id = id;
|
||||
Status = status;
|
||||
Name = name;
|
||||
Familia = familia;
|
||||
Age = age;
|
||||
Department = department;
|
||||
Salary = salary;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
36
WinFormsLibrary1/WinFormsLibrary1/ComponentDiagram.Designer.cs
generated
Normal file
36
WinFormsLibrary1/WinFormsLibrary1/ComponentDiagram.Designer.cs
generated
Normal file
@ -0,0 +1,36 @@
|
||||
namespace WinFormsLibrary1
|
||||
{
|
||||
partial class ComponentDiagram
|
||||
{
|
||||
/// <summary>
|
||||
/// Обязательная переменная конструктора.
|
||||
/// </summary>
|
||||
private System.ComponentModel.IContainer components = null;
|
||||
|
||||
/// <summary>
|
||||
/// Освободить все используемые ресурсы.
|
||||
/// </summary>
|
||||
/// <param name="disposing">истинно, если управляемый ресурс должен быть удален; иначе ложно.</param>
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (disposing && (components != null))
|
||||
{
|
||||
components.Dispose();
|
||||
}
|
||||
base.Dispose(disposing);
|
||||
}
|
||||
|
||||
#region Код, автоматически созданный конструктором компонентов
|
||||
|
||||
/// <summary>
|
||||
/// Требуемый метод для поддержки конструктора — не изменяйте
|
||||
/// содержимое этого метода с помощью редактора кода.
|
||||
/// </summary>
|
||||
private void InitializeComponent()
|
||||
{
|
||||
components = new System.ComponentModel.Container();
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
123
WinFormsLibrary1/WinFormsLibrary1/ComponentDiagram.cs
Normal file
123
WinFormsLibrary1/WinFormsLibrary1/ComponentDiagram.cs
Normal file
@ -0,0 +1,123 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using WinFormsLibrary1.Models;
|
||||
using Excel = Microsoft.Office.Interop.Excel;
|
||||
|
||||
namespace WinFormsLibrary1
|
||||
{
|
||||
public partial class ComponentDiagram: Component
|
||||
{
|
||||
public ComponentDiagram()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
public ComponentDiagram(IContainer container)
|
||||
{
|
||||
container.Add(this);
|
||||
|
||||
InitializeComponent();
|
||||
}
|
||||
public void CreateDocument(LineChartConfig config)
|
||||
{
|
||||
if (config == null)
|
||||
{
|
||||
throw new ArgumentNullException("Не задана конфигурация для построения линйеной диаграммы!");
|
||||
}
|
||||
if (string.IsNullOrEmpty(config.Filepath))
|
||||
{
|
||||
throw new ArgumentNullException("Не указан путь к файлу!");
|
||||
}
|
||||
if (string.IsNullOrEmpty(config.Header))
|
||||
{
|
||||
throw new ArgumentNullException("Не указан заголовок документа!");
|
||||
}
|
||||
if (string.IsNullOrEmpty(config.ChartTitle))
|
||||
{
|
||||
throw new ArgumentNullException("Не указано название диаграммы!");
|
||||
}
|
||||
if (config.Values == null || config.Values.Count == 0)
|
||||
{
|
||||
throw new ArgumentNullException("Не заданы значения для отображения на диаграмме!");
|
||||
}
|
||||
|
||||
Excel.Application excelApp = new Excel.Application();
|
||||
Excel.Workbook workbook = excelApp.Workbooks.Add();
|
||||
Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Sheets[1];
|
||||
|
||||
worksheet.Cells[1, 1] = config.Header;
|
||||
|
||||
// Создание диаграммы
|
||||
Excel.ChartObjects chartObjs = (Excel.ChartObjects)worksheet.ChartObjects();
|
||||
Excel.ChartObject chartObj = chartObjs.Add(5, 50, 300, 300);
|
||||
Excel.Chart excelChart = chartObj.Chart;
|
||||
excelChart.ChartType = Excel.XlChartType.xlLine;
|
||||
|
||||
// Запись данных
|
||||
Excel.Range[] valuesRange = new Excel.Range[config.Values.Count];
|
||||
int leftTopI = 2, leftTopJ = 1;
|
||||
for (int i = 0; i < config.Values.Count; i++)
|
||||
{
|
||||
string key = config.Values.Keys.ToList()[i];
|
||||
for (int j = 0; j < config.Values[key].Count; j++)
|
||||
{
|
||||
worksheet.Cells[leftTopI + i, leftTopJ + j] = config.Values[key][j];
|
||||
}
|
||||
|
||||
valuesRange[i] = worksheet.Range
|
||||
[worksheet.Cells[leftTopI + i, leftTopJ],
|
||||
worksheet.Cells[leftTopI + i, leftTopJ + config.Values[key].Count - 1]];
|
||||
}
|
||||
|
||||
// Задание данных
|
||||
Excel.SeriesCollection seriesCollection = (Excel.SeriesCollection)excelChart.SeriesCollection();
|
||||
for (int i = 0; i < config.Values.Keys.Count; i++)
|
||||
{
|
||||
Excel.Series series = seriesCollection.NewSeries();
|
||||
series.Name = config.Values.Keys.ToList()[i];
|
||||
series.Values = valuesRange[i];
|
||||
}
|
||||
|
||||
// Задание заголовка
|
||||
excelChart.HasTitle = true;
|
||||
excelChart.ChartTitle.Text = config.ChartTitle;
|
||||
|
||||
// Задание легенды
|
||||
excelChart.HasLegend = true;
|
||||
switch (config.LegendPosition)
|
||||
{
|
||||
case LegendPosition.Top:
|
||||
excelChart.Legend.Position = Excel.XlLegendPosition.xlLegendPositionTop;
|
||||
break;
|
||||
case LegendPosition.Right:
|
||||
excelChart.Legend.Position = Excel.XlLegendPosition.xlLegendPositionRight;
|
||||
break;
|
||||
case LegendPosition.Bottom:
|
||||
excelChart.Legend.Position = Excel.XlLegendPosition.xlLegendPositionBottom;
|
||||
break;
|
||||
case LegendPosition.Left:
|
||||
excelChart.Legend.Position = Excel.XlLegendPosition.xlLegendPositionLeft;
|
||||
break;
|
||||
default:
|
||||
excelChart.Legend.Position = Excel.XlLegendPosition.xlLegendPositionBottom;
|
||||
break;
|
||||
}
|
||||
|
||||
if (File.Exists(config.Filepath))
|
||||
{
|
||||
File.Delete(config.Filepath);
|
||||
}
|
||||
|
||||
excelApp.Application.ActiveWorkbook.SaveAs(config.Filepath);
|
||||
workbook.Close(true);
|
||||
excelApp.Quit();
|
||||
|
||||
Marshal.ReleaseComObject(excelApp);
|
||||
}
|
||||
}
|
||||
}
|
36
WinFormsLibrary1/WinFormsLibrary1/ComponentTable.Designer.cs
generated
Normal file
36
WinFormsLibrary1/WinFormsLibrary1/ComponentTable.Designer.cs
generated
Normal file
@ -0,0 +1,36 @@
|
||||
namespace WinFormsLibrary1
|
||||
{
|
||||
partial class ComponentTable
|
||||
{
|
||||
/// <summary>
|
||||
/// Обязательная переменная конструктора.
|
||||
/// </summary>
|
||||
private System.ComponentModel.IContainer components = null;
|
||||
|
||||
/// <summary>
|
||||
/// Освободить все используемые ресурсы.
|
||||
/// </summary>
|
||||
/// <param name="disposing">истинно, если управляемый ресурс должен быть удален; иначе ложно.</param>
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (disposing && (components != null))
|
||||
{
|
||||
components.Dispose();
|
||||
}
|
||||
base.Dispose(disposing);
|
||||
}
|
||||
|
||||
#region Код, автоматически созданный конструктором компонентов
|
||||
|
||||
/// <summary>
|
||||
/// Требуемый метод для поддержки конструктора — не изменяйте
|
||||
/// содержимое этого метода с помощью редактора кода.
|
||||
/// </summary>
|
||||
private void InitializeComponent()
|
||||
{
|
||||
components = new System.ComponentModel.Container();
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
162
WinFormsLibrary1/WinFormsLibrary1/ComponentTable.cs
Normal file
162
WinFormsLibrary1/WinFormsLibrary1/ComponentTable.cs
Normal file
@ -0,0 +1,162 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using WinFormsLibrary1.Models;
|
||||
using Excel = Microsoft.Office.Interop.Excel;
|
||||
|
||||
namespace WinFormsLibrary1
|
||||
{
|
||||
public partial class ComponentTable : Component
|
||||
{
|
||||
private char[] _columnIndexes = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
|
||||
public ComponentTable()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
public ComponentTable(IContainer container)
|
||||
{
|
||||
container.Add(this);
|
||||
|
||||
InitializeComponent();
|
||||
}
|
||||
public void CreateDocument<T>(string filepath, string title,
|
||||
List<MergeCell> mergeCells, List<Column> columns,
|
||||
List<T> data) where T : class, new()
|
||||
{
|
||||
if (string.IsNullOrEmpty(filepath))
|
||||
{
|
||||
throw new ArgumentNullException("Не указан путь к файлу!");
|
||||
}
|
||||
if (string.IsNullOrEmpty(title))
|
||||
{
|
||||
throw new ArgumentNullException("Не указан заголовок документа!");
|
||||
}
|
||||
if (mergeCells == null || mergeCells.Count == 0 || columns == null || columns.Count == 0)
|
||||
{
|
||||
throw new ArgumentNullException("Не заполнена информация по колонкам!");
|
||||
}
|
||||
if (data == null)
|
||||
{
|
||||
throw new ArgumentNullException("Данные не заполнены!");
|
||||
}
|
||||
|
||||
Excel.Application excelApp = new Excel.Application();
|
||||
Excel.Workbook workbook = excelApp.Workbooks.Add();
|
||||
Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Sheets[1];
|
||||
|
||||
worksheet.Cells[1, 1] = title;
|
||||
|
||||
// Заголовки таблицы
|
||||
for (int i = 1; i <= columns.Count; i++)
|
||||
{
|
||||
if (string.IsNullOrEmpty(columns[i - 1].Header))
|
||||
{
|
||||
throw new Exception("Заголовок не имеет данных!");
|
||||
}
|
||||
|
||||
worksheet.Cells[2, i] = columns[i - 1].Header;
|
||||
|
||||
Excel.Range column = (Excel.Range)worksheet.Columns[i];
|
||||
column.ColumnWidth = columns[i - 1].Width;
|
||||
|
||||
Excel.Range cell = (Excel.Range)worksheet.Cells[2, i];
|
||||
cell.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
|
||||
cell.VerticalAlignment = Excel.XlHAlign.xlHAlignGeneral;
|
||||
cell.Font.Bold = true;
|
||||
}
|
||||
|
||||
// Объединение ячеек по столбцам
|
||||
List<int> mergeIndexes = new List<int>();
|
||||
foreach (var merge in mergeCells)
|
||||
{
|
||||
mergeIndexes.AddRange(merge.CellIndexes);
|
||||
|
||||
Excel.Range rangeToCopy = worksheet.get_Range($"{_columnIndexes[merge.CellIndexes[0]]}2", $"{_columnIndexes[merge.CellIndexes[merge.CellIndexes.Length - 1]]}2").Cells;
|
||||
Excel.Range rangeToInsert = worksheet.get_Range($"{_columnIndexes[merge.CellIndexes[0]]}3", $"{_columnIndexes[merge.CellIndexes[merge.CellIndexes.Length - 1]]}3").Cells;
|
||||
rangeToInsert.Insert(Excel.XlInsertShiftDirection.xlShiftToRight, rangeToCopy.Cut());
|
||||
|
||||
Excel.Range rangeMerge = worksheet.get_Range($"{_columnIndexes[merge.CellIndexes[0]]}2", $"{_columnIndexes[merge.CellIndexes[merge.CellIndexes.Length - 1]]}2").Cells;
|
||||
rangeMerge.Merge();
|
||||
worksheet.Cells[2, merge.CellIndexes[0] + 1] = merge.Header;
|
||||
Excel.Range cell = (Excel.Range)worksheet.Cells[2, merge.CellIndexes[0] + 1];
|
||||
cell.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
|
||||
cell.VerticalAlignment = Excel.XlHAlign.xlHAlignCenter;
|
||||
cell.Font.Bold = true;
|
||||
}
|
||||
|
||||
// Объединение ячеек по строкам, которые не объединяются по столбцам
|
||||
for (int i = 1; i <= columns.Count; i++)
|
||||
{
|
||||
if (!mergeIndexes.Contains(i - 1))
|
||||
{
|
||||
Excel.Range range = worksheet.get_Range($"{_columnIndexes[i - 1]}2", $"{_columnIndexes[i - 1]}3").Cells;
|
||||
range.Merge();
|
||||
}
|
||||
}
|
||||
|
||||
// Заполнение данных
|
||||
int row = 4;
|
||||
foreach (var item in data)
|
||||
{
|
||||
var properties = item.GetType().GetProperties();
|
||||
if (properties.Count() != columns.Count)
|
||||
{
|
||||
throw new Exception("Количество полей объекта не соответствует количеству столбцов в таблице!");
|
||||
}
|
||||
|
||||
for (int i = 0; i < properties.Count(); i++)
|
||||
{
|
||||
int columnIndex = 0;
|
||||
var property = properties[i];
|
||||
var propertyValue = property.GetValue(item);
|
||||
if (propertyValue == null)
|
||||
{
|
||||
throw new Exception("Поле имеет пустое значение!");
|
||||
}
|
||||
|
||||
foreach (var column in columns)
|
||||
{
|
||||
if (column.FieldName == property.Name)
|
||||
{
|
||||
columnIndex = columns.IndexOf(column) + 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (columnIndex != 0)
|
||||
{
|
||||
worksheet.Cells[row, columnIndex] = propertyValue;
|
||||
}
|
||||
}
|
||||
|
||||
row++;
|
||||
}
|
||||
|
||||
// Границы таблицы
|
||||
for (int i = 2; i <= (data.Count() + 3); i++)
|
||||
{
|
||||
for (int j = 1; j <= columns.Count(); j++)
|
||||
{
|
||||
Excel.Range cell = (Excel.Range)worksheet.Cells[i, j];
|
||||
cell.BorderAround(true);
|
||||
}
|
||||
}
|
||||
|
||||
if (File.Exists(filepath))
|
||||
{
|
||||
File.Delete(filepath);
|
||||
}
|
||||
|
||||
excelApp.Application.ActiveWorkbook.SaveAs(filepath);
|
||||
workbook.Close(true);
|
||||
excelApp.Quit();
|
||||
|
||||
Marshal.ReleaseComObject(excelApp);
|
||||
}
|
||||
}
|
||||
}
|
24
WinFormsLibrary1/WinFormsLibrary1/ComponentWithBigText.Designer.cs
generated
Normal file
24
WinFormsLibrary1/WinFormsLibrary1/ComponentWithBigText.Designer.cs
generated
Normal file
@ -0,0 +1,24 @@
|
||||
namespace WinFormsLibrary1
|
||||
{
|
||||
partial class ComponentWithBigText
|
||||
{
|
||||
private System.ComponentModel.IContainer components = null;
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (disposing && (components != null))
|
||||
{
|
||||
components.Dispose();
|
||||
}
|
||||
base.Dispose(disposing);
|
||||
}
|
||||
|
||||
#region Код, автоматически созданный конструктором компонентов
|
||||
|
||||
private void InitializeComponent()
|
||||
{
|
||||
components = new System.ComponentModel.Container();
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
75
WinFormsLibrary1/WinFormsLibrary1/ComponentWithBigText.cs
Normal file
75
WinFormsLibrary1/WinFormsLibrary1/ComponentWithBigText.cs
Normal file
@ -0,0 +1,75 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Excel = Microsoft.Office.Interop.Excel;
|
||||
|
||||
namespace WinFormsLibrary1
|
||||
{
|
||||
public partial class ComponentWithBigText : Component
|
||||
{
|
||||
public ComponentWithBigText()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Конструктор
|
||||
/// </summary>
|
||||
/// <param name="container"></param>
|
||||
public ComponentWithBigText(IContainer container)
|
||||
{
|
||||
container.Add(this);
|
||||
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Создать документ
|
||||
/// </summary>
|
||||
/// <param name="filename"></param>
|
||||
/// <param name="title"></param>
|
||||
/// <param name="rows"></param>
|
||||
/// <exception cref="ArgumentNullException"></exception>
|
||||
public void CreateDocument(string filepath, string title, string[] rows)
|
||||
{
|
||||
if (string.IsNullOrEmpty(filepath))
|
||||
{
|
||||
throw new ArgumentNullException("Не указан путь к файлу!");
|
||||
}
|
||||
if (string.IsNullOrEmpty(title))
|
||||
{
|
||||
throw new ArgumentNullException("Не указан заголовок документа!");
|
||||
}
|
||||
if (rows == null || rows.Length == 0)
|
||||
{
|
||||
throw new ArgumentNullException("Массив с текстом не заполнен!");
|
||||
}
|
||||
|
||||
Excel.Application excelApp = new Excel.Application();
|
||||
Excel.Workbook workbook = excelApp.Workbooks.Add();
|
||||
Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Sheets[1];
|
||||
|
||||
worksheet.Cells[1, 1] = title;
|
||||
for (int index = 0; index < rows.Length; index++)
|
||||
{
|
||||
worksheet.Cells[index + 3, 1] = rows[index];
|
||||
}
|
||||
|
||||
if (File.Exists(filepath))
|
||||
{
|
||||
File.Delete(filepath);
|
||||
}
|
||||
|
||||
excelApp.Application.ActiveWorkbook.SaveAs(filepath);
|
||||
workbook.Close(true);
|
||||
excelApp.Quit();
|
||||
|
||||
Marshal.ReleaseComObject(excelApp);
|
||||
}
|
||||
}
|
||||
}
|
@ -25,7 +25,7 @@
|
||||
this.listBox.Name = "listBox";
|
||||
this.listBox.Size = new System.Drawing.Size(120, 95);
|
||||
this.listBox.TabIndex = 0;
|
||||
this.listBox.SelectedIndexChanged += new System.EventHandler(this.listBox_SelectedIndexChanged);
|
||||
//this.listBox.SelectedIndexChanged += new System.EventHandler(this.listBox_SelectedIndexChanged);
|
||||
//
|
||||
// ListBoxUserControl
|
||||
//
|
||||
|
21
WinFormsLibrary1/WinFormsLibrary1/Models/Column.cs
Normal file
21
WinFormsLibrary1/WinFormsLibrary1/Models/Column.cs
Normal file
@ -0,0 +1,21 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace WinFormsLibrary1.Models
|
||||
{
|
||||
public class Column
|
||||
{
|
||||
public string FieldName { get; set; } = string.Empty;
|
||||
public string Header { get; set; } = string.Empty;
|
||||
public int Width { get; set; }
|
||||
public Column(string fieldName, string header, int width)
|
||||
{
|
||||
FieldName = fieldName;
|
||||
Header = header;
|
||||
Width = width;
|
||||
}
|
||||
}
|
||||
}
|
19
WinFormsLibrary1/WinFormsLibrary1/Models/LegendPosition.cs
Normal file
19
WinFormsLibrary1/WinFormsLibrary1/Models/LegendPosition.cs
Normal file
@ -0,0 +1,19 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace WinFormsLibrary1.Models
|
||||
{
|
||||
public enum LegendPosition
|
||||
{
|
||||
Top,
|
||||
|
||||
Right,
|
||||
|
||||
Bottom,
|
||||
|
||||
Left,
|
||||
}
|
||||
}
|
17
WinFormsLibrary1/WinFormsLibrary1/Models/LineChartConfig.cs
Normal file
17
WinFormsLibrary1/WinFormsLibrary1/Models/LineChartConfig.cs
Normal file
@ -0,0 +1,17 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace WinFormsLibrary1.Models
|
||||
{
|
||||
public class LineChartConfig
|
||||
{
|
||||
public string Filepath { get; set; } = string.Empty;
|
||||
public string Header { get; set; } = string.Empty;
|
||||
public string ChartTitle { get; set; } = string.Empty;
|
||||
public Dictionary<string, List<int>> Values { get; set; } = new();
|
||||
public LegendPosition LegendPosition { get; set; }
|
||||
}
|
||||
}
|
19
WinFormsLibrary1/WinFormsLibrary1/Models/MergeCell.cs
Normal file
19
WinFormsLibrary1/WinFormsLibrary1/Models/MergeCell.cs
Normal file
@ -0,0 +1,19 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace WinFormsLibrary1.Models
|
||||
{
|
||||
public class MergeCell
|
||||
{
|
||||
public string Header;
|
||||
public int[] CellIndexes;
|
||||
public MergeCell(string header, int[] cellIndexes)
|
||||
{
|
||||
Header = header;
|
||||
CellIndexes = cellIndexes;
|
||||
}
|
||||
}
|
||||
}
|
@ -7,4 +7,8 @@
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Excel-DNA.Interop" Version="15.0.1" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
Loading…
Reference in New Issue
Block a user