PDF Barcode in C#, VB.NET
PDF DrawImage in C#, VB.NET
PDF DrawShape in C#, VB.NET
PDF DrawText in C#, VB.NET
C#/VB.NET: Create a Column Chart in Excel
A column chart is a chart that visualizes data as a set of rectangular columns, and the height of the column indicates the value of the data point. Creating column charts in Excel is a great way to compare data and show data change over time. In this article, you will learn how to programmatically create a column chart in Excel using Spire.XLS for .NET.
Install Spire.XLS for .NET
To begin with, you need to add the DLL files included in the Spire.XLS for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.XLS
Create a Column Chart in Excel
The detailed steps are as follows.
- Create a Workbook instance.
- Get the first worksheet using Workbook.Worksheets[sheetIndex] property.
- Add data to specified cells and set the cell styles.
- Add a clustered column chart to the worksheet using Worksheet.Charts.Add(ExcelChartType.ColumnClustered) method.
- Set data range for the chart using Chart.DataRange property.
- Set position, title, category axis and value axis for the chart.
- Loop through the data series of the chart, and show data labels for data points by setting the ChartSerie.DataPoints.DefaultDataPoint.DataLabels.HasValue property as true.
- Set the position of chart legend using Chart.Legend.Position property.
- Save the result file using Workbook.SaveToFile() method.
- C#
- VB.NET
using Spire.Xls;
using Spire.Xls.Charts;
using System.Drawing;
namespace ColumnChart
{
class Program
{
static void Main(string[] args)
{
//Create a Workbook instance
Workbook workbook = new Workbook();
//Get the first worksheet
Worksheet sheet = workbook.Worksheets[0];
//Add data to specified cells
sheet.Range["A1"].Value = "Country";
sheet.Range["A2"].Value = "Cuba";
sheet.Range["A3"].Value = "Mexico";
sheet.Range["A4"].Value = "France";
sheet.Range["A5"].Value = "German";
sheet.Range["B1"].Value = "Jun";
sheet.Range["B2"].NumberValue = 5000;
sheet.Range["B3"].NumberValue = 8000;
sheet.Range["B4"].NumberValue = 9000;
sheet.Range["B5"].NumberValue = 8500;
sheet.Range["C1"].Value = "Aug";
sheet.Range["C2"].NumberValue = 3000;
sheet.Range["C3"].NumberValue = 5000;
sheet.Range["C4"].NumberValue = 7000;
sheet.Range["C5"].NumberValue = 6000;
//Set cell styles
sheet.Range["A1:C1"].Style.Font.IsBold = true;
sheet.Range["A1:C1"].Style.KnownColor = ExcelColors.Black;
sheet.Range["A1:C1"].Style.Font.Color = Color.White;
sheet.Range["A1:C5"].Style.HorizontalAlignment = HorizontalAlignType.Center;
sheet.Range["A1:C5"].Style.VerticalAlignment = VerticalAlignType.Center;
//Set number format
sheet.Range["B2:C5"].Style.NumberFormat = "\"$\"#,##0";
//Add a column chart to the worksheet
Chart chart = sheet.Charts.Add(ExcelChartType.ColumnClustered);
//Set data range for the chart
chart.DataRange = sheet.Range["A1:C5"];
chart.SeriesDataFromRange = false;
//Set position of the chart
chart.LeftColumn = 1;
chart.TopRow = 7;
chart.RightColumn = 11;
chart.BottomRow = 29;
//Set and format chart title
chart.ChartTitle = "Sales market by country";
chart.ChartTitleArea.Size = 13;
chart.ChartTitleArea.IsBold = true;
//Set and format category axis
chart.PrimaryCategoryAxis.Title = "Country";
chart.PrimaryCategoryAxis.Font.Color = Color.Blue;
//Set and format value axis
chart.PrimaryValueAxis.Title = "Sales(in Dollars)";
chart.PrimaryValueAxis.HasMajorGridLines = false;
chart.PrimaryValueAxis.MinValue = 1000;
chart.PrimaryValueAxis.TitleArea.TextRotationAngle = 90;
//Show data labels for data points
foreach (ChartSerie cs in chart.Series)
{
cs.Format.Options.IsVaryColor = true;
cs.DataPoints.DefaultDataPoint.DataLabels.HasValue = true;
}
//Set position of chart legend
chart.Legend.Position = LegendPositionType.Top;
//Save the result file
workbook.SaveToFile("ExcelColumnChart.xlsx", ExcelVersion.Version2010);
}
}
}

Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
Save Excel Document in C#, VB.NET
Automation of an Excel file allows us to doing various operations in C#/VB.NET. Any loss in these operations may result in unexpected negative consequences for developers and the clients of the developers. That means we must find a solution that enables us to Save Excel with no loss in quality of our operations. This section will demonstrate how to fast save Excel file with perfect performance as directly operations in Excel files.
Spire.Xls for .NET is a professional component that enables developers directly manages Excel operation regardless whether Microsoft Excel is installed on or not. With Spire.Xls for .NET, we can save Excel to what we want it to be. Any kind of trial and evaluation on Spire.Xls for .NET is always welcomed; so now please feel free to download Spire.Xls for .NET and then follow our guide to save perfect Excel or try other function of Spire.Xls for .NET.
Spire.Xls for .NET allows us to create a new Excel file, write data in to it, edit the input data and then save Excel file.
using Spire.Xls;
namespace Excel_save
{
class Program
{
static void Main(string[] args)
{
//Create a new workbook
Workbook workbook = new Workbook();
//Initialize worksheet
Worksheet sheet = workbook.Worksheets[0];
//Append text
sheet.Range["A1"].Text = "Demo: Save Excel in .NET";
//Save it as Excel file
workbook.SaveToFile("Sample.xls",ExcelVersion.Version97to2003);
//Launch the file
System.Diagnostics.Process.Start(workbook.FileName);
}
}
}
Imports Spire.Xls
Namespace Excel_save
Class Program
Private Shared Sub Main(args As String())
'Create a new workbook
Dim workbook As New Workbook()
'Initialize worksheet
Dim sheet As Worksheet = workbook.Worksheets(0)
'Append text
sheet.Range("A1").Text = "Demo: Save Excel in .NET"
'Save it as Excel file
workbook.SaveToFile("Sample.xls",ExcelVersion.Version97to2003)
'Launch the file
System.Diagnostics.Process.Start(workbook.FileName)
End Sub
End Class
End Namespace
C#/VB.NET: Add or Read Formulas and Functions in Excel
Excel is a powerful spreadsheet software with numerous features, but formulas and functions are undoubtedly among its most critical tools. They enable users to perform a wide range of mathematical, statistical, and logical operations on their data, allowing them to derive meaningful insights quickly and accurately. In this article, we will explain how to add or read formulas and functions in Excel files in C# and VB.NET using Spire.XLS for .NET.
- Add Formulas and Functions to Excel in C# and VB.NET
- Read Formulas and Functions in Excel in C# and VB.NET
Install Spire.XLS for .NET
To begin with, you need to add the DLL files included in the Spire.XLS for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.XLS
Add Formulas and Functions to Excel in C# and VB.NET
The Worksheet.Range[int row, int column].Formula property in Spire.XLS for .NET is used to add formulas or functions to specific cells in an Excel worksheet. The main steps are as follows:
- Initialize an instance of the Workbook class.
- Get a specific worksheet by its index using the Workbook.Worksheets[int index] property.
- Add some text and numeric data to specific cells of the worksheet using the Worksheet.Range[int row, int column].Text and Worksheet.Range[int row, int column].NumberValue properties.
- Add text and formulas to specific cells of the worksheet using the Worksheet.Range[int row, int column].Text and Worksheet.Range[int row, int column].Formula properties.
- Add text and functions to specific cells of the worksheet using the Worksheet.Range[int row, int column].Text and Worksheet.Range[int row, int column].Formula properties.
- Save the result file using Workbook.SaveToFile(string fileName, ExcelVersion version) method.
- C#
- VB.NET
using Spire.Xls;
namespace AddFormulasAndFunctions
{
internal class Program
{
static void Main(string[] args)
{
//Initialize an instance of the Workbook class
Workbook workbook = new Workbook();
//Get the first worksheet
Worksheet sheet = workbook.Worksheets[0];
//Declare two variables: currentRow, currentFormula
int currentRow = 1;
string currentFormula;
//Add text to the worksheet and set cell style
sheet.Range[currentRow, 1].Text = "Test Data:";
sheet.Range[currentRow, 1].Style.Font.IsBold = true;
sheet.Range[currentRow, 1].Style.FillPattern = ExcelPatternType.Solid;
sheet.Range[currentRow, 1].Style.KnownColor = ExcelColors.LightGreen1;
sheet.Range[currentRow, 1].Style.Borders[BordersLineType.EdgeBottom].LineStyle = LineStyleType.Medium;
//Add some numeric data to the worksheet
sheet.Range[++currentRow, 1].NumberValue = 7.3;
sheet.Range[currentRow, 2].NumberValue = 5;
sheet.Range[currentRow, 3].NumberValue = 8.2;
sheet.Range[currentRow, 4].NumberValue = 4;
sheet.Range[currentRow, 5].NumberValue = 3;
sheet.Range[currentRow, 6].NumberValue = 11.3;
currentRow++;
//Add text to the worksheet and set cell style
sheet.Range[++currentRow, 1].Text = "Formulas";
sheet.Range[currentRow, 2].Text = "Results";
sheet.Range[currentRow, 1, currentRow, 2].Style.Font.IsBold = true;
sheet.Range[currentRow, 1, currentRow, 2].Style.KnownColor = ExcelColors.LightGreen1;
sheet.Range[currentRow, 1, currentRow, 2].Style.FillPattern = ExcelPatternType.Solid;
sheet.Range[currentRow, 1, currentRow, 2].Style.Borders[BordersLineType.EdgeBottom].LineStyle = LineStyleType.Medium;
//Add text and formulas to the worksheet
currentFormula = "=\"Hello\"";
sheet.Range[++currentRow, 1].Text = "'" + currentFormula;
sheet.Range[currentRow, 2].Formula = currentFormula;
currentFormula = "=300";
sheet.Range[++currentRow, 1].Text = "'" + currentFormula;
sheet.Range[currentRow, 2].Formula = currentFormula;
currentFormula = "=3389.639421";
sheet.Range[++currentRow, 1].Text = "'" + currentFormula;
sheet.Range[currentRow, 2].Formula = currentFormula;
currentFormula = "=false";
sheet.Range[++currentRow, 1].Text = "'" + currentFormula;
sheet.Range[currentRow, 2].Formula = currentFormula;
currentFormula = "=1+2+3+4+5-6-7+8-9";
sheet.Range[++currentRow, 1].Text = "'" + currentFormula;
sheet.Range[currentRow, 2].Formula = currentFormula;
currentFormula = "=33*3/4-2+10";
sheet.Range[++currentRow, 1].Text = "'" + currentFormula;
sheet.Range[currentRow, 2].Formula = currentFormula;
currentFormula = "=Sheet1!$B$2";
sheet.Range[++currentRow, 1].Text = "'" + currentFormula;
sheet.Range[currentRow, 2].Formula = currentFormula;
//Add text and Functions to the worksheet
//AVERAGE
currentFormula = "=AVERAGE(Sheet1!$D$2:F$2)";
sheet.Range[++currentRow, 1].Text = "'" + currentFormula;
sheet.Range[currentRow, 2].Formula = currentFormula;
//COUNT
currentFormula = "=COUNT(3,5,8,10,2,34)";
sheet.Range[++currentRow, 1].Text = "'" + currentFormula;
sheet.Range[currentRow, 2].Formula = currentFormula;
//NOW
currentFormula = "=NOW()";
sheet.Range[++currentRow, 1].Text = "'" + currentFormula;
sheet.Range[currentRow, 2].Formula = currentFormula;
sheet.Range[currentRow, 2].Style.NumberFormat = "yyyy-MM-DD";
//SECOND
currentFormula = "=SECOND(0.503)";
sheet.Range[++currentRow, 1].Text = "'" + currentFormula;
sheet.Range[currentRow++, 2].Formula = currentFormula;
//MINUTE
currentFormula = "=MINUTE(0.78125)";
sheet.Range[currentRow, 1].Text = "'" + currentFormula;
sheet.Range[currentRow++, 2].Formula = currentFormula;
//MONTH
currentFormula = "=MONTH(9)";
sheet.Range[currentRow, 1].Text = "'" + currentFormula;
sheet.Range[currentRow++, 2].Formula = currentFormula;
//DAY
currentFormula = "=DAY(10)";
sheet.Range[currentRow, 1].Text = "'" + currentFormula;
sheet.Range[currentRow++, 2].Formula = currentFormula;
//TIME
currentFormula = "=TIME(4,5,7)";
sheet.Range[currentRow, 1].Text = "'" + currentFormula;
sheet.Range[currentRow++, 2].Formula = currentFormula;
//DATE
currentFormula = "=DATE(6,4,2)";
sheet.Range[currentRow, 1].Text = "'" + currentFormula;
sheet.Range[currentRow++, 2].Formula = currentFormula;
//RAND
currentFormula = "=RAND()";
sheet.Range[currentRow, 1].Text = "'" + currentFormula;
sheet.Range[currentRow++, 2].Formula = currentFormula;
//HOUR
currentFormula = "=HOUR(0.5)";
sheet.Range[currentRow, 1].Text = "'" + currentFormula;
sheet.Range[currentRow++, 2].Formula = currentFormula;
//MOD
currentFormula = "=MOD(5,3)";
sheet.Range[currentRow, 1].Text = "'" + currentFormula;
sheet.Range[currentRow++, 2].Formula = currentFormula;
//WEEKDAY
currentFormula = "=WEEKDAY(3)";
sheet.Range[currentRow, 1].Text = "'" + currentFormula;
sheet.Range[currentRow++, 2].Formula = currentFormula;
//YEAR
currentFormula = "=YEAR(23)";
sheet.Range[currentRow, 1].Text = "'" + currentFormula;
sheet.Range[currentRow++, 2].Formula = currentFormula;
//NOT
currentFormula = "=NOT(true)";
sheet.Range[currentRow, 1].Text = "'" + currentFormula;
sheet.Range[currentRow++, 2].Formula = currentFormula;
//OR
currentFormula = "=OR(true)";
sheet.Range[currentRow, 1].Text = "'" + currentFormula;
sheet.Range[currentRow++, 2].Formula = currentFormula;
//AND
currentFormula = "=AND(TRUE)";
sheet.Range[currentRow, 1].Text = "'" + currentFormula;
sheet.Range[currentRow++, 2].Formula = currentFormula;
//VALUE
currentFormula = "=VALUE(30)";
sheet.Range[currentRow, 1].Text = "'" + currentFormula;
sheet.Range[currentRow++, 2].Formula = currentFormula;
//LEN
currentFormula = "=LEN(\"world\")";
sheet.Range[currentRow, 1].Text = "'" + currentFormula;
sheet.Range[currentRow++, 2].Formula = currentFormula;
//MID
currentFormula = "=MID(\"world\",4,2)";
sheet.Range[currentRow, 1].Text = "'" + currentFormula;
sheet.Range[currentRow++, 2].Formula = currentFormula;
//ROUND
currentFormula = "=ROUND(7,3)";
sheet.Range[currentRow, 1].Text = "'" + currentFormula;
sheet.Range[currentRow++, 2].Formula = currentFormula;
//SIGN
currentFormula = "=SIGN(4)";
sheet.Range[currentRow, 1].Text = "'" + currentFormula;
sheet.Range[currentRow++, 2].Formula = currentFormula;
//INT
currentFormula = "=INT(200)";
sheet.Range[currentRow, 1].Text = "'" + currentFormula;
sheet.Range[currentRow++, 2].Formula = currentFormula;
//ABS
currentFormula = "=ABS(-1.21)";
sheet.Range[currentRow, 1].Text = "'" + currentFormula;
sheet.Range[currentRow++, 2].Formula = currentFormula;
//LN
currentFormula = "=LN(15)";
sheet.Range[currentRow, 1].Text = "'" + currentFormula;
sheet.Range[currentRow++, 2].Formula = currentFormula;
//EXP
currentFormula = "=EXP(20)";
sheet.Range[currentRow, 1].Text = "'" + currentFormula;
sheet.Range[currentRow++, 2].Formula = currentFormula;
//SQRT
currentFormula = "=SQRT(40)";
sheet.Range[currentRow, 1].Text = "'" + currentFormula;
sheet.Range[currentRow++, 2].Formula = currentFormula;
//PI
currentFormula = "=PI()";
sheet.Range[currentRow, 1].Text = "'" + currentFormula;
sheet.Range[currentRow++, 2].Formula = currentFormula;
//COS
currentFormula = "=COS(9)";
sheet.Range[currentRow, 1].Text = "'" + currentFormula;
sheet.Range[currentRow++, 2].Formula = currentFormula;
//SIN
currentFormula = "=SIN(45)";
sheet.Range[currentRow, 1].Text = "'" + currentFormula;
sheet.Range[currentRow++, 2].Formula = currentFormula;
//MAX
currentFormula = "=MAX(10,30)";
sheet.Range[currentRow, 1].Text = "'" + currentFormula;
sheet.Range[currentRow++, 2].Formula = currentFormula;
//MIN
currentFormula = "=MIN(5,7)";
sheet.Range[currentRow, 1].Text = "'" + currentFormula;
sheet.Range[currentRow++, 2].Formula = currentFormula;
//AVERAGE
currentFormula = "=AVERAGE(12,45)";
sheet.Range[currentRow, 1].Text = "'" + currentFormula;
sheet.Range[currentRow++, 2].Formula = currentFormula;
//SUM
currentFormula = "=SUM(18,29)";
sheet.Range[currentRow, 1].Text = "'" + currentFormula;
sheet.Range[currentRow++, 2].Formula = currentFormula;
//IF
currentFormula = "=IF(4,2,2)";
sheet.Range[currentRow, 1].Text = "'" + currentFormula;
sheet.Range[currentRow++, 2].Formula = currentFormula;
//SUBTOTAL
currentFormula = "=SUBTOTAL(3,Sheet1!A2:F2)";
sheet.Range[currentRow, 1].Text = "'" + currentFormula;
sheet.Range[currentRow++, 2].Formula = currentFormula;
//Set width of the 1st, 2nd and 3rd columns
sheet.SetColumnWidth(1, 32);
sheet.SetColumnWidth(2, 16);
sheet.SetColumnWidth(3, 16);
//Create a cell style
CellStyle style = workbook.Styles.Add("Style");
//Set the horizontal alignment as left
style.HorizontalAlignment = HorizontalAlignType.Left;
//Apply the style to the worksheet
sheet.ApplyStyle(style);
//Save the result file
workbook.SaveToFile("AddFormulasAndFunctions.xlsx", ExcelVersion.Version2016);
workbook.Dispose();
}
}
}

Read Formulas and Functions in Excel in C# and VB.NET
To read formulas and functions in an Excel worksheet, you need to iterate through all the cells in the worksheet, after that, find the cells containing formulas or functions using the Cell.HasFormula property, then get the formulas or functions of the cells using the CellRange.Formula property. The detailed steps are as follows:
- Initialize an instance of the Workbook class.
- Load an Excel file using the Workbook.LoadFromFile() method.
- Get a specific worksheet by its index using the Workbook.Worksheets[sheetIndex] property.
- Initialize an instance of the StringBuilder class.
- Access the used range of the worksheet using the Worksheet.AllocatedRange property.
- Iterate through all the cells in the used range.
- Find the cells containing formulas/functions using the Cell.HasFormula property.
- Get the names and the formulas/functions of the cells using the CellRange.RangeAddressLocal and CellRange.Formula properties.
- Append the cell names and formulas/functions to the StringBuilder using the StringBuilder.AppendLine() method.
- Write the content of the StringBuilder into a .txt file using the File.WriteAllText() method.
- C#
- VB.NET
using Spire.Xls;
using System.IO;
using System.Text;
namespace ReadFormulasAndFunctions
{
internal class Program
{
static void Main(string[] args)
{
//Initialize an instance of the Workbook class
Workbook workbook = new Workbook();
//Load an Excel file
workbook.LoadFromFile("AddFormulasAndFunctions.xlsx");
//Get the first worksheet
Worksheet sheet = workbook.Worksheets[0];
//Initialize an instance of the StringBuilder class
StringBuilder sb = new StringBuilder();
//Access the used range of the worksheet
CellRange usedRange = sheet.AllocatedRange;
//Loop through all the cells in the used range
foreach (CellRange cell in usedRange)
{
//Detect if the current cell has formula/function
if (cell.HasFormula)
{
//Get the cell name
string cellName = cell.RangeAddressLocal;
//Get the formula/function
string formula = cell.Formula;
//Append the cell name and formula/function to the StringBuilder
sb.AppendLine(cellName + " has a formula: " + formula);
}
}
//Write the content of the StringBuilder into a .txt file
File.WriteAllText("ReadFormulasAndFunctions.txt", sb.ToString());
}
}
}

Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
C#/VB.NET: Insert Images into Excel
Images are highly effective in conveying thoughts and ideas. Sometimes, you may need to insert images into an Excel report so that audiences can grasp your intentions quickly and clearly. In this article, you will learn how to insert image into Excel in C# and VB.NET using Spire.XLS for .NET library.
- Insert Image from Disk into Excel in C# and VB.NET
- Insert Web Image from a URL into Excel in C# and VB.NET
Install Spire.XLS for .NET
To begin with, you need to add the DLL files included in the Spire.XLS for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.XLS
Insert Image from Disk into Excel in C# and VB.NET
The following are the steps to insert an image from disk into Excel:
- Initialize a Workbook instance.
- Get the desired worksheet using Workbook.Worksheets[sheetIndex] property.
- Insert an image into the worksheet using the Worksheet.Pictures.Add() method.
- Save the result file using Workbook.SaveToFile() method.
- C#
- VB.NET
using Spire.Xls;
namespace InsertImageInExcel
{
class Program
{
static void Main(string[] args)
{
//Initialize a Workbook instance
Workbook workbook = new Workbook();
//Get the first worksheet
Worksheet sheet = workbook.Worksheets[0];
//Insert image into the worksheet
sheet.Pictures.Add(1, 1, @"E:\work\sample.jpg");
//Save the result file
workbook.SaveToFile("InsertImageFromDisk.xlsx", ExcelVersion.Version2016);
}
}
}

Insert Web Image from a URL into Excel in C# and VB.NET
The following are the steps to insert a web image from a URL into Excel:
- Initialize a Workbook instance.
- Get the desired worksheet using Workbook.Worksheets[sheetIndex] property.
- Initialize a WebClient instance, then download the web image as a byte array from the specified URL using WebClient.DownloadData(urlAddress) method.
- Initialize a MemoryStream instance from the byte array.
- Create an Image object from the memory stream then insert it into the worksheet using Worksheet.Pictures.Add() method.
- Save the result file using Workbook.SaveToFile() method.
- C#
- VB.NET
using Spire.Xls;
using System.Drawing;
using System.IO;
using System.Net;
namespace InsertWebImageInExcel
{
class Program
{
static void Main(string[] args)
{
//Initialize a Workbook instance
Workbook workbook = new Workbook();
//Get the first sheet
Worksheet sheet = workbook.Worksheets[0];
//Specify the image URL
string URL = "https://www.e-iceblue.com/downloads/demo/Logo.png";
//Initialize a WebClient instance
WebClient webClient = new WebClient();
//Download the image as a byte array from the URL
byte[] imageData = webClient.DownloadData(URL);
//Initialize a MemoryStream instance from the byte array
MemoryStream objImage = new MemoryStream(imageData);
//Create an Image object from the memory stream
Image image = Image.FromStream(objImage);
//Insert the image into the worksheet
sheet.Pictures.Add(3, 2, image);
//Save the result file
workbook.SaveToFile("InsertWebImage.xlsx", ExcelVersion.Version2016);
}
}
}

Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.





