C # create an EXCEL chart and save it as an image,
Data Tables can clearly present data information. However, when we want to intuitively see the trend of data changes or proportions of data in complicated and variable data, data charts are more representative, in addition, it is more visual in presenting data information and can also obtain more information that cannot be directly presented by pure digital information. The following code shows you how to use the 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, load the Excel document 12 Workbook workbook = new Workbook (); 13 workbook. loadFromFile (@ "C: \ Users \ Administrator \ Desktop \ Sample.xlsx"); 14 15 // obtain the first Worksheet 16 Worksheet sheet = workbook. worksheets [0]; 17 18 // set the worksheet name 19 sheet. name = "Bar Chart"; 20 sheet. gridLinesVisible = false; 21 22 // create a bar Chart 23 chart = sheet. charts. add (ExcelChartType. columnClustered); 24 25 // specify the Data Area 26 chart used to generate the chart. dataRange = sheet. range ["A2: G6"]; 27 chart. seriesDataFromRange = false; 28 29 // specify the chart position 30 chart. leftColumn = 1; 31 chart. topRow = 9; 32 chart. rightColumn = 12; 33 chart. bottomRow = 26; 34 35 // set the chart name and font format 36 chart. chartTitle = "product sales in the first half of the year (unit: US $10 million)"; 37 chart. chartTitleArea. isBold = true; 38 chart. chartTitleArea. size = 12; 39 40 // set the 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 the Y axis coordinate name and font format 46 chart. primaryValueAxis. title = "sales"; 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 legend position 53 chart. legend. position = LegendPositionType. right; 54 55 // save document 56 workbook. saveToFile ("ColumnChart.xlsx", ExcelVersion. version2013); 57 58 59 // load the Excel document 60 workbook after the generated chart. loadFromFile ("ColumnChart.xlsx"); 61 62 // traverse the workbook and diagnose whether the workbook contains 63 images [] images = workbook. saveChartAsImage (sheet); 64 65 for (int I = 0; I <images. length; I ++) 66 {67 // Save the chart as image 68 images [I]. save (string. format ("img-1_02.16.png", I), ImageFormat. png); 69} 70} 71} 72}
Shows the generated Chart files and images:
Component get address: https://www.e-iceblue.cn/Downloads/Free-Spire-XLS-NET.html
The above are all operations for generating and converting Excel Data Tables into charts. I hope it will be helpful to you. You are welcome to repost it. (Please indicate the source for reprinting)
Thank you for browsing!