PIbd-31_Putincev.D.M._COP_29/COP/WinForms/FormNoVisual.cs

153 lines
6.0 KiB
C#

using PutincevLibrary;
using PutincevLibrary.Info;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using PutincevLibrary.Enums;
using OfficeOpenXml.Table;
namespace WinForms
{
public partial class FormNoVisual : Form
{
public FormNoVisual()
{
InitializeComponent();
}
private void buttonCreateExcelFile_Click(object sender, EventArgs e)
{
string[] list = new string[listBoxImages.Items.Count];
for (int i = 0; i < listBoxImages.Items.Count; i++)
{
list[i] = listBoxImages.Items[i].ToString();
}
try
{
componentExcelWithImage1.CreateExcelWithImages(textBoxFilePath.Text, textBoxTitle.Text, list);
MessageBox.Show("Файл успешно создан", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show($"Ошибка при создании файла:\n{ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void buttonSetFilePath_Click(object sender, EventArgs e)
{
var filePath = string.Empty;
using (SaveFileDialog saveFileDialog = new SaveFileDialog())
{
saveFileDialog.InitialDirectory = "d:\\tmp";
saveFileDialog.Filter = "Excel files (*.xlsx)|*.xlsx|All files (*.*)|*.*";
saveFileDialog.FilterIndex = 1;
saveFileDialog.RestoreDirectory = true;
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
filePath = saveFileDialog.FileName;
}
}
if (!string.IsNullOrEmpty(filePath))
{
textBoxFilePath.Text = filePath;
}
else
{
textBoxFilePath.Text = string.Empty;
}
}
private void buttonAddImage_Click(object sender, EventArgs e)
{
var filePath = string.Empty;
using (OpenFileDialog openFileDialog = new OpenFileDialog())
{
openFileDialog.InitialDirectory = "d:\\tmp";
openFileDialog.Filter = "Image files (*.jpeg;*.jpg;*.png)|*.jpeg;*.jpg;*.png|All files (*.*)|*.*";
openFileDialog.FilterIndex = 1;
openFileDialog.RestoreDirectory = true;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
filePath = openFileDialog.FileName;
}
}
if (!string.IsNullOrEmpty(filePath))
{
listBoxImages.Items.Add(filePath);
}
}
private void buttonClearImages_Click(object sender, EventArgs e)
{
listBoxImages.Items.Clear();
}
private void buttonCreateExcelWithPieDiagram_Click(object sender, EventArgs e)
{
ComponentExcelWithPieDiagram chart = new();
LegendPosition legend = new();
var data = new List<DataItem>()
{
new DataItem() { Name = "Собаки", Value = 40 },
new DataItem() { Name = "Кошки", Value = 30 },
new DataItem() { Name = "Хомячки", Value = 20 },
new DataItem() { Name = "Люди", Value = 50 }
};
ExcelChartInfo info = new("E:\\testchart.xlsx", "My Document", "My Chart", legend, data);
try
{
chart.GenerateDocument(info);
MessageBox.Show("Сохарнено успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void buttonCreateExcelWithTable_Click(object sender, EventArgs e)
{
ComponentExcelWithTable table = new();
var data = new List<Employee2>
{
new Employee2 { Id = 1, Name = "Даниил", Surname = "Путинцев", Age = "20", Department = "IT", Position = "Уборщик" },
new Employee2 { Id = 2, Name = "Илья", Surname = "Родионов", Age = "19", Department = "Design", Position = "Работник" },
};
Dictionary<string, (List<(string, string)>, List<int>)> headers = new()
{
{ "ID", (new List<(string, string)> { ("Id", "Идентификатор") }, new List<int> { 30 }) },
{ "Личные данные", (new List<(string, string)> { ("Name", "Имя"), ("Surname", "Фамилия"), ("Age", "Возраст") }, new List<int> { 25, 25, 25 }) },
{ "Работа", (new List<(string, string)> { ("Department", "Отдел"), ("Position", "Должность") }, new List<int> { 25, 25 }) }
};
ExcelTableInfo<Employee2> info = new("E:\\table.xlsx", "My Document", data, headers);
try
{
table.GenerateDocument(info);
MessageBox.Show("Сохарнено успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
public class Employee2
{
public int? Id { get; set; }
public string? Name { get; set; } = string.Empty;
public string? Surname { get; set; } = string.Empty;
public string? Age { get; set; } = string.Empty;
public string? Department { get; set; } = string.Empty;
public string? Position { get; set; } = string.Empty;
}
}
}