Класс OpenXmlDocumentChart
Представляет диаграмму документа Open XML.
Вот пример, показывающий, как создать файл DOCX с диаграммой (в примере используется документ-шаблон).Chart.docx):
''' <summary>
''' Creates the DOCX document with chart.
''' </summary>
''' <param name="resultFilePath">The result file path.</param>
Public Shared Sub CreateDocxWithChart(resultFilePath As String)
' path to a template DOCX document with chart
Dim templateDocDocumentPath As String = "Chart.docx"
' create DOCX document editor on template document with chart
Using docxDocumentEditor As New Vintasoft.Imaging.Office.OpenXml.Editor.Docx.DocxDocumentEditor(templateDocDocumentPath)
' get chart
Dim chart As Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentChart = docxDocumentEditor.Charts(0)
' change chart title
' find title
chart.Title = "Chart Title Example. " & System.DateTime.Now.ToShortDateString()
' change chart data
' create the chart data
Dim chartData As System.Nullable(Of Double)(,) = New System.Nullable(Of Double)(,) {{55, 32, 23}, {84, 48, 33}, {72, 53, 86}, {34, 82, 11}}
' change the chart data
chart.ChartData.SetData(chartData)
' save changes
docxDocumentEditor.Save(resultFilePath)
End Using
End Sub
/// <summary>
/// Creates the DOCX document with chart.
/// </summary>
/// <param name="resultFilePath">The result file path.</param>
public static void CreateDocxWithChart(string resultFilePath)
{
// path to a template DOCX document with chart
string templateDocDocumentPath = "Chart.docx";
// create DOCX document editor on template document with chart
using (Vintasoft.Imaging.Office.OpenXml.Editor.Docx.DocxDocumentEditor docxDocumentEditor =
new Vintasoft.Imaging.Office.OpenXml.Editor.Docx.DocxDocumentEditor(templateDocDocumentPath))
{
// get chart
Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentChart chart = docxDocumentEditor.Charts[0];
// change chart title
// find title
chart.Title = "Chart Title Example. " + System.DateTime.Now.ToShortDateString();
// change chart data
// create the chart data
double?[,] chartData = new double?[,] {
{ 55, 32, 23 },
{ 84, 48, 33 },
{ 72, 53, 86 },
{ 34, 82, 11 } };
// change the chart data
chart.ChartData.SetData(chartData);
// save changes
docxDocumentEditor.Save(resultFilePath);
}
}
Целевые платформы: .NET 8; .NET 7; .NET 6; .NET Framework 4.8, 4.7, 4.6, 4.5, 4.0, 3.5