C#Create EXCEL diagrams and save them as pictures

Data tables can clearly present data information, but when we want to intuitively see the trend of data change or the proportion of data for some complex and changeable data, data charts will be more representative, and will also be more image in presenting data information, and can also get more information that pure digital information cannot directly display.In the code below, you will be shown how to do this by using the free Free Spire XLS for.NET component.

Original data table:

C#

 1 using Spire.Xls;
 2 using System.Drawing;
 3 using System.Drawing.Imaging;
 4 
 5 namespace CreateChart_XLS
 6 {
 7     class Program
 8     {
 9         static void Main(string[] args)
10         {
11             //Create a Workbook Class instance, loading Excel File
12             Workbook workbook = new Workbook();
13             workbook.LoadFromFile(@"C:\Users\Administrator\Desktop\Sample.xlsx");
14 
15             //Get the first sheet
16             Worksheet sheet = workbook.Worksheets[0];
17 
18             //Set the name of the worksheet
19             sheet.Name = "Column Chart";
20             sheet.GridLinesVisible = false;
21 
22             //Create Column Chart
23             Chart chart = sheet.Charts.Add(ExcelChartType.ColumnClustered);
24 
25             //Specify the data area used to generate the chart
26             chart.DataRange = sheet.Range["A2:G6"];
27             chart.SeriesDataFromRange = false;
28 
29             //Specify where the chart is located
30             chart.LeftColumn = 1;
31             chart.TopRow = 9;
32             chart.RightColumn = 12;
33             chart.BottomRow = 26;
34 
35             //Set the name and font format of the chart
36             chart.ChartTitle = "Product Sales in the First Half Year(Unit: $10,000)";
37             chart.ChartTitleArea.IsBold = true;
38             chart.ChartTitleArea.Size = 12;
39 
40             //Set up X Axis coordinate name and font format
41             chart.PrimaryCategoryAxis.Title = "Product Category";
42             chart.PrimaryCategoryAxis.Font.IsBold = true;
43             chart.PrimaryCategoryAxis.TitleArea.IsBold = false;
44          
45             //Set up Y Axis coordinate name and font format
46             chart.PrimaryValueAxis.Title = "Sales volume";            
47             chart.PrimaryValueAxis.HasMajorGridLines = false;
48             chart.PrimaryValueAxis.TitleArea.TextRotationAngle = 90;
49             chart.PrimaryValueAxis.MinValue = 0.5;
50             chart.PrimaryValueAxis.TitleArea.IsBold = false;
51 
52             //Set the position of the legend
53             chart.Legend.Position = LegendPositionType.Right;
54 
55              //Save Document
56             workbook.SaveToFile("ColumnChart.xlsx", ExcelVersion.Version2013);
57 
58 
59              //After loading the generated graph Excel File
60             workbook.LoadFromFile("ColumnChart.xlsx");
61 
62             //Traverse the workbook to diagnose if it contains charts
63             Image[] images = workbook.SaveChartAsImage(sheet);
64 
65              for (int i = 0; i < images.Length; i++)
66               {
67                  //Save Chart as Picture
68                   images[i].Save(string.Format("img-{0}.png", i), ImageFormat.Png);
69                }
70         }
71     }
72 }

 

The resulting chart file and picture are as follows:

Component Get Address: https://www.e-iceblue.cn/Downloads/Free-Spire-XLS-NET.html

The above is all about generating and converting Excel data tables into charts. I hope it will be helpful to you. Welcome to upload (please indicate where you are from)

Thanks for browsing!

Tags: C# Excel

Posted on Sun, 05 Jul 2020 10:31:04 -0400 by superuser2