Featured

How to create excel files and charts using Free Spire.XLS for .NET

Last modified: July 04, 2021
Step 1

Create an ASP.NET Web Form application.

Step 2

Install Free Spire.XLS Nuget package. nuget

Step 3
public IActionResult Index() { var ms = new MemoryStream(); var contentType = "application/vnd.ms-excel"; Workbook workbook = new Workbook(); Worksheet sheet = workbook.Worksheets[0]; //create first worksheet sheet.Range["A1:M1"].Merge(); //merge A1 to D1 sheet.Range["A1:M1"].Style.Color = Color.LightSeaGreen; // set the background colour sheet.Range["A1:M1"].Style.Font.Size = 15; //set the font size to 15 sheet.Range["A1:M1"].Style.Font.IsBold = true; //set the font bold sheet.Range["A1:M1"].Style.HorizontalAlignment = HorizontalAlignType.Center; // text align center sheet.Range["A1:M1"].Style.VerticalAlignment = VerticalAlignType.Center; sheet.Range["A1:M1"].RowHeight = 20;//set height to 20 sheet.Range["A1:M1"].Text = "Website Visit Analytics"; sheet.Range["A2"].Text = "Month"; sheet.Range["B2"].Text = "Visit Count"; sheet.Range["A3"].Text = "Feb"; sheet.Range["B3"].NumberValue = 500; sheet.Range["A4"].Text = "March"; sheet.Range["B4"].NumberValue = 600; sheet.Range["A5"].Text = "April"; sheet.Range["B5"].NumberValue = 1050; Chart columnChart = sheet.Charts.Add(); columnChart.ChartType = ExcelChartType.ColumnClustered; columnChart.DataRange = sheet.Range["B2:B5"]; columnChart.SeriesDataFromRange = false; var cs = columnChart.Series[0]; cs.CategoryLabels = sheet.Range["A3:A5"]; //Chart Labels resource cs.Values = sheet.Range["B3:B5"]; //Chart value resource cs.DataPoints.DefaultDataPoint.DataLabels.HasValue = true; //Set the value visible in the chart cs.Format.Fill.ForeColor = Color.FromKnownColor(KnownColor.Purple); //Set column colour columnChart.LeftColumn = 1; columnChart.TopRow = 6; columnChart.RightColumn = 9; columnChart.BottomRow = 25; Chart pieChart = sheet.Charts.Add(ExcelChartType.Line); pieChart.DataRange = sheet.Range["B2:B5"]; pieChart.SeriesDataFromRange = false; var cs2 = pieChart.Series[0]; cs2.CategoryLabels = sheet.Range["A3:A5"];//Chart Labels resource cs2.Values = sheet.Range["B3:B5"]; //Chart value resource cs2.DataPoints.DefaultDataPoint.DataLabels.HasValue = true; cs2.Format.LineProperties.Color = Color.FromKnownColor(KnownColor.Purple); //set line colour pieChart.LeftColumn = 10; pieChart.TopRow = 6; pieChart.RightColumn = 20; pieChart.BottomRow = 40; workbook.SaveToStream(ms, Spire.Xls.FileFormat.Version2016); ms.Position = 0; var fileName = "Analytics.xlsx"; return File(ms, contentType, fileName); }

graph