Spire.XLS for WPF Program Guide Content

2016-01-12 05:30:56 Written by Koohji

Spire.XLS is a versatile Excel library that is designed for software developers to perform a wide range of Excel processing tasks on .NET, Silverlight and WPF Platform. As a combination of APIs and GUI controls, Spire.XLS does not need to install MS Excel or any third party libraries and supports to apply Excel on the formats of either Excel .xls 97-2003 or Excel .xlsx 2007, 2010 and 2013.

This API gives developers powerful tools for performing simple tasks as well as more complex tasks. Basic operation tasks such as create, save, protect and merge Excel files, add/delete/hide worksheets, edit spreadsheet cell data, generate charts can be all realized with high efficiency.

Spire.Doc for WPF Program Guide Content

2016-01-12 01:40:48 Written by Koohji

Spire.Doc, specially designed for developers/programmers to manipulate word proceeding tasks, from Word version 97-2003 to 2010 and 2013 in .NET, Silverlight and WPF Platform, always welcomes any kind of evaluation or test from either organizations or individuals with no charges.

This professional word component enables users to perform a large range of tasks on Word, as document operation (create, open, edit and save), mail merge, security, format (font, paragraph and page settings), objects (text, image, hyperlink, comment, table, bookmark, header/footer, footnote/endnote etc.). The conversion feature stands out of the peers, which enables developers to realize conversion between most popular formats with high fidelity, including Word to PDF, HTML, RTF, XML, Image formats, TEXT and RTF, XML and HTML files also can be converted to Word back. The following sections have been well-organized to show you how these functions work in detail.

Mail Merge

Others

C#: Add a Stamp to a PDF Document

2024-10-11 06:34:00 Written by Koohji

Adding a stamp to a PDF document can enhance its visual appeal and convey important information, such as approval, confidentiality, or urgency. This simple process allows users to mark documents with customized images or text, making it easier to communicate key messages. Whether for professional use or personal projects, stamping PDFs ensures clarity and adds a professional touch.

In this article, you will learn how to add a stamp to a PDF document using C# with Spire.PDF for .NET.

Install Spire.PDF for .NET

To begin with, you need to add the DLL files included in the Spire.PDF 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.PDF

Prerequisite Knowledge

In a PDF, a stamp is an annotation or graphical element that adds supplementary information to a document. Spire.PDF for .NET includes the PdfRubberStampAnnotation class, which represents a rubber stamp. To create the appearance for the rubber stamp, you can use the PdfTemplate class, which serves as a canvas for drawing text, images, shapes, and date/time elements.

Add a Dynamic Stamp to PDF in C#

A dynamic stamp is a customizable annotation added to a PDF document to convey specific statuses, approvals, or other information. Unlike static stamps, dynamic stamps include variables or fields that can be updated in real time, such as the current date, time, username, or other custom data.

Here are the steps to add a dynamic stamp to a PDF using Spire.PDF for .NET:

  • Create a PdfDocument object.
  • Load a PDF file using PdfDocument.LoadFromFile() method.
  • Create a PdfTemplate object with the desired size.
  • Draw strings, including dynamic information like date and time, on the template using PdfTemplate.Graphics.DrawString().
  • Create a PdfRubberStampAnnotation object and set the template as its appearance.
  • Add the stamp to a specific PDF page using PdfPageBase.Annotations.Add() method.
  • Save the document to a different PDF file.
  • C#
using System.Drawing;
using Spire.Pdf;
using Spire.Pdf.Graphics;
using Spire.Pdf.Annotations;
using Spire.Pdf.Annotations.Appearance;

namespace AddDynamicStamp
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a PdfDocument object
            PdfDocument doc = new PdfDocument();

            // Load a PDF document
            doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\input.pdf");

            // Get a specific page
            PdfPageBase page = doc.Pages[1];

            // Create a PdfTemplate object
            PdfTemplate template = new PdfTemplate(220, 50);

            // Create two fonts
            PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Elephant", 16f, FontStyle.Bold),true);  
            PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Times New Roman", 10f, FontStyle.Bold),true);

            // Create a solid brush and a gradient brush
            PdfSolidBrush solidBrush = new PdfSolidBrush(Color.Blue);
            RectangleF rectangle1 = new RectangleF(new PointF(0, 0), template.Size);
            PdfLinearGradientBrush linearGradientBrush = new PdfLinearGradientBrush(rectangle1, new PdfRGBColor(Color.White), new PdfRGBColor(Color.Blue), PdfLinearGradientMode.Horizontal);

            // Create a pen
            PdfPen pen = new PdfPen(solidBrush);

            // Create a rounded rectangle path
            int CornerRadius = 10;
            PdfPath path = new PdfPath();
            path.AddArc(template.GetBounds().X, template.GetBounds().Y, CornerRadius, CornerRadius, 180, 90);
            path.AddArc(template.GetBounds().X + template.Width - CornerRadius, template.GetBounds().Y, CornerRadius, CornerRadius, 270, 90);
            path.AddArc(template.GetBounds().X + template.Width - CornerRadius, template.GetBounds().Y + template.Height - CornerRadius, CornerRadius, CornerRadius, 0, 90);
            path.AddArc(template.GetBounds().X, template.GetBounds().Y + template.Height - CornerRadius, CornerRadius, CornerRadius, 90, 90);
            path.AddLine(template.GetBounds().X, template.GetBounds().Y + template.Height - CornerRadius, template.GetBounds().X, template.GetBounds().Y + CornerRadius / 2);

            // Draw path on the template
            template.Graphics.DrawPath(pen, path);
            template.Graphics.DrawPath(linearGradientBrush, path);

            // Draw text on the template
            String string1 = "APPROVED\n";
            String string2 = "By Marketing Manager at " + DateTime.Now.ToString("HH:mm, MMM dd, yyyy");
            template.Graphics.DrawString(string1, font1, solidBrush, new PointF(5, 5));
            template.Graphics.DrawString(string2, font2, solidBrush, new PointF(2, 28));

            // Create a rubber stamp, specifying its size and location
            RectangleF rectangle2 = new RectangleF(55, page.ActualSize.Height - 55 - 70, 240, 55);
            PdfRubberStampAnnotation stamp = new PdfRubberStampAnnotation(rectangle2);

            // Create a PdfAppearance object and apply the template as its normal state
            PdfAppearance apprearance = new PdfAppearance(stamp);
            apprearance.Normal = template;

            // Apply the appearance to stamp
            stamp.Appearance = apprearance;

            // Add the stamp annotation to annotation collection
            page.Annotations.Add(stamp);

            // Save the file
            doc.SaveToFile("DynamicStamp.pdf", FileFormat.PDF);

            // Dispose resources
            doc.Dispose();
        }
    }
}

C#: Add a Stamp to a PDF Document

Add an Image Stamp to PDF in C#

An image stamp in a PDF is a graphical element that is added to a document as an annotation or overlay. This stamp typically consists of an image that can be positioned at a specific location on a PDF page.

Here are the steps to add an image stamp to a PDF using Spire.PDF for .NET:

  • Create a PdfDocument object.
  • Load a PDF file using PdfDocument.LoadFromFile() method.
  • Create a PdfTemplate object with the desired size.
  • Draw strings, including dynamic information like date and time, on the template using PdfTemplate.Graphics.DrawString().
  • Create a PdfRubberStampAnnotation object and set the template as its appearance.
  • Add the stamp to a specific PDF page using PdfPageBase.Annotations.Add() method.
  • Save the document to a different PDF file.
  • C#
using Spire.Pdf;
using Spire.Pdf.Annotations;
using Spire.Pdf.Annotations.Appearance;
using Spire.Pdf.Graphics;
using System.Drawing;

namespace AddImageStamp
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a PdfDocument object
            PdfDocument doc = new PdfDocument();

            // Load a PDF document
            doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\input.pdf");

            // Get a specific page
            PdfPageBase page = doc.Pages[1];

            // Load an image file
            PdfImage image = PdfImage.FromFile("C:\\Users\\Administrator\\Desktop\\confidential.png");

            // Get the width and height of the image
            int width = image.Width;
            int height = image.Height;

            // Create a PdfTemplate object based on the size of the image
            PdfTemplate template = new PdfTemplate(width, height, true);

            // Draw image on the template
            template.Graphics.DrawImage(image, 0, 0, width, height);

            // Create a rubber stamp annotation, specifying its location and position
            RectangleF rect = new RectangleF(page.ActualSize.Width - width - 50, page.ActualSize.Height - height - 50, width, height);
            PdfRubberStampAnnotation stamp = new PdfRubberStampAnnotation(rect);

            // Create a PdfAppearance object
            PdfAppearance pdfAppearance = new PdfAppearance(stamp);

            // Set the template as the normal state of the appearance
            pdfAppearance.Normal = template;

            // Apply the appearance to the stamp
            stamp.Appearance = pdfAppearance;

            // Add the stamp annotation to PDF
            page.Annotations.Add(stamp);

            // Save the file
            doc.SaveToFile("ImageStamp.pdf");

            // Dispose resources
            doc.Dispose();
        }
    }
}

C#: Add a Stamp to a PDF Document

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.

When we work with Excel pie chart, we may need to separate each part of pie chart to make them stand out. Spire.XLS offers a property of Series.DataFormat.Percent to enable developers to pull the whole pie apart. It also offers a property of Series.DataPoints.DataFormat.Percent to pull apart a single slice from the whole pie chart.

This article is going to introduce the method of how to set the separation width between slices in pie chart in C# by using Spire.XLS.

On MS Excel, We can adjust the percentage of "Pie Explosion" on the Series Options at the "format data series" area to control the width between each section in the chart.

How to explode a pie chart sections in C#

Code Snippet of how to set the separation width between slices in pie chart.

using Spire.Xls;
namespace ExplodePieChart
{

    class Program
    {

        static void Main(string[] args)
        {
            Workbook workbook = new Workbook();
            workbook.LoadFromFile("Sample.xlsx");

            Worksheet ws = workbook.Worksheets[0];
            Chart chart = ws.Charts[0];

            // Set the separation width between slices in pie chart.
            for (int i = 0; i < chart.Series.Count; i++)
            {
                chart.Series[i].DataFormat.Percent = 20;
            }

            workbook.SaveToFile("result.xlsx", ExcelVersion.Version2010);

        }
    }
}

Effective screenshot after pull the whole pie apart.

How to explode a pie chart sections in C#

Code Snippet of how to split a single slice from the whole pie chart.

using Spire.Xls;
namespace ExplodePieChart
{

    class Program
    {

        static void Main(string[] args)
        {
            {
                Workbook workbook = new Workbook();
                workbook.LoadFromFile("Sample.xlsx");

                Worksheet ws = workbook.Worksheets[0];
                Chart chart = ws.Charts[0];

                chart.Series[0].DataPoints[0].DataFormat.Percent = 20;

                workbook.SaveToFile("ExplodePieChart.xlsx", ExcelVersion.Version2013);
            }


        }
    }
}

Effective screenshot after pull a single part from the pie chart apart.

How to explode a pie chart sections in C#

Video is often used to create a more attractive and interesting effect in PowerPoint, there are two modes to play video in PowerPoint: on click and automatically. In general, the video would be played by clicking on it, people who want to play the video automatically needs to set the default play mode as auto style. This article will demonstrate how to set the play mode for video in PowerPoint using C#, VB.NET.

Note: Before start, please download and install Spire.Presentation correctly, after that add the Spire.Presentation.dll file as the reference of your project.

Detail steps overview:

Step 1: Initialize a new presentation instance and load the original document from file.

Presentation presentation = new Presentation();
presentation.LoadFromFile(@"C:\Users\Administrator\Desktop\Sample.pptx");

Step 2: Find the video by looping through all the slides and set its play mode as auto.

foreach (ISlide slide in presentation.Slides)
{
    foreach (IShape shape in slide.Shapes)
        {
            if (shape is IVideo)
            {
                (shape as IVideo).PlayMode = VideoPlayMode.Auto;
            }
        }
}

Step 3: Save the file as Video.pptx.

presentation.SaveToFile("Video.pptx", FileFormat.Pptx2010);

Full codes:

[C#]
using Spire.Presentation;

namespace Set_Video_Play_Mode
{
    class Program
    {
        static void Main(string[] args)
        {
            Presentation presentation = new Presentation();
            presentation.LoadFromFile(@"C:\Users\Administrator\Desktop\Sample.pptx");
            foreach (ISlide slide in presentation.Slides)
            {
                foreach (IShape shape in slide.Shapes)
                {
                    if (shape is IVideo)
                    {
                        (shape as IVideo).PlayMode = VideoPlayMode.Auto;
                    }
                }
            }
            presentation.SaveToFile("Video.pptx", FileFormat.Pptx2010);
        }
    }
}
[VB.NET]
Imports Spire.Presentation
Namespace Set_Video_Play_Mode
	Class Program
		Private Shared Sub Main(args As String())
			Dim presentation As New Presentation()
	presentation.LoadFromFile("C:\Users\Administrator\Desktop\Sample.pptx")
			For Each slide As ISlide In presentation.Slides
				For Each shape As IShape In slide.Shapes
					If TypeOf shape Is IVideo Then
						TryCast(shape, IVideo).PlayMode = VideoPlayMode.Auto
					End If
				Next
			Next
			presentation.SaveToFile("Video.pptx", FileFormat.Pptx2010)
		End Sub
	End Class
End Namespace

If you couldn't use Spire.Presentation successfully, please refer the Spire.Presentation Quick Start which will guide you to quickly use the Spire.Presentation.

The different border color and styles on the Excel Chart can distinguish the chart categories easily. Spire.XLS offers a property of LineProperties to enables developers to set the color and styles for the data point. This article is going to introduce the method of how to format data series for Excel charts in C# using Spire.XLS.

Note: Before Start, please download the latest version of Spire.XLS and add Spire.xls.dll in the bin folder as the reference of Visual Studio.

Firstly, please check the original screenshot of excel chart with the automatic setting for border.

How to set the border color and styles for Excel Chart

Code Snippet of how to set the border color and border styles for Excel chart data series.

Step 1: Create a new workbook and load from file.

Workbook workbook = new Workbook();
workbook.LoadFromFile("sample.xlsx");

Step 2: Get the first worksheet from workbook and then get the first chart from the worksheet.

Worksheet ws = workbook.Worksheets[0];
Chart chart = ws.Charts[0];

Step 3: Set CustomLineWeight property for Series line.

(chart.Series[0].DataPoints[0].DataFormat.LineProperties as XlsChartBorder).CustomLineWeight = 1.5f;

Step 4: Set Color property for Series line.

(chart.Series[0].DataPoints[0].DataFormat.LineProperties as XlsChartBorder).Color = Color.Red;

Step 5: Save the document to file.

workbook.SaveToFile("result.xlsx", FileFormat.Version2013);

Effective screenshot after set the color and width of excel chart border.

How to set the border color and styles for Excel Chart

Full codes:

using Spire.Xls;
using Spire.Xls.Charts;
using Spire.Xls.Core.Spreadsheet.Charts;
using System.Drawing;
namespace SetBoarderColor
{

    class Program
    {

        static void Main(string[] args)
        {
            Workbook workbook = new Workbook();
            workbook.LoadFromFile("sample.xlsx");
            Worksheet ws = workbook.Worksheets[0];
            Chart chart = ws.Charts[0];

            (chart.Series[0].DataPoints[0].DataFormat.LineProperties as XlsChartBorder).CustomLineWeight = 1.5f;
            (chart.Series[0].DataPoints[0].DataFormat.LineProperties as XlsChartBorder).Color = Color.Red;

            workbook.SaveToFile("result.xlsx", FileFormat.Version2013);


        }
    }
}

In MS Excel, the spaces between data bars have been defined as Series Overlap and Gap Width.

  • Series Overlap: Spaces between data series within a single category.
  • Gap Width: Spaces between two categories.

Check below picture, you'll have a better understanding of these two concepts. Normally the spaces are automatically calculated based on the date and chart area, the space may be very narrow or wide depending on how many date series you have in a fixed chart area. In this article, we'll introduce how to adjust the spaces between data bars using Spire.XLS.

How to Adjust the Spaces between Bars in Excel Chart in C#, VB.NET

Code Snippet:

Step 1: Initialize a new instance of Wordbook class and load the sample Excel file that contains some data in A1 to C5.

Workbook workbook = new Workbook();
workbook.LoadFromFile("sample.xlsx");

Step 2: Create a Column chart based on the data in cell range A1 to C5.

Worksheet sheet = workbook.Worksheets[0];           
Chart chart = sheet.Charts.Add(ExcelChartType.ColumnClustered);
chart.DataRange = sheet.Range["A1:C5"];
chart.SeriesDataFromRange = false;
chart.PrimaryValueAxis.MajorGridLines.LineProperties.Color = Color.LightGray;

Step 3: Set chart position.

chart.LeftColumn = 5;
chart.TopRow = 7;
chart.RightColumn = 13;
chart.BottomRow = 21;

Step 4: The ChartSerieDataFormat class has two properties - GapWidth property and Overlap property to handle the Gap Width and Series Overlap respectively. The value of GapWidth varies from 0 to 500, and the value of Overlap varies from -100 to 100.

foreach (ChartSerie cs in chart.Series)
{
    cs.Format.Options.GapWidth = 200;
    cs.Format.Options.Overlap = 0;
}

Step 5: Save and launch the file.

workbook.SaveToFile("result.xlsx", ExcelVersion.Version2010);
System.Diagnostics.Process.Start("result.xlsx");

Output:

How to Adjust the Spaces between Bars in Excel Chart in C#, VB.NET

Full Code:

[C#]
using Spire.Xls;
using Spire.Xls.Charts;
namespace AdjustSpaces
{

    class Program
    {

        static void Main(string[] args)
        {
            Workbook workbook = new Workbook();
            workbook.LoadFromFile("data.xlsx");

            Worksheet sheet = workbook.Worksheets[0];
            Chart chart = sheet.Charts.Add(ExcelChartType.ColumnClustered);
            chart.DataRange = sheet.Range["A1:C5"];
            chart.SeriesDataFromRange = false;
            chart.PrimaryValueAxis.MajorGridLines.LineProperties.Color = Color.LightGray;

            chart.LeftColumn = 5;
            chart.TopRow = 7;
            chart.RightColumn = 13;
            chart.BottomRow = 21;

            foreach (ChartSerie cs in chart.Series)
            {
                cs.Format.Options.GapWidth = 200;
                cs.Format.Options.Overlap = 0;
            }

            workbook.SaveToFile("result.xlsx", ExcelVersion.Version2010);
            System.Diagnostics.Process.Start("result.xlsx");

        }
    }
}
[VB.NET]
Imports Spire.Xls
Imports Spire.Xls.Charts
Namespace AdjustSpaces

	Class Program

		Private Shared Sub Main(args As String())
			Dim workbook As New Workbook()
			workbook.LoadFromFile("data.xlsx")

			Dim sheet As Worksheet = workbook.Worksheets(0)
			Dim chart As Chart = sheet.Charts.Add(ExcelChartType.ColumnClustered)
			chart.DataRange = sheet.Range("A1:C5")
			chart.SeriesDataFromRange = False
			chart.PrimaryValueAxis.MajorGridLines.LineProperties.Color = Color.LightGray

			chart.LeftColumn = 5
			chart.TopRow = 7
			chart.RightColumn = 13
			chart.BottomRow = 21

			For Each cs As ChartSerie In chart.Series
				cs.Format.Options.GapWidth = 200
				cs.Format.Options.Overlap = 0
			Next

			workbook.SaveToFile("result.xlsx", ExcelVersion.Version2010)
			System.Diagnostics.Process.Start("result.xlsx")

		End Sub
	End Class
End Namespace

When dealing with an Excel worksheet containing a large amount of data, splitting it into several separate Excel files based on specific criteria can be beneficial. By dividing the worksheet into smaller, more manageable files, you can improve your work efficiency and make data analysis easier. This article will demonstrate how to programmatically split an Excel worksheet into multiple Excel files 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

Split an Excel Sheet into Multiple Files in C# and VB.NET

The Worksheet.Copy(CellRange sourceRange, CellRange destRange) method provided by Spire.XLS for .NET allows you to split a worksheet by copying a specified cell range from the original Excel file to a new Excel file. The following are the detailed steps.

  • Create a Workbook instance.
  • Load a sample Excel file using Workbook.LoadFromFile() method.
  • Get a specific worksheet using Workbook.Worksheets[] property.
  • Get the header row and the specified cell ranges using Worksheet.Range property.
  • Create a new workbook and get its first worksheet.
  • Copy the header row and specified cell range to the first worksheet of the new workbook using Worksheet.Copy(CellRange sourceRange, CellRange destRange) method.
  • Copy the column width from the original workbook to the new workbook, and then save the new workbook to an Excel file using Workbook.SaveToFile() method.
  • Create another new workbook, and then repeat the above steps to copy the header row and specified cell range into the new workbook.
  • Save the new workbook to another Excel file.
  • C#
  • VB.NET
using Spire.Xls;

namespace splitworksheet
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Workbook instance 
            Workbook originalBook= new Workbook();

            //Load the original Excel document from file
            originalBook.LoadFromFile("Info.xlsx");

            //Get the first worksheet
            Worksheet sheet = originalBook.Worksheets[0];

            //Get the header row
            CellRange headerRow = sheet.Range[1, 1, 1,5];

            //Get two cell ranges
            CellRange range1 = sheet.Range[2, 1, 6, sheet.LastColumn];
            CellRange range2 = sheet.Range[7, 1, sheet.LastRow, sheet.LastColumn];

            //Create a new workbook
            Workbook newBook1 = new Workbook();

            //Get the first worksheet of new workbook
            Worksheet newSheet1 = newBook1.Worksheets[0];

            //Copy the header row and range 1 to the first worksheet of the new workbook
            sheet.Copy(headerRow, newSheet1.Range[1, 1]);
            sheet.Copy(range1, newSheet1.Range[2, 1]);

            //Copy the column width from the original workbook to the new workbook
            for (int i = 0; (i < sheet.LastColumn); i++)
            {
                newBook1.Worksheets[0].SetColumnWidth(i + 1, sheet.GetColumnWidth(i + 1));
            }

            //Save the new workbook to an Excel file
            newBook1.SaveToFile("Sales Depart.xlsx", ExcelVersion.Version2016);

            //Create another new workbook
            Workbook newBook2 = new Workbook();

            //Get the first worksheet of new workbook
            Worksheet newSheet2 = newBook2.Worksheets[0];

            //Copy the header row and range 2 to the first worksheet of the new workbook
            sheet.Copy(headerRow, newSheet2.Range[1, 1]);
            sheet.Copy(range2, newSheet2.Range[2, 1]);

            //Copy the column width from the original workbook to the new workbook
            for (int i = 0; (i < sheet.LastColumn); i++)
            {
                newBook2.Worksheets[0].SetColumnWidth(i + 1, sheet.GetColumnWidth(i + 1));
            }

            //Save the new workbook to another Excel file
            newBook2.SaveToFile("Development Depart.xlsx", ExcelVersion.Version2016);

        }
    }
}

C#/VB.NET: Split an Excel Worksheet into Multiple Files

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.

Gridlines are often added to charts to help improve the readability of the chart itself, but it is not a necessary to display the gridlines in every chart especially when we do not need to know the exact value of each data point from graphic. This article will present how to hide gridlines in Excel chart using Spire.XLS.

Code Snippet:

Step 1: Initialize a new instance of Workbook class and load a sample Excel file that contains some data in A1 to C5.

Workbook workbook = new Workbook();
workbook.LoadFromFile("data.xlsx");

Step 2: Create a Column chart based on the data in cell range A1 to C5.

Worksheet sheet = workbook.Worksheets[0];           
Chart chart = sheet.Charts.Add(ExcelChartType.ColumnClustered);
chart.DataRange = sheet.Range["A1:C5"];
chart.SeriesDataFromRange = false;

Step 3: Set chart position.

chart.LeftColumn = 1;
chart.TopRow = 6;
chart.RightColumn = 8
chart.BottomRow = 19;

Step 4: Set the PrimaryValueAxis.HasMajorGridLines property to false.

chart.PrimaryValueAxis.HasMajorGridLines = false;

Step 5: Save and launch the file.

workbook.SaveToFile("result.xlsx", ExcelVersion.Version2010);
System.Diagnostics.Process.Start("result.xlsx");

Output:

How to Hide Gridlines in Excel Chart in C#, VB.NET

Full Code:

[C#]
using Spire.Xls;
namespace HideGridLine
{

    class Program
    {

        static void Main(string[] args)
        {
            Workbook workbook = new Workbook();
            workbook.LoadFromFile("data.xlsx");

            Worksheet sheet = workbook.Worksheets[0];
            Chart chart = sheet.Charts.Add(ExcelChartType.ColumnClustered);
            chart.DataRange = sheet.Range["A1:C5"];
            chart.SeriesDataFromRange = false;
            chart.LeftColumn = 1;
            chart.TopRow = 6;
            chart.RightColumn = 8;
            chart.BottomRow = 19;

            chart.PrimaryValueAxis.HasMajorGridLines = false;

            workbook.SaveToFile("result.xlsx", ExcelVersion.Version2010);
            System.Diagnostics.Process.Start("result.xlsx");
        }

    }
}
[VB.NET]
Imports Spire.Xls
Namespace HideGridLine

	Class Program

		Private Shared Sub Main(args As String())
			Dim workbook As New Workbook()
			workbook.LoadFromFile("data.xlsx")

			Dim sheet As Worksheet = workbook.Worksheets(0)
			Dim chart As Chart = sheet.Charts.Add(ExcelChartType.ColumnClustered)
			chart.DataRange = sheet.Range("A1:C5")
			chart.SeriesDataFromRange = False
			chart.LeftColumn = 1
			chart.TopRow = 6
			chart.RightColumn = 8
			chart.BottomRow = 19

			chart.PrimaryValueAxis.HasMajorGridLines = False

			workbook.SaveToFile("result.xlsx", ExcelVersion.Version2010)
			System.Diagnostics.Process.Start("result.xlsx")
		End Sub

	End Class
End Namespace

The leader line on Excel chart is very helpful since it gives a visual connection between a data label and its corresponding data point. Spire.XLS offers a property of DataLabels.ShowLeaderLines to enable developers to show or hide the leader lines easily. This article will focus on demonstrating how to show the leader line on Excel stacked bar chart in C#.

Note: Before Start, please ensure that you have download the latest version of Spire.XLS (V7.8.64 or above) and add Spire.xls.dll in the bin folder as the reference of Visual Studio.

Here comes to the code snippet of how to show the leader line on Excel stacked bar chart in C#.

Step 1: Create a new excel document instance and get the first worksheet.

Workbook book = new Workbook();
Worksheet sheet = book.Worksheets[0];

Step 2: Add some data to the Excel sheet cell range.

sheet.Range["A1"].Value = "1";
sheet.Range["A2"].Value = "2";
sheet.Range["A3"].Value = "3";
sheet.Range["B1"].Value = "4";
sheet.Range["B2"].Value = "5";
sheet.Range["B3"].Value = "6";

Step 3: Create a bar chart and define the data for it.

Chart chart = sheet.Charts.Add(ExcelChartType.BarStacked);
chart.DataRange = sheet.Range["A1:B3"];

Step 4: Set the property of HasValue and ShowLeaderLines for DataLabels.

foreach (ChartSerie cs in chart.Series)
  {
    cs.DataPoints.DefaultDataPoint.DataLabels.HasValue = true; 
    cs.DataPoints.DefaultDataPoint.DataLabels.ShowLeaderLines = true;
  }

Step 5: Save the document to file and set the excel version.

book.Version = ExcelVersion.Version2013;
book.SaveToFile("result.xlsx", FileFormat.Version2013);

Effective screenshots:

How to show the leader line on Excel chart in C#

Full codes:

using Spire.Xls;
using Spire.Xls.Charts;
using Spire.Xls.Core.Spreadsheet.Charts;
using System.Drawing;
namespace ShowLeaderLine
{
    class Program
    {

        static void Main(string[] args)
        {
            Workbook book = new Workbook();
            Worksheet sheet = book.Worksheets[0];

            sheet.Range["A1"].Value = "1";
            sheet.Range["A2"].Value = "2";
            sheet.Range["A3"].Value = "3";
            sheet.Range["B1"].Value = "4";
            sheet.Range["B2"].Value = "5";
            sheet.Range["B3"].Value = "6";

            Chart chart = sheet.Charts.Add(ExcelChartType.BarStacked);
            chart.DataRange = sheet.Range["A1:B3"];

            foreach (ChartSerie cs in chart.Series)
            {
                cs.DataPoints.DefaultDataPoint.DataLabels.HasValue = true;
                cs.DataPoints.DefaultDataPoint.DataLabels.ShowLeaderLines = true;
            }

            book.Version = ExcelVersion.Version2013;
            book.SaveToFile("result.xlsx", FileFormat.Version2013);
        }

    }
}
page 45