2 лаба не работает!!!
This commit is contained in:
parent
6c2613df86
commit
2ba7165ccd
@ -7,4 +7,9 @@
|
|||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.Office.Interop.Excel" Version="15.0.4795.1001" />
|
||||||
|
<PackageReference Include="MicrosoftOfficeCore" Version="15.0.0" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
16
COP/Components/LogicalComponents/DiagramLegendEnum.cs
Normal file
16
COP/Components/LogicalComponents/DiagramLegendEnum.cs
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Components.LogicalComponents
|
||||||
|
{
|
||||||
|
public enum DiagramLegendEnum
|
||||||
|
{
|
||||||
|
TopLeft = 0,
|
||||||
|
TopRight = 1,
|
||||||
|
BottomRight = 2,
|
||||||
|
BottomLeft = 3,
|
||||||
|
}
|
||||||
|
}
|
36
COP/Components/LogicalComponents/ExcelDiagramComponent.Designer.cs
generated
Normal file
36
COP/Components/LogicalComponents/ExcelDiagramComponent.Designer.cs
generated
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
namespace Components.LogicalComponents
|
||||||
|
{
|
||||||
|
partial class ExcelDiagramComponent
|
||||||
|
{
|
||||||
|
/// <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
|
||||||
|
}
|
||||||
|
}
|
117
COP/Components/LogicalComponents/ExcelDiagramComponent.cs
Normal file
117
COP/Components/LogicalComponents/ExcelDiagramComponent.cs
Normal file
@ -0,0 +1,117 @@
|
|||||||
|
using Microsoft.Office.Interop.Excel;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.ComponentModel;
|
||||||
|
using System.Diagnostics;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Reflection;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Components.LogicalComponents
|
||||||
|
{
|
||||||
|
public partial class ExcelDiagramComponent : Component
|
||||||
|
{
|
||||||
|
public ExcelDiagramComponent()
|
||||||
|
{
|
||||||
|
InitializeComponent();
|
||||||
|
}
|
||||||
|
|
||||||
|
public ExcelDiagramComponent(IContainer container)
|
||||||
|
{
|
||||||
|
container.Add(this);
|
||||||
|
|
||||||
|
InitializeComponent();
|
||||||
|
}
|
||||||
|
public bool createWithDiagram<T>(string path, string title, string diagramTitle, DiagramLegendEnum diagramLegendAnchor, List<T> data, string seriesNameField, string valueField)
|
||||||
|
{
|
||||||
|
var excelApp = new Microsoft.Office.Interop.Excel.Application { SheetsInNewWorkbook = 1 };
|
||||||
|
Workbook workbook = excelApp.Workbooks.Add(Type.Missing);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Worksheet worksheet = (Worksheet)workbook.Worksheets.get_Item(1);
|
||||||
|
|
||||||
|
FieldInfo? seriesName = typeof(T).GetField(seriesNameField);
|
||||||
|
FieldInfo? value = typeof(T).GetField(valueField);
|
||||||
|
if (seriesName == null || value == null) throw new ArgumentException("Переданного поля не существует");
|
||||||
|
int columnCount = 2;
|
||||||
|
foreach (var item in data)
|
||||||
|
{
|
||||||
|
var cell = worksheet.get_Range("A" + columnCount, "A" + columnCount);
|
||||||
|
cell.Font.Size = 14;
|
||||||
|
cell.Font.Name = "Times New Roman";
|
||||||
|
cell.ColumnWidth = 8;
|
||||||
|
cell.RowHeight = 25;
|
||||||
|
cell.HorizontalAlignment = Constants.xlCenter;
|
||||||
|
cell.VerticalAlignment = Constants.xlCenter;
|
||||||
|
cell.Value2 = seriesName.GetValue(item);
|
||||||
|
|
||||||
|
cell = worksheet.get_Range("B" + columnCount, "B" + columnCount);
|
||||||
|
cell.Font.Size = 14;
|
||||||
|
cell.Font.Name = "Times New Roman";
|
||||||
|
cell.ColumnWidth = 8;
|
||||||
|
cell.RowHeight = 25;
|
||||||
|
cell.HorizontalAlignment = Constants.xlCenter;
|
||||||
|
cell.VerticalAlignment = Constants.xlCenter;
|
||||||
|
cell.Value2 = value.GetValue(item);
|
||||||
|
|
||||||
|
columnCount++;
|
||||||
|
}
|
||||||
|
|
||||||
|
//header
|
||||||
|
var excelcells = worksheet.get_Range("A1", "A1");
|
||||||
|
excelcells.Font.Bold = true;
|
||||||
|
excelcells.Font.Size = 14;
|
||||||
|
excelcells.Font.Name = "Times New Roman";
|
||||||
|
excelcells.ColumnWidth = 8;
|
||||||
|
excelcells.RowHeight = 25;
|
||||||
|
excelcells.HorizontalAlignment = Constants.xlCenter;
|
||||||
|
excelcells.VerticalAlignment = Constants.xlCenter;
|
||||||
|
excelcells.Value2 = title;
|
||||||
|
|
||||||
|
var charts = worksheet.ChartObjects() as ChartObjects;
|
||||||
|
int chartWidth = 300;
|
||||||
|
int chartHeight = 300;
|
||||||
|
var chartObject = charts.Add(250, 10, chartWidth, chartHeight);
|
||||||
|
var chart = chartObject.Chart;
|
||||||
|
var range = worksheet.get_Range($"A2", $"B{columnCount - 1}");
|
||||||
|
chart.SetSourceData(range);
|
||||||
|
chart.ChartType = XlChartType.xlPie;
|
||||||
|
switch (diagramLegendAnchor)
|
||||||
|
{
|
||||||
|
case DiagramLegendEnum.TopLeft:
|
||||||
|
chart.Legend.Top = 0;
|
||||||
|
chart.Legend.Left = 0;
|
||||||
|
break;
|
||||||
|
case DiagramLegendEnum.TopRight:
|
||||||
|
chart.Legend.Top = 0;
|
||||||
|
chart.Legend.Left = chartWidth - chart.Legend.Width;
|
||||||
|
break;
|
||||||
|
case DiagramLegendEnum.BottomLeft:
|
||||||
|
chart.Legend.Top = chartHeight - chart.Legend.Height;
|
||||||
|
chart.Legend.Left = 0;
|
||||||
|
break;
|
||||||
|
case DiagramLegendEnum.BottomRight:
|
||||||
|
chart.Legend.Top = chartHeight - chart.Legend.Height;
|
||||||
|
chart.Legend.Left = chartWidth - chart.Legend.Width;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
chart.ChartWizard(Source: range, Title: diagramTitle);
|
||||||
|
|
||||||
|
object missing = System.Reflection.Missing.Value;
|
||||||
|
workbook.SaveAs(path, XlFileFormat.xlOpenXMLWorkbook, missing, missing, false, false, XlSaveAsAccessMode.xlNoChange,
|
||||||
|
XlSaveConflictResolution.xlUserResolution, true, missing, missing, missing);
|
||||||
|
workbook.Close();
|
||||||
|
excelApp.Quit();
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
catch (Exception)
|
||||||
|
{
|
||||||
|
workbook.Close();
|
||||||
|
excelApp.Quit();
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
22
COP/Components/LogicalComponents/ExcelImageInfo.cs
Normal file
22
COP/Components/LogicalComponents/ExcelImageInfo.cs
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Components.LogicalComponents
|
||||||
|
{
|
||||||
|
public class ExcelImageInfo
|
||||||
|
{
|
||||||
|
public ExcelImageInfo(string path, string title, string[] imagePaths)
|
||||||
|
{
|
||||||
|
this.path = path;
|
||||||
|
this.title = title;
|
||||||
|
this.imagePaths = imagePaths;
|
||||||
|
}
|
||||||
|
|
||||||
|
public string path;
|
||||||
|
public string title;
|
||||||
|
public string[] imagePaths;
|
||||||
|
}
|
||||||
|
}
|
36
COP/Components/LogicalComponents/ExcelImagesComponent.Designer.cs
generated
Normal file
36
COP/Components/LogicalComponents/ExcelImagesComponent.Designer.cs
generated
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
namespace Components.LogicalComponents
|
||||||
|
{
|
||||||
|
partial class ExcelImagesComponent
|
||||||
|
{
|
||||||
|
/// <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
|
||||||
|
}
|
||||||
|
}
|
73
COP/Components/LogicalComponents/ExcelImagesComponent.cs
Normal file
73
COP/Components/LogicalComponents/ExcelImagesComponent.cs
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.ComponentModel;
|
||||||
|
using System.Diagnostics;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Microsoft.Office.Interop.Excel;
|
||||||
|
using Microsoft.Office.Core;
|
||||||
|
|
||||||
|
namespace Components.LogicalComponents
|
||||||
|
{
|
||||||
|
public partial class ExcelImagesComponent : Component
|
||||||
|
{
|
||||||
|
public ExcelImagesComponent()
|
||||||
|
{
|
||||||
|
InitializeComponent();
|
||||||
|
}
|
||||||
|
|
||||||
|
public ExcelImagesComponent(IContainer container)
|
||||||
|
{
|
||||||
|
container.Add(this);
|
||||||
|
|
||||||
|
InitializeComponent();
|
||||||
|
}
|
||||||
|
public bool createWithImages(ExcelImageInfo info)
|
||||||
|
{
|
||||||
|
var excelApp = new Microsoft.Office.Interop.Excel.Application { SheetsInNewWorkbook = 1 };
|
||||||
|
Workbook workbook = excelApp.Workbooks.Add(Type.Missing);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
//create
|
||||||
|
Worksheet worksheet = (Worksheet)workbook.Worksheets.get_Item(1);
|
||||||
|
|
||||||
|
//header
|
||||||
|
var excelcells = worksheet.get_Range("A1", "D1");
|
||||||
|
excelcells.Merge(Type.Missing);
|
||||||
|
excelcells.Font.Bold = true;
|
||||||
|
excelcells.Font.Size = 14;
|
||||||
|
excelcells.Font.Name = "Times New Roman";
|
||||||
|
excelcells.ColumnWidth = 8;
|
||||||
|
excelcells.RowHeight = 25;
|
||||||
|
excelcells.HorizontalAlignment = Constants.xlCenter;
|
||||||
|
excelcells.VerticalAlignment = Constants.xlCenter;
|
||||||
|
excelcells.Value2 = info.title;
|
||||||
|
|
||||||
|
int topOffset = 25;
|
||||||
|
foreach (string path in info.imagePaths)
|
||||||
|
{
|
||||||
|
Bitmap bm = new Bitmap(path);
|
||||||
|
worksheet.Shapes.AddPicture2(path, MsoTriState.msoFalse, MsoTriState.msoCTrue, 0, topOffset, bm.Width, bm.Height, MsoPictureCompress.msoPictureCompressFalse);
|
||||||
|
topOffset += bm.Height;
|
||||||
|
bm.Dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
//save
|
||||||
|
object missing = System.Reflection.Missing.Value;
|
||||||
|
workbook.SaveAs(info.path, XlFileFormat.xlOpenXMLWorkbook, missing, missing, false, false, XlSaveAsAccessMode.xlNoChange,
|
||||||
|
XlSaveConflictResolution.xlUserResolution, true, missing, missing, missing);
|
||||||
|
workbook.Close();
|
||||||
|
excelApp.Quit();
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
catch (Exception)
|
||||||
|
{
|
||||||
|
workbook.Close();
|
||||||
|
excelApp.Quit();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
36
COP/Components/LogicalComponents/ExcelTableComponent.Designer.cs
generated
Normal file
36
COP/Components/LogicalComponents/ExcelTableComponent.Designer.cs
generated
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
namespace Components.LogicalComponents
|
||||||
|
{
|
||||||
|
partial class ExcelTableComponent
|
||||||
|
{
|
||||||
|
/// <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
|
||||||
|
}
|
||||||
|
}
|
191
COP/Components/LogicalComponents/ExcelTableComponent.cs
Normal file
191
COP/Components/LogicalComponents/ExcelTableComponent.cs
Normal file
@ -0,0 +1,191 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.ComponentModel;
|
||||||
|
using System.Diagnostics;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Microsoft.Office.Interop.Excel;
|
||||||
|
using System.Reflection;
|
||||||
|
|
||||||
|
namespace Components.LogicalComponents
|
||||||
|
{
|
||||||
|
public partial class ExcelTableComponent : Component
|
||||||
|
{
|
||||||
|
public ExcelTableComponent()
|
||||||
|
{
|
||||||
|
InitializeComponent();
|
||||||
|
}
|
||||||
|
|
||||||
|
public ExcelTableComponent(IContainer container)
|
||||||
|
{
|
||||||
|
container.Add(this);
|
||||||
|
|
||||||
|
InitializeComponent();
|
||||||
|
}
|
||||||
|
private string GetExcelColumnName(int columnNumber)
|
||||||
|
{
|
||||||
|
string columnName = "";
|
||||||
|
|
||||||
|
while (columnNumber > 0)
|
||||||
|
{
|
||||||
|
int modulo = (columnNumber - 1) % 26;
|
||||||
|
columnName = Convert.ToChar('A' + modulo) + columnName;
|
||||||
|
columnNumber = (columnNumber - modulo) / 26;
|
||||||
|
}
|
||||||
|
|
||||||
|
return columnName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool createWithTable<T>(string path, string title, List<(int, int)> merges, List<int> heights, List<(string, string)> headers, List<T> items)
|
||||||
|
{
|
||||||
|
if (merges.Count == 0 || heights.Count == 0 || headers.Count == 0 || items.Count == 0) throw new ArgumentException("Недостаточно данных");
|
||||||
|
int[] cellsArray = new int[heights.Count];
|
||||||
|
foreach (var merge in merges)
|
||||||
|
{
|
||||||
|
if (merge.Item1 >= merge.Item2) throw new ArgumentException("Неправильно заполнены объединения строк");
|
||||||
|
for (int i = merge.Item1; i < merge.Item2; i++)
|
||||||
|
{
|
||||||
|
cellsArray[i]++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
foreach (int cell in cellsArray)
|
||||||
|
{
|
||||||
|
if (cell > 1) throw new ArgumentException("Объединения заходят друг на друга");
|
||||||
|
}
|
||||||
|
|
||||||
|
var excelApp = new Microsoft.Office.Interop.Excel.Application { SheetsInNewWorkbook = 1 };
|
||||||
|
Workbook workbook = excelApp.Workbooks.Add(Type.Missing);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
//create
|
||||||
|
Worksheet worksheet = (Worksheet)workbook.Worksheets.get_Item(1);
|
||||||
|
|
||||||
|
//header
|
||||||
|
var excelcells = worksheet.get_Range("A1", "A1");
|
||||||
|
excelcells.Font.Bold = true;
|
||||||
|
excelcells.Font.Size = 14;
|
||||||
|
excelcells.Font.Name = "Times New Roman";
|
||||||
|
excelcells.ColumnWidth = 8;
|
||||||
|
excelcells.RowHeight = 25;
|
||||||
|
excelcells.HorizontalAlignment = Constants.xlCenter;
|
||||||
|
excelcells.VerticalAlignment = Constants.xlCenter;
|
||||||
|
excelcells.Value2 = title;
|
||||||
|
|
||||||
|
//checks
|
||||||
|
List<Microsoft.Office.Interop.Excel.Range> ranges = new List<Microsoft.Office.Interop.Excel.Range>();
|
||||||
|
foreach (var merge in merges)
|
||||||
|
{
|
||||||
|
ranges.Add(worksheet.get_Range("A" + (merge.Item1 + 2), "A" + (merge.Item2 + 2)));
|
||||||
|
}
|
||||||
|
|
||||||
|
int rangeIndex = 0;
|
||||||
|
int headerIndex = 0;
|
||||||
|
List<FieldInfo> cellFields = new List<FieldInfo>();
|
||||||
|
var type = typeof(T);
|
||||||
|
for (int i = 0; i < heights.Count; i++)
|
||||||
|
{
|
||||||
|
if (cellsArray[i] == 1)
|
||||||
|
{
|
||||||
|
//work with merge
|
||||||
|
if (!string.IsNullOrEmpty(headers[headerIndex].Item1)) throw new ArgumentException("Заголовки и объединения строк не совпадают");
|
||||||
|
|
||||||
|
var groupRange = ranges[rangeIndex];
|
||||||
|
groupRange.Merge(Type.Missing);
|
||||||
|
groupRange.Font.Bold = true;
|
||||||
|
groupRange.Font.Size = 14;
|
||||||
|
groupRange.Font.Name = "Times New Roman";
|
||||||
|
groupRange.ColumnWidth = 20;
|
||||||
|
groupRange.HorizontalAlignment = Constants.xlCenter;
|
||||||
|
groupRange.VerticalAlignment = Constants.xlCenter;
|
||||||
|
groupRange.Value2 = headers[headerIndex].Item2;
|
||||||
|
headerIndex++;
|
||||||
|
|
||||||
|
//work with cells in merge
|
||||||
|
for (; i <= merges[rangeIndex].Item2; i++)
|
||||||
|
{
|
||||||
|
//work with cell
|
||||||
|
if (string.IsNullOrEmpty(headers[headerIndex].Item1)) throw new ArgumentException("Заголовки и объединения строк не совпадают");
|
||||||
|
var field = type.GetField(headers[headerIndex].Item1);
|
||||||
|
if (field == null) throw new ArgumentException("В заголовках указано поле, которого нет в переданном классе");
|
||||||
|
//format header
|
||||||
|
var range = worksheet.get_Range("B" + (i + 2), "B" + (i + 2));
|
||||||
|
range.Font.Bold = true;
|
||||||
|
range.Font.Size = 14;
|
||||||
|
range.Font.Name = "Times New Roman";
|
||||||
|
range.ColumnWidth = 20;
|
||||||
|
range.RowHeight = heights[i];
|
||||||
|
range.HorizontalAlignment = Constants.xlCenter;
|
||||||
|
range.VerticalAlignment = Constants.xlCenter;
|
||||||
|
range.Value2 = headers[headerIndex].Item2;
|
||||||
|
|
||||||
|
cellFields.Add(field);
|
||||||
|
headerIndex++;
|
||||||
|
}
|
||||||
|
i--;
|
||||||
|
rangeIndex++;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
//work with cell
|
||||||
|
if (string.IsNullOrEmpty(headers[headerIndex].Item1)) throw new ArgumentException("Заголовки и объединения строк не совпадают");
|
||||||
|
var field = type.GetField(headers[headerIndex].Item1);
|
||||||
|
if (field == null) throw new ArgumentException("В заголовках указано поле, которого нет в переданном классе");
|
||||||
|
//format header
|
||||||
|
var range = worksheet.get_Range("A" + (i + 2), "B" + (i + 2));
|
||||||
|
range.Merge(Type.Missing);
|
||||||
|
range.Font.Bold = true;
|
||||||
|
range.Font.Size = 14;
|
||||||
|
range.Font.Name = "Times New Roman";
|
||||||
|
range.ColumnWidth = 40;
|
||||||
|
range.RowHeight = heights[i];
|
||||||
|
range.HorizontalAlignment = Constants.xlCenter;
|
||||||
|
range.VerticalAlignment = Constants.xlCenter;
|
||||||
|
range.Value2 = headers[headerIndex].Item2;
|
||||||
|
|
||||||
|
cellFields.Add(field);
|
||||||
|
headerIndex++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int columnNum = 3;
|
||||||
|
foreach (T item in items)
|
||||||
|
{
|
||||||
|
string column = GetExcelColumnName(columnNum);
|
||||||
|
int rowNum = 2;
|
||||||
|
foreach (var cellField in cellFields)
|
||||||
|
{
|
||||||
|
var range = worksheet.get_Range(column + rowNum, column + rowNum);
|
||||||
|
range.Font.Size = 14;
|
||||||
|
range.Font.Name = "Times New Roman";
|
||||||
|
range.ColumnWidth = 20;
|
||||||
|
range.HorizontalAlignment = Constants.xlCenter;
|
||||||
|
range.VerticalAlignment = Constants.xlCenter;
|
||||||
|
range.Value2 = cellField.FieldType == typeof(bool) ? ((bool)cellField.GetValue(item) ? "Да" : "Нет") : cellField.GetValue(item);
|
||||||
|
|
||||||
|
rowNum++;
|
||||||
|
}
|
||||||
|
columnNum++;
|
||||||
|
}
|
||||||
|
var borderRange = worksheet.get_Range("A2", GetExcelColumnName(columnNum - 1) + (headerIndex - 1));
|
||||||
|
borderRange.Borders.LineStyle = XlLineStyle.xlContinuous;
|
||||||
|
borderRange.Borders.Weight = 2d;
|
||||||
|
|
||||||
|
//save
|
||||||
|
object missing = System.Reflection.Missing.Value;
|
||||||
|
workbook.SaveAs(path, XlFileFormat.xlOpenXMLWorkbook, missing, missing, false, false, XlSaveAsAccessMode.xlNoChange,
|
||||||
|
XlSaveConflictResolution.xlUserResolution, true, missing, missing, missing);
|
||||||
|
workbook.Close();
|
||||||
|
excelApp.Quit();
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
workbook.Close();
|
||||||
|
excelApp.Quit();
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
20
COP/WinFormsTest/Department.cs
Normal file
20
COP/WinFormsTest/Department.cs
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace WinFormsTest
|
||||||
|
{
|
||||||
|
public class Department
|
||||||
|
{
|
||||||
|
public string name;
|
||||||
|
public int sells;
|
||||||
|
|
||||||
|
public Department(string name, int sells)
|
||||||
|
{
|
||||||
|
this.name = name;
|
||||||
|
this.sells = sells;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
42
COP/WinFormsTest/FormTest.Designer.cs
generated
42
COP/WinFormsTest/FormTest.Designer.cs
generated
@ -42,6 +42,10 @@
|
|||||||
this.buttonGetValue = new System.Windows.Forms.Button();
|
this.buttonGetValue = new System.Windows.Forms.Button();
|
||||||
this.buttonGetIndex = new System.Windows.Forms.Button();
|
this.buttonGetIndex = new System.Windows.Forms.Button();
|
||||||
this.buttonSetIndex = new System.Windows.Forms.Button();
|
this.buttonSetIndex = new System.Windows.Forms.Button();
|
||||||
|
this.buttonExcelDiagram = new System.Windows.Forms.Button();
|
||||||
|
this.buttonExcelTable = new System.Windows.Forms.Button();
|
||||||
|
this.buttonExcelImages = new System.Windows.Forms.Button();
|
||||||
|
this.openFileDialog = new System.Windows.Forms.OpenFileDialog();
|
||||||
this.SuspendLayout();
|
this.SuspendLayout();
|
||||||
//
|
//
|
||||||
// userCheckedListBox
|
// userCheckedListBox
|
||||||
@ -182,11 +186,45 @@
|
|||||||
this.buttonSetIndex.UseVisualStyleBackColor = true;
|
this.buttonSetIndex.UseVisualStyleBackColor = true;
|
||||||
this.buttonSetIndex.Click += new System.EventHandler(this.buttonSetIndex_Click);
|
this.buttonSetIndex.Click += new System.EventHandler(this.buttonSetIndex_Click);
|
||||||
//
|
//
|
||||||
|
// buttonExcelDiagram
|
||||||
|
//
|
||||||
|
this.buttonExcelDiagram.Location = new System.Drawing.Point(12, 407);
|
||||||
|
this.buttonExcelDiagram.Name = "buttonExcelDiagram";
|
||||||
|
this.buttonExcelDiagram.Size = new System.Drawing.Size(150, 29);
|
||||||
|
this.buttonExcelDiagram.TabIndex = 19;
|
||||||
|
this.buttonExcelDiagram.Text = "Excel (Диаграмма)";
|
||||||
|
this.buttonExcelDiagram.UseVisualStyleBackColor = true;
|
||||||
|
//
|
||||||
|
// buttonExcelTable
|
||||||
|
//
|
||||||
|
this.buttonExcelTable.Location = new System.Drawing.Point(168, 362);
|
||||||
|
this.buttonExcelTable.Name = "buttonExcelTable";
|
||||||
|
this.buttonExcelTable.Size = new System.Drawing.Size(128, 29);
|
||||||
|
this.buttonExcelTable.TabIndex = 18;
|
||||||
|
this.buttonExcelTable.Text = "Excel (Таблица)";
|
||||||
|
this.buttonExcelTable.UseVisualStyleBackColor = true;
|
||||||
|
//
|
||||||
|
// buttonExcelImages
|
||||||
|
//
|
||||||
|
this.buttonExcelImages.Location = new System.Drawing.Point(12, 362);
|
||||||
|
this.buttonExcelImages.Name = "buttonExcelImages";
|
||||||
|
this.buttonExcelImages.Size = new System.Drawing.Size(150, 29);
|
||||||
|
this.buttonExcelImages.TabIndex = 17;
|
||||||
|
this.buttonExcelImages.Text = "Excel (картинки)";
|
||||||
|
this.buttonExcelImages.UseVisualStyleBackColor = true;
|
||||||
|
//
|
||||||
|
// openFileDialog
|
||||||
|
//
|
||||||
|
this.openFileDialog.FileName = "openFileDialog";
|
||||||
|
//
|
||||||
// FormTest
|
// FormTest
|
||||||
//
|
//
|
||||||
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F);
|
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F);
|
||||||
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
|
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
|
||||||
this.ClientSize = new System.Drawing.Size(850, 450);
|
this.ClientSize = new System.Drawing.Size(850, 450);
|
||||||
|
this.Controls.Add(this.buttonExcelDiagram);
|
||||||
|
this.Controls.Add(this.buttonExcelTable);
|
||||||
|
this.Controls.Add(this.buttonExcelImages);
|
||||||
this.Controls.Add(this.buttonSetIndex);
|
this.Controls.Add(this.buttonSetIndex);
|
||||||
this.Controls.Add(this.buttonGetIndex);
|
this.Controls.Add(this.buttonGetIndex);
|
||||||
this.Controls.Add(this.buttonGetValue);
|
this.Controls.Add(this.buttonGetValue);
|
||||||
@ -223,5 +261,9 @@
|
|||||||
private Button buttonGetValue;
|
private Button buttonGetValue;
|
||||||
private Button buttonGetIndex;
|
private Button buttonGetIndex;
|
||||||
private Button buttonSetIndex;
|
private Button buttonSetIndex;
|
||||||
|
private Button buttonExcelDiagram;
|
||||||
|
private Button buttonExcelTable;
|
||||||
|
private Button buttonExcelImages;
|
||||||
|
private OpenFileDialog openFileDialog;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,4 +1,5 @@
|
|||||||
using Components.Exceptions;
|
using Components.Exceptions;
|
||||||
|
using Components.LogicalComponents;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.ComponentModel;
|
using System.ComponentModel;
|
||||||
@ -133,5 +134,53 @@ namespace WinFormsTest
|
|||||||
{
|
{
|
||||||
userTreeView.SelectedNodeIndex = 0;
|
userTreeView.SelectedNodeIndex = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void buttonExcelImages_Click(object sender, EventArgs e)
|
||||||
|
{
|
||||||
|
var res = openFileDialog.ShowDialog(this);
|
||||||
|
if (res != DialogResult.OK) return;
|
||||||
|
var files = openFileDialog.FileNames;
|
||||||
|
openFileDialog.Dispose();
|
||||||
|
List<Bitmap> images = new List<Bitmap>();
|
||||||
|
string path = AppDomain.CurrentDomain.BaseDirectory + "test.xlsx";
|
||||||
|
//if (excelImagesComponent.createWithImages(new ExcelImageInfo(path, "Header", files))) MessageBox.Show("Успех!");
|
||||||
|
//else MessageBox.Show("Ошибка, проверьте консоль");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void buttonExcelTable_Click(object sender, EventArgs e)
|
||||||
|
{
|
||||||
|
List<OfficeWorker> workers = new List<OfficeWorker>();
|
||||||
|
|
||||||
|
workers.Add(new OfficeWorker(1, "Иванов", "Иван", 20, "Отдел продаж", "Бухгалтер", 25, "+7(834)234-03-49"));
|
||||||
|
workers.Add(new OfficeWorker(1, "Петров", "Петр", 25, "Отдел продаж", "Менеджер", 20, "+7(834)123-03-49"));
|
||||||
|
workers.Add(new OfficeWorker(1, "Сидоров", "Сергей", 27, "Отдел кадров", "HR", 2, "+7(834)593-03-49", true));
|
||||||
|
string path = AppDomain.CurrentDomain.BaseDirectory + "test2.xlsx";
|
||||||
|
List<(int, int)> merges = new List<(int, int)>();
|
||||||
|
merges.Add((1, 3));
|
||||||
|
merges.Add((4, 6));
|
||||||
|
List<int> heights = Enumerable.Repeat(20, 9).ToList();
|
||||||
|
|
||||||
|
List<(string, string)> headers = new List<(string, string)> { ("id", "id"), ("", "Личные данные"),
|
||||||
|
("lastName", "Фамилия"), ("firstName", "Имя"),
|
||||||
|
("age", "Возраст"), ("", "Работа"),
|
||||||
|
("department", "Отдел"), ("position", "Должность"),
|
||||||
|
("boxNumber", "Номер бокса"), ("phoneNumber", "Телефон"),
|
||||||
|
("isInVacation", "В отпуске"), };
|
||||||
|
|
||||||
|
//if (excelTableComponent.createWithTable(path, "test2", merges, heights, headers, workers)) MessageBox.Show("Успех");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void buttonExcelDiagram_Click(object sender, EventArgs e)
|
||||||
|
{
|
||||||
|
List<Department> departments = new List<Department>();
|
||||||
|
|
||||||
|
departments.Add(new Department("Dep 1", 330));
|
||||||
|
departments.Add(new Department("Dep 2", 500));
|
||||||
|
departments.Add(new Department("Dep 3", 170));
|
||||||
|
string path = AppDomain.CurrentDomain.BaseDirectory + "test3.xlsx";
|
||||||
|
//if (excelDiagramComponent.createWithDiagram(path, "test3", "Продажи", DiagramLegendEnum.TopRight, departments, "name", "sells")) MessageBox.Show("Успех");
|
||||||
|
//else MessageBox.Show("Fail :(");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,64 +1,4 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<root>
|
||||||
<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: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:import namespace="http://www.w3.org/XML/1998/namespace" />
|
||||||
<xsd:element name="root" msdata:IsDataSet="true">
|
<xsd:element name="root" msdata:IsDataSet="true">
|
||||||
@ -117,4 +57,7 @@
|
|||||||
<resheader name="writer">
|
<resheader name="writer">
|
||||||
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
||||||
</resheader>
|
</resheader>
|
||||||
|
<metadata name="openFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
|
||||||
|
<value>17, 17</value>
|
||||||
|
</metadata>
|
||||||
</root>
|
</root>
|
34
COP/WinFormsTest/OfficeWorker.cs
Normal file
34
COP/WinFormsTest/OfficeWorker.cs
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace WinFormsTest
|
||||||
|
{
|
||||||
|
public class OfficeWorker
|
||||||
|
{
|
||||||
|
public OfficeWorker(int id, string lastName, string firstName, int age, string department, string position, int boxNumber, string phoneNumber, bool isInVacation = false)
|
||||||
|
{
|
||||||
|
this.id = id;
|
||||||
|
this.lastName = lastName;
|
||||||
|
this.firstName = firstName;
|
||||||
|
this.age = age;
|
||||||
|
this.department = department;
|
||||||
|
this.position = position;
|
||||||
|
this.boxNumber = boxNumber;
|
||||||
|
this.phoneNumber = phoneNumber;
|
||||||
|
this.isInVacation = isInVacation;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int id;
|
||||||
|
public string lastName;
|
||||||
|
public string firstName;
|
||||||
|
public int age;
|
||||||
|
public string department;
|
||||||
|
public string position;
|
||||||
|
public int boxNumber;
|
||||||
|
public string phoneNumber;
|
||||||
|
public bool isInVacation;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user