Skip to content

Commit b6476e4

Browse files
AaronRobinsonMSFTmairaw
authored andcommitted
Add demo for Excel automation in .NET Core 3.0 (dotnet#487)
* Add demo for Excel automation in .NET Core 3.0 * Feedback * Update core/extensions/ExcelDemo/ReadMe.md Co-Authored-By: AaronRobinsonMSFT <[email protected]> * Update core/extensions/ExcelDemo/ReadMe.md Co-Authored-By: AaronRobinsonMSFT <[email protected]> * Feedback
1 parent c3774d6 commit b6476e4

File tree

3 files changed

+152
-0
lines changed

3 files changed

+152
-0
lines changed
+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp3.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
<!--
9+
The following 'COMReference' items were copied from a .NET Framework project. They were added by using the Visual Studio COM References window. See https://docs.microsoft.com/en-us/visualstudio/ide/managing-references-in-a-project?view=vs-2017.
10+
11+
Observe the 'EmbedInteropTypes' tag value.
12+
See https://docs.microsoft.com/en-us/visualstudio/msbuild/common-msbuild-project-items?view=vs-2017#comreference
13+
-->
14+
<ItemGroup>
15+
<COMReference Include="Microsoft.Office.Core">
16+
<Guid>{2DF8D04C-5BFA-101B-BDE5-00AA0044DE52}</Guid>
17+
<VersionMajor>2</VersionMajor>
18+
<VersionMinor>8</VersionMinor>
19+
<Lcid>0</Lcid>
20+
<WrapperTool>primary</WrapperTool>
21+
<Isolated>False</Isolated>
22+
<EmbedInteropTypes>True</EmbedInteropTypes>
23+
</COMReference>
24+
<COMReference Include="Microsoft.Office.Interop.Excel">
25+
<Guid>{00020813-0000-0000-C000-000000000046}</Guid>
26+
<VersionMajor>1</VersionMajor>
27+
<VersionMinor>9</VersionMinor>
28+
<Lcid>0</Lcid>
29+
<WrapperTool>primary</WrapperTool>
30+
<Isolated>False</Isolated>
31+
<EmbedInteropTypes>True</EmbedInteropTypes>
32+
</COMReference>
33+
<COMReference Include="VBIDE">
34+
<Guid>{0002E157-0000-0000-C000-000000000046}</Guid>
35+
<VersionMajor>5</VersionMajor>
36+
<VersionMinor>3</VersionMinor>
37+
<Lcid>0</Lcid>
38+
<WrapperTool>primary</WrapperTool>
39+
<Isolated>False</Isolated>
40+
<EmbedInteropTypes>True</EmbedInteropTypes>
41+
</COMReference>
42+
</ItemGroup>
43+
44+
</Project>

core/extensions/ExcelDemo/Program.cs

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
using System;
2+
using System.Reflection;
3+
using Excel = Microsoft.Office.Interop.Excel;
4+
5+
namespace ExcelDemo
6+
{
7+
class Program
8+
{
9+
public static void Main(string[] args)
10+
{
11+
Excel.Application excel;
12+
Excel.Workbook workbook;
13+
Excel.Worksheet sheet;
14+
Excel.Range range;
15+
16+
try
17+
{
18+
// Start Excel and get Application object.
19+
excel = new Excel.Application();
20+
excel.Visible = true;
21+
22+
// Get a new workbook.
23+
workbook = excel.Workbooks.Add(Missing.Value);
24+
sheet = (Excel.Worksheet)workbook.ActiveSheet;
25+
26+
// Add table headers going cell by cell.
27+
sheet.Cells[1, 1] = "First Name";
28+
sheet.Cells[1, 2] = "Last Name";
29+
sheet.Cells[1, 3] = "Full Name";
30+
sheet.Cells[1, 4] = "Salary";
31+
32+
// Format A1:D1 as bold, vertical alignment = center.
33+
sheet.get_Range("A1", "D1").Font.Bold = true;
34+
sheet.get_Range("A1", "D1").VerticalAlignment =
35+
Excel.XlVAlign.xlVAlignCenter;
36+
37+
// Create an array to multiple values at once.
38+
string[,] saNames = new string[5, 2];
39+
40+
saNames[0, 0] = "John";
41+
saNames[0, 1] = "Smith";
42+
saNames[1, 0] = "Tom";
43+
saNames[1, 1] = "Brown";
44+
saNames[2, 0] = "Sue";
45+
saNames[2, 1] = "Thomas";
46+
saNames[3, 0] = "Jane";
47+
saNames[3, 1] = "Jones";
48+
saNames[4, 0] = "Adam";
49+
saNames[4, 1] = "Johnson";
50+
51+
// Fill A2:B6 with an array of values (First and Last Names).
52+
sheet.get_Range("A2", "B6").Value2 = saNames;
53+
54+
// Fill C2:C6 with a relative formula (=A2 & " " & B2).
55+
range = sheet.get_Range("C2", "C6");
56+
range.Formula = "=A2 & \" \" & B2";
57+
58+
// Fill D2:D6 with a formula(=RAND()*100000) and apply format.
59+
range = sheet.get_Range("D2", "D6");
60+
range.Formula = "=RAND()*100000";
61+
range.NumberFormat = "$0.00";
62+
63+
// AutoFit columns A:D.
64+
range = sheet.get_Range("A1", "D1");
65+
range.EntireColumn.AutoFit();
66+
67+
// Make sure Excel is visible and give the user control
68+
// of Microsoft Excel's lifetime.
69+
excel.Visible = true;
70+
excel.UserControl = true;
71+
}
72+
catch (Exception e)
73+
{
74+
Console.WriteLine($"Error: {e.Message} Line: {e.Source}");
75+
}
76+
}
77+
}
78+
}

core/extensions/ExcelDemo/ReadMe.md

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
Excel Demo
2+
================
3+
4+
This is a basic Excel demo sample in .NET Core. It is designed to work with .NET Core 3.0. It is based on the [How to automate Microsoft Excel from Microsoft Visual C#.NET](https://support.microsoft.com/en-us/help/302084/how-to-automate-microsoft-excel-from-microsoft-visual-c-net) sample for .NET Framework.
5+
6+
Key Features
7+
------------
8+
9+
Demonstrates how to consume Office Primary Interop Assemblies (PIA) with .NET Core 3.0 Preview 1.
10+
11+
- Embedding of Interop types (i.e. [No-PIA](https://docs.microsoft.com/en-us/dotnet/framework/interop/type-equivalence-and-embedded-interop-types)).
12+
- Support for the [`IDispatch`](https://docs.microsoft.com/en-us/windows/desktop/winauto/idispatch-interface) interface.
13+
14+
**Note** Adding COM references to .NET Core projects from Visual Studio is not currently supported. The workaround is to create a .NET Framework project, add the COM references, and then copy the relevant `COMReference` elements in the project. See `ExcelDemo.csproj` for further details.
15+
16+
Build and Run
17+
-------------
18+
19+
To build and run the sample, the project must be loaded in Visual Studio 2017. Build support for `COMReference` elements is not supported in the `dotnet` tool for the Preview 1 release, but the scenario is demoable from within Visual Studio 2017.
20+
21+
1) Install .NET Core 3.0 Preview 1.
22+
23+
1) Load `ExcelDemo.csproj` in Visual Studio 2017.
24+
- Double click on `ExcelDemo.csproj` in File Explorer.
25+
26+
or
27+
28+
- Open a Developer Command prompt and open with `devenv.exe ExcelDemo.csproj`.
29+
30+
1) Press F5 to build and debug the project.

0 commit comments

Comments
 (0)