Skip to content

Commit d9da3f5

Browse files
authored
Merge pull request #185 from SyncfusionExamples/926787-PositionChart
926787-Position a Chart in Excel
2 parents 16606c1 + 9216e56 commit d9da3f5

File tree

5 files changed

+156
-0
lines changed

5 files changed

+156
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.12.35506.116 d17.12
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Position Chart", "Position Chart\Position Chart.csproj", "{8E50C960-D43F-4F91-903D-5F818E7C4C7B}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{8E50C960-D43F-4F91-903D-5F818E7C4C7B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{8E50C960-D43F-4F91-903D-5F818E7C4C7B}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{8E50C960-D43F-4F91-903D-5F818E7C4C7B}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{8E50C960-D43F-4F91-903D-5F818E7C4C7B}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
EndGlobal

Create and Edit Charts/Position Chart/.NET/Position Chart/Position Chart/Output/.gitkeep

Whitespace-only changes.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Position_Chart</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Syncfusion.XlsIO.Net.Core" Version="*" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<None Update="Output\*">
17+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
18+
</None>
19+
</ItemGroup>
20+
21+
</Project>
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using System;
2+
using System.IO;
3+
using Syncfusion.XlsIO;
4+
5+
namespace Position_Chart
6+
{
7+
class Program
8+
{
9+
static void Main(string[] args)
10+
{
11+
using (ExcelEngine excelEngine = new ExcelEngine())
12+
{
13+
IApplication application = excelEngine.Excel;
14+
application.DefaultVersion = ExcelVersion.Xlsx;
15+
IWorkbook workbook = application.Workbooks.Create(1);
16+
IWorksheet worksheet = workbook.Worksheets[0];
17+
18+
//Add data
19+
worksheet.Range["A1"].Text = "Category";
20+
worksheet.Range["B1"].Text = "Value";
21+
worksheet.Range["A2"].Text = "A";
22+
worksheet.Range["A3"].Text = "B";
23+
worksheet.Range["A4"].Text = "C";
24+
worksheet.Range["B2"].Number = 10;
25+
worksheet.Range["B3"].Number = 20;
26+
worksheet.Range["B4"].Number = 30;
27+
28+
//Add a chart
29+
IChartShape chart = worksheet.Charts.Add();
30+
chart.DataRange = worksheet.Range["A1:B4"];
31+
chart.ChartType = ExcelChartType.Column_Clustered;
32+
33+
//Set chart position
34+
chart.Top = 100;
35+
chart.Left = 150;
36+
37+
//Set height and width
38+
IChart chart1 = worksheet.Charts[0];
39+
chart1.Height = 300;
40+
chart1.Width = 500;
41+
42+
#region Save
43+
//Saving the workbook
44+
FileStream outputStream = new FileStream(Path.GetFullPath("Output.xlsx"), FileMode.Create, FileAccess.Write);
45+
workbook.SaveAs(outputStream);
46+
#endregion
47+
48+
//Dispose streams
49+
outputStream.Dispose();
50+
}
51+
}
52+
}
53+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Positioning a Chart
2+
3+
Step 1: Create a New C# Console Application Project.
4+
5+
Step 2: Name the Project.
6+
7+
Step 3: Install the [Syncfusion.XlsIO.Net.Core](https://www.nuget.org/packages/Syncfusion.XlsIO.Net.Core) NuGet package as reference to your .NET Standard applications from [NuGet.org](https://www.nuget.org).
8+
9+
Step 4: Include the following namespaces in the **Program.cs** file.
10+
11+
```csharp
12+
using System;
13+
using System.IO;
14+
using Syncfusion.XlsIO;
15+
```
16+
17+
Step 5: Include the below code snippet in **Program.cs** to position a chart in Excel.
18+
19+
```csharp
20+
using (ExcelEngine excelEngine = new ExcelEngine())
21+
{
22+
IApplication application = excelEngine.Excel;
23+
application.DefaultVersion = ExcelVersion.Xlsx;
24+
IWorkbook workbook = application.Workbooks.Create(1);
25+
IWorksheet worksheet = workbook.Worksheets[0];
26+
27+
//Add data
28+
worksheet.Range["A1"].Text = "Category";
29+
worksheet.Range["B1"].Text = "Value";
30+
worksheet.Range["A2"].Text = "A";
31+
worksheet.Range["A3"].Text = "B";
32+
worksheet.Range["A4"].Text = "C";
33+
worksheet.Range["B2"].Number = 10;
34+
worksheet.Range["B3"].Number = 20;
35+
worksheet.Range["B4"].Number = 30;
36+
37+
//Add a chart
38+
IChartShape chart = worksheet.Charts.Add();
39+
chart.DataRange = worksheet.Range["A1:B4"];
40+
chart.ChartType = ExcelChartType.Column_Clustered;
41+
42+
//Set chart position
43+
chart.Top = 100;
44+
chart.Left = 150;
45+
46+
//Set height and width
47+
IChart chart1 = worksheet.Charts[0];
48+
chart1.Height = 300;
49+
chart1.Width = 500;
50+
51+
#region Save
52+
//Saving the workbook
53+
FileStream outputStream = new FileStream(Path.GetFullPath("Output.xlsx"), FileMode.Create, FileAccess.Write);
54+
workbook.SaveAs(outputStream);
55+
#endregion
56+
57+
//Dispose streams
58+
outputStream.Dispose();
59+
}
60+
```

0 commit comments

Comments
 (0)