page 232

Sometimes, we need to copy the data with formatting from one cell range (a row or a column) to another. It is an extremely easy work in MS Excel, because we can select the source range and then use Copy and Paste function to insert the same data in destination cells.

In fact, Spire.XLS has provided two methods Worksheet.Copy(CellRange sourceRange, CellRange destRange, bool copyStyle) and Worksheet.Copy(CellRange sourceRange, Worksheet worksheet, int destRow, int destColumn, bool copyStyle) that allow programmers to copy rows and columns within or between workbooks. This article will present how to duplicate a row within a workbook using Spire.XLS.

Test File:

How to Duplicate a Row in Excel in C#, VB.NET

Code Snippet:

Step 1: Create a new instance of Workbook class and load the sample file.

Workbook book = new Workbook();
book.LoadFromFile("sample.xlsx", ExcelVersion.Version2010);

Step 2: Get the first worksheet.

Worksheet sheet = book.Worksheets[0];

Step 3: Call Worksheet.Copy(CellRange sourceRange, CellRange destRange, bool copyStyle) method to copy data from source range (A1:G1) to destination range (A4:G4) and maintain the formatting.

sheet.Copy(sheet.Range["A1:G1"], sheet.Range["A4:G4"],true);

Step 4: Save and launch the file.

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

Output:

How to Duplicate a Row in Excel in C#, VB.NET

Full Code:

[C#]
using Spire.Xls;
namespace DuplicateRowExcel
{
    class Program
    {

        static void Main(string[] args)
        {
            Workbook book = new Workbook();
            book.LoadFromFile("sample.xlsx", ExcelVersion.Version2010);
            Worksheet sheet = book.Worksheets[0];

            sheet.Copy(sheet.Range["A1:G1"], sheet.Range["A4:G4"], true);

            book.SaveToFile("result.xlsx", ExcelVersion.Version2010);
            System.Diagnostics.Process.Start("result.xlsx");
        }
    }
}
[VB.NET]
Imports Spire.Xls
Namespace DuplicateRowExcel
	Class Program

		Private Shared Sub Main(args As String())
			Dim book As New Workbook()
			book.LoadFromFile("sample.xlsx", ExcelVersion.Version2010)
			Dim sheet As Worksheet = book.Worksheets(0)

			sheet.Copy(sheet.Range("A1:G1"), sheet.Range("A4:G4"), True)

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

With the help of Spire.Presentation for .NET, we can easily save PowerPoint slides as image in C# and VB.NET. Sometimes, we need to use the resulted images for other purpose and the image size becomes very important. To ensure the image is easy and beautiful to use, we need to set the image with specified size beforehand. This example will demonstrate how to save a particular presentation slide as image with specified size by using Spire.Presentation for your .NET applications.

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

Step 1: Create a presentation document and load the document from file.

Presentation presentation = new Presentation();
presentation.LoadFromFile("sample.pptx");

Step 2: Save the first slide to Image and set the image size to 600*400.

Image img = presentation.Slides[0].SaveAsImage(600, 400);

Step 3: Save image to file.

img.Save("result.png",System.Drawing.Imaging.ImageFormat.Png);

Effective screenshot of the resulted image with specified size:

Save a PowerPoint Slide as Image with Specified Size

Full codes:

using Spire.Presentation;
using System.Drawing;
namespace SavePowerPointSlideasImage
{
    class Program
    {
        static void Main(string[] args)
        {
            Presentation presentation = new Presentation();
            presentation.LoadFromFile("sample.pptx");

            Image img = presentation.Slides[0].SaveAsImage(600, 400);
            img.Save("result.png", System.Drawing.Imaging.ImageFormat.Png);
        }
    }

There are many kinds of areas in a chart, such as chart area, plot area, legend area. Spire.XLS offers properties to set the performance of each area easily in C# and VB.NET. We have already shown you how to set the background color and image for chart area and plot area in C#. This article will show you how to set the background color for chart legend in C# with the help of Spire.XLS 7.8.43 or above.

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

How to Set the Background Color of Legend in an Excel Chart

Code Snippet of how to set the background color of legend in a Chart:

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: Change the background color of the legend in a chart and specify a Solid Fill of SkyBlue.

XlsChartFrameFormat x = chart.Legend.FrameFormat as XlsChartFrameFormat;
x.Fill.FillType = ShapeFillType.SolidColor;
x.ForeGroundColor = Color.SkyBlue;

Step 4: Save the document to file.

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

Effective screenshot after fill the background color for Excel chart legend:

How to Set the Background Color of Legend in an Excel Chart

Full codes:

using Spire.Xls;
using Spire.Xls.Core.Spreadsheet.Charts;
using System.Drawing;
namespace SetBackgroundColor
{
    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];

            XlsChartFrameFormat x = chart.Legend.FrameFormat as XlsChartFrameFormat;
            x.Fill.FillType = ShapeFillType.SolidColor;
            x.ForeGroundColor = Color.SkyBlue;

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