PIbd-32_Rogashova_E.A._COP_1/COPWinForms/ComponentWord3.cs

88 lines
2.9 KiB
C#

using Aspose.Words.Drawing.Charts;
using Aspose.Words;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using Aspose.Words.Drawing;
using DocumentFormat.OpenXml.Office2010.Excel;
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();
}
public void CreateLineChart(DiagramWord diagramWord)
{
// Проверка наличия заголовка, пути и названия диаграммы
if (string.IsNullOrEmpty(diagramWord.ChartTitle) || string.IsNullOrEmpty(diagramWord.FilePath) || string.IsNullOrEmpty(diagramWord.DocumentTitle))
{
throw new ArgumentException("Chart title is null or empty");
}
// Создание документа
Document document = new Document();
DocumentBuilder builder = new DocumentBuilder(document);
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);
// Вставка диаграммы
Shape chartShape = builder.InsertChart(ChartType.Line, 400, 300);
Chart chart = chartShape.Chart;
// Настройка заголовка диаграммы
ChartTitle title = chart.Title;
title.Text = diagramWord.ChartTitle;
ChartSeriesCollection seriesColl = chart.Series;
seriesColl.Clear();
// Создание и заполнение коллекции категорий
foreach (var diagramDatas in diagramWord.DiagramDatas)
{
seriesColl.Add(diagramDatas.Seria, diagramWord.Category, diagramDatas.Data);
}
chart.Legend.Position = (Aspose.Words.Drawing.Charts.LegendPosition)diagramWord.LegendPosition;
// Сохранение документа в файл
document.Save(diagramWord.FilePath);
}
}
}