Microsoft® Criação e conversão de gráficos em arquivos Excel via .NET
Crie gráficos de documentos Excel e converta-os em imagens usando APIs do lado do servidor em aplicativos baseados em .NET.
Desenhar gráficos é uma arte de exibir dados graficamente para facilitar a análise. .NET Biblioteca Excel suporta desenho de gráficos em arquivos Excel. API suporta a criação de diferentes gráficos listados em Enumeração ChartType incluindo gráficos de pizza, pirâmide, linhas e bolhas. Além disso, também converte gráficos em imagens. API fornece um Classe de gráficos para blocos de construção de gráficos.
Crie gráficos em arquivo Excel
Criar gráficos usando Excel API é simples. O processo é, criar Aula de apostila objeto e selecione a primeira planilha ou a planilha relevante fornecendo seu índice. Insira os dados das células necessárias usando Método PutValue . Adicione gráfico à planilha usando a coleção Charts Adicionar método . Especifique o Tipo de Gráfico da enumeração ChartType.
Código C# para criar gráficos Excel
// Instantiating a Workbook object | |
Workbook wkb = new Workbook(); | |
// Obtaining the reference of the first worksheet | |
Worksheet wks = workbook.Worksheets[0]; | |
// Adding sample values to cells | |
wks.Cells["A2"].PutValue("Category1"); | |
wks.Cells["A3"].PutValue("Category2"); | |
wks.Cells["A4"].PutValue("Category3"); | |
wks.Cells["B1"].PutValue("Column1"); | |
wks.Cells["B2"].PutValue(4); | |
wks.Cells["B3"].PutValue(20); | |
wks.Cells["B4"].PutValue(50); | |
wks.Cells["C1"].PutValue("Column2"); | |
wks.Cells["C2"].PutValue(50); | |
wks.Cells["C3"].PutValue(100); | |
wks.Cells["C4"].PutValue(150); | |
// Adding a chart to the worksheet | |
int chartIndex = wks.Charts.Add(Aspose.Cells.Charts.ChartType.Column, 5, 0, 15, 5); | |
// Accessing the instance of the newly added chart | |
Aspose.Cells.Charts.Chart chart = wks.Charts[chartIndex]; | |
// Setting chart data source as the range "A1:C4" | |
chart.SetChartDataRange("A1:C4", true); | |
// Saving the Excel file | |
wkb.Save(dataDir + "output.xls"); | |
// API supports creating Pyramid, Line and Bubble chart as well |
Converter gráficos do Excel em imagens
O processo de conversão de gráficos em imagens é: Use a classe Workbook para carregar o arquivo Excel, selecione o workset relevante que contém os gráficos e chame o Método ToImage para conversão.
C# Código para converter gráfico do Excel em imagem
// Load Excel XLSX document having pie chart | |
var workbook = new Workbook("PieChart.xlsx"); | |
// get the designer chart (first chart) in the first worksheet of the workbook | |
var chart = workbook.Worksheets[0].Charts[0]; | |
// convert the chart to an image file. | |
chart.ToImage("PieChartOut.emf", System.Drawing.Imaging.ImageFormat.Emf); |