2023-10-12 00:24:32 +04:00
|
|
|
|
using Aspose.Words.Drawing.Charts;
|
|
|
|
|
using Aspose.Words;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using Aspose.Words.Drawing;
|
2023-10-14 10:14:58 +04:00
|
|
|
|
using DocumentFormat.OpenXml.Office2010.Excel;
|
2023-10-12 00:24:32 +04:00
|
|
|
|
|
|
|
|
|
namespace COPWinForms
|
|
|
|
|
{
|
|
|
|
|
public partial class ComponentWord3 : Component
|
|
|
|
|
{
|
|
|
|
|
private string _fileName;
|
|
|
|
|
public string FileName
|
|
|
|
|
{
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(value))
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (!value.EndsWith(".docx"))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("No docx file");
|
|
|
|
|
}
|
|
|
|
|
_fileName = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ComponentWord3()
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ComponentWord3(IContainer container)
|
|
|
|
|
{
|
|
|
|
|
container.Add(this);
|
|
|
|
|
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-12 22:42:25 +04:00
|
|
|
|
public void CreateLineChart(DiagramWord diagramWord)
|
2023-10-12 00:24:32 +04:00
|
|
|
|
{
|
2023-10-14 10:14:58 +04:00
|
|
|
|
// Проверка наличия заголовка, пути и названия диаграммы
|
2023-10-12 22:42:25 +04:00
|
|
|
|
if (string.IsNullOrEmpty(diagramWord.ChartTitle) || string.IsNullOrEmpty(diagramWord.FilePath) || string.IsNullOrEmpty(diagramWord.DocumentTitle))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("Chart title is null or empty");
|
|
|
|
|
}
|
2023-10-12 00:24:32 +04:00
|
|
|
|
// Создание документа
|
|
|
|
|
Document document = new Document();
|
|
|
|
|
DocumentBuilder builder = new DocumentBuilder(document);
|
|
|
|
|
|
2023-10-12 22:42:25 +04:00
|
|
|
|
Style titleStyle = builder.Document.Styles.Add(StyleType.Paragraph, "Title");
|
|
|
|
|
titleStyle.Font.Size = 16;
|
|
|
|
|
titleStyle.Font.Bold = true;
|
|
|
|
|
|
|
|
|
|
// Установка заголовка документа
|
|
|
|
|
builder.ParagraphFormat.Style = titleStyle;
|
|
|
|
|
builder.Writeln(diagramWord.DocumentTitle);
|
2023-10-12 00:24:32 +04:00
|
|
|
|
|
|
|
|
|
// Вставка диаграммы
|
|
|
|
|
Shape chartShape = builder.InsertChart(ChartType.Line, 400, 300);
|
|
|
|
|
Chart chart = chartShape.Chart;
|
|
|
|
|
|
|
|
|
|
// Настройка заголовка диаграммы
|
|
|
|
|
ChartTitle title = chart.Title;
|
2023-10-12 22:42:25 +04:00
|
|
|
|
title.Text = diagramWord.ChartTitle;
|
2023-10-12 00:24:32 +04:00
|
|
|
|
|
|
|
|
|
ChartSeriesCollection seriesColl = chart.Series;
|
2023-10-14 10:14:58 +04:00
|
|
|
|
seriesColl.Clear();
|
|
|
|
|
// Создание и заполнение коллекции категорий
|
|
|
|
|
foreach (var diagramDatas in diagramWord.DiagramDatas)
|
|
|
|
|
{
|
|
|
|
|
seriesColl.Add(diagramDatas.Seria, diagramWord.Category, diagramDatas.Data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2023-10-12 00:24:32 +04:00
|
|
|
|
|
2023-10-12 22:42:25 +04:00
|
|
|
|
chart.Legend.Position = (Aspose.Words.Drawing.Charts.LegendPosition)diagramWord.LegendPosition;
|
2023-10-12 00:24:32 +04:00
|
|
|
|
|
|
|
|
|
// Сохранение документа в файл
|
2023-10-12 22:42:25 +04:00
|
|
|
|
document.Save(diagramWord.FilePath);
|
2023-10-12 00:24:32 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|