Made excel diagram component
This commit is contained in:
parent
973a9d37d2
commit
64766eabc4
@ -0,0 +1,16 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace NevaevaLibrary.LogicalComponents
|
||||
{
|
||||
public enum DiagramLegendEnum
|
||||
{
|
||||
TopLeft = 0,
|
||||
TopRight = 1,
|
||||
BottomRight = 2,
|
||||
BottomLeft = 3,
|
||||
}
|
||||
}
|
36
NevaevaLibrary/NevaevaLibrary/LogicalComponents/WordDiagramComponent.Designer.cs
generated
Normal file
36
NevaevaLibrary/NevaevaLibrary/LogicalComponents/WordDiagramComponent.Designer.cs
generated
Normal file
@ -0,0 +1,36 @@
|
||||
namespace NevaevaLibrary.LogicalComponents
|
||||
{
|
||||
partial class WordDiagramComponent
|
||||
{
|
||||
/// <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
|
||||
}
|
||||
}
|
@ -0,0 +1,142 @@
|
||||
using Microsoft.Office.Interop.Excel;
|
||||
using Microsoft.Office.Interop.Word;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace NevaevaLibrary.LogicalComponents
|
||||
{
|
||||
public partial class WordDiagramComponent : Component
|
||||
{
|
||||
public WordDiagramComponent()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
public WordDiagramComponent(IContainer container)
|
||||
{
|
||||
container.Add(this);
|
||||
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
private object missing = System.Reflection.Missing.Value;
|
||||
|
||||
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 void 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);
|
||||
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 offsetX = 1;
|
||||
int offsetYMax = -1;
|
||||
foreach (var item in data)
|
||||
{
|
||||
string columnChar = GetExcelColumnName(offsetX);
|
||||
var cell = worksheet.get_Range(columnChar + "2", columnChar + "2");
|
||||
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);
|
||||
|
||||
int offsetY = 3;
|
||||
|
||||
foreach (var val in (value.GetValue(item) as IEnumerable))
|
||||
{
|
||||
cell = worksheet.get_Range(columnChar + offsetY, columnChar + offsetY);
|
||||
cell.Value2 = val;
|
||||
|
||||
offsetY++;
|
||||
}
|
||||
if (offsetY > offsetYMax) offsetYMax = offsetY;
|
||||
|
||||
offsetX++;
|
||||
}
|
||||
|
||||
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 endColumn = GetExcelColumnName(offsetX - 1);
|
||||
var range = worksheet.get_Range($"A2", endColumn + (offsetYMax - 1));
|
||||
chart.SetSourceData(range);
|
||||
chart.ChartType = XlChartType.xlLine;
|
||||
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);
|
||||
|
||||
chart.Export(path.Substring(0, path.Length - 5) + ".jpg", "JPG", false);
|
||||
|
||||
workbook.Close(SaveChanges: false);
|
||||
excelApp.Quit();
|
||||
|
||||
var winword = new Microsoft.Office.Interop.Word.Application();
|
||||
var document = winword.Documents.Add(ref missing, ref missing, ref missing, ref missing);
|
||||
document.PageSetup.RightMargin = 50;
|
||||
document.PageSetup.LeftMargin = 50;
|
||||
|
||||
var header = document.Content.Paragraphs.Add(Type.Missing);
|
||||
header.Range.Text = title;
|
||||
header.Format.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
|
||||
header.Range.Font.Name = "Times New Roman";
|
||||
header.Range.Font.Size = 22;
|
||||
header.Range.Font.Bold = 2;
|
||||
header.Format.SpaceAfter = 18;
|
||||
header.Range.InsertParagraphAfter();
|
||||
|
||||
document.Shapes.AddPicture(path.Substring(0, path.Length - 5) + ".jpg", Top: 200);
|
||||
|
||||
document.SaveAs(path, Type.Missing, Type.Missing, Type.Missing,
|
||||
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
|
||||
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
|
||||
Type.Missing, Type.Missing, Type.Missing, Type.Missing);
|
||||
document.Close(ref missing, ref missing, ref missing);
|
||||
document = null;
|
||||
winword.Quit(ref missing, ref missing, ref missing);
|
||||
}
|
||||
}
|
||||
}
|
@ -26,6 +26,15 @@
|
||||
<Isolated>false</Isolated>
|
||||
<EmbedInteropTypes>true</EmbedInteropTypes>
|
||||
</COMReference>
|
||||
<COMReference Include="Microsoft.Office.Interop.Excel">
|
||||
<WrapperTool>tlbimp</WrapperTool>
|
||||
<VersionMinor>9</VersionMinor>
|
||||
<VersionMajor>1</VersionMajor>
|
||||
<Guid>00020813-0000-0000-c000-000000000046</Guid>
|
||||
<Lcid>0</Lcid>
|
||||
<Isolated>false</Isolated>
|
||||
<EmbedInteropTypes>true</EmbedInteropTypes>
|
||||
</COMReference>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
20
NevaevaLibrary/TestApp/Department.cs
Normal file
20
NevaevaLibrary/TestApp/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 TestApp
|
||||
{
|
||||
public class Department
|
||||
{
|
||||
public string name;
|
||||
public List<int> sells;
|
||||
|
||||
public Department(string name, List<int> sells)
|
||||
{
|
||||
this.name = name;
|
||||
this.sells = sells;
|
||||
}
|
||||
}
|
||||
}
|
15
NevaevaLibrary/TestApp/FormTest.Designer.cs
generated
15
NevaevaLibrary/TestApp/FormTest.Designer.cs
generated
@ -44,6 +44,8 @@
|
||||
this.openFileDialog = new System.Windows.Forms.OpenFileDialog();
|
||||
this.buttonTable = new System.Windows.Forms.Button();
|
||||
this.wordTableComponent = new NevaevaLibrary.LogicalComponents.WordTableComponent(this.components);
|
||||
this.wordDiagramComponent = new NevaevaLibrary.LogicalComponents.WordDiagramComponent(this.components);
|
||||
this.buttonDiagram = new System.Windows.Forms.Button();
|
||||
this.SuspendLayout();
|
||||
//
|
||||
// comboBoxControl
|
||||
@ -166,11 +168,22 @@
|
||||
this.buttonTable.UseVisualStyleBackColor = true;
|
||||
this.buttonTable.Click += new System.EventHandler(this.buttonTable_Click);
|
||||
//
|
||||
// buttonDiagram
|
||||
//
|
||||
this.buttonDiagram.Location = new System.Drawing.Point(345, 319);
|
||||
this.buttonDiagram.Name = "buttonDiagram";
|
||||
this.buttonDiagram.Size = new System.Drawing.Size(145, 29);
|
||||
this.buttonDiagram.TabIndex = 12;
|
||||
this.buttonDiagram.Text = "Word (диаграмма)";
|
||||
this.buttonDiagram.UseVisualStyleBackColor = true;
|
||||
this.buttonDiagram.Click += new System.EventHandler(this.buttonDiagram_Click);
|
||||
//
|
||||
// FormTest
|
||||
//
|
||||
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F);
|
||||
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
|
||||
this.ClientSize = new System.Drawing.Size(1109, 363);
|
||||
this.Controls.Add(this.buttonDiagram);
|
||||
this.Controls.Add(this.buttonTable);
|
||||
this.Controls.Add(this.buttonWordText);
|
||||
this.Controls.Add(this.buttonGetSelectedList);
|
||||
@ -206,5 +219,7 @@
|
||||
private OpenFileDialog openFileDialog;
|
||||
private Button buttonTable;
|
||||
private NevaevaLibrary.LogicalComponents.WordTableComponent wordTableComponent;
|
||||
private NevaevaLibrary.LogicalComponents.WordDiagramComponent wordDiagramComponent;
|
||||
private Button buttonDiagram;
|
||||
}
|
||||
}
|
@ -100,5 +100,17 @@ namespace TestApp
|
||||
wordTableComponent.createWithTable(path, "header", merges, widths, headers, workers);
|
||||
MessageBox.Show("Готово!");
|
||||
}
|
||||
|
||||
private void buttonDiagram_Click(object sender, EventArgs e)
|
||||
{
|
||||
List<Department> departments = new List<Department>();
|
||||
|
||||
departments.Add(new Department("Dep 1", new List<int> { 330, 220, 400, 500 }));
|
||||
departments.Add(new Department("Dep 2", new List<int> { 400, 300, 302 }));
|
||||
departments.Add(new Department("Dep 3", new List<int> { 200, 220, 270 }));
|
||||
string path = AppDomain.CurrentDomain.BaseDirectory + "test3.docx";
|
||||
wordDiagramComponent.createWithDiagram(path, "test3", "Продажи", DiagramLegendEnum.TopRight, departments, "name", "sells");
|
||||
MessageBox.Show("Готово!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -66,4 +66,7 @@
|
||||
<metadata name="wordTableComponent.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
|
||||
<value>407, 17</value>
|
||||
</metadata>
|
||||
<metadata name="wordDiagramComponent.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
|
||||
<value>612, 17</value>
|
||||
</metadata>
|
||||
</root>
|
Loading…
x
Reference in New Issue
Block a user