کار با اشیاء COM در NET Core.
نویسنده: وحید نصیری
تاریخ: ۱۳۹۸/۰۶/۱۸ ۱۳:۳۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
dynamic excel = Activator.CreateInstance(Type.GetTypeFromProgID("Excel.Application", true));
excel.Visible = true;
Console.WriteLine("Press Enter to close Excel.");
Console.ReadLine();
excel.Quit(); System.__ComObject does not contain a definition for 'Visible'
using Excel = Microsoft.Office.Interop.Excel;
// ...
var excel = new Excel.Application();
excel.Visible = true;
Console.WriteLine("Press Enter to close Excel.");
Console.ReadLine();
excel.Quit(); <ItemGroup>
<Reference Include="Interop.WIA">
<HintPath>..\DNTScanner.Core.TypeLibrary\bin\Debug\Interop.WIA.dll</HintPath>
<EmbedInteropTypes>True</EmbedInteropTypes>
</Reference>
</ItemGroup> <ItemGroup Condition=" '$(TargetFramework)' == 'net40' ">
<Reference Include="Microsoft.CSharp" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="Microsoft.CSharp" Version="4.5.0" />
</ItemGroup> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.0</TargetFramework>
</PropertyGroup>
<!--
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.
Observe the 'EmbedInteropTypes' tag value.
See https://docs.microsoft.com/en-us/visualstudio/msbuild/common-msbuild-project-items?view=vs-2017#comreference
-->
<ItemGroup>
<COMReference Include="Microsoft.Office.Core">
<Guid>{2DF8D04C-5BFA-101B-BDE5-00AA0044DE52}</Guid>
<VersionMajor>2</VersionMajor>
<VersionMinor>8</VersionMinor>
<Lcid>0</Lcid>
<WrapperTool>primary</WrapperTool>
<Isolated>False</Isolated>
<EmbedInteropTypes>True</EmbedInteropTypes>
</COMReference>
<COMReference Include="Microsoft.Office.Interop.Excel">
<Guid>{00020813-0000-0000-C000-000000000046}</Guid>
<VersionMajor>1</VersionMajor>
<VersionMinor>9</VersionMinor>
<Lcid>0</Lcid>
<WrapperTool>primary</WrapperTool>
<Isolated>False</Isolated>
<EmbedInteropTypes>True</EmbedInteropTypes>
</COMReference>
<COMReference Include="VBIDE">
<Guid>{0002E157-0000-0000-C000-000000000046}</Guid>
<VersionMajor>5</VersionMajor>
<VersionMinor>3</VersionMinor>
<Lcid>0</Lcid>
<WrapperTool>primary</WrapperTool>
<Isolated>False</Isolated>
<EmbedInteropTypes>True</EmbedInteropTypes>
</COMReference>
</ItemGroup>
</Project> using System;
using System.Reflection;
using Excel = Microsoft.Office.Interop.Excel;
namespace ExcelDemo
{
class Program
{
public static void Main(string[] args)
{
Excel.Application excel;
Excel.Workbook workbook;
Excel.Worksheet sheet;
Excel.Range range;
try
{
// Start Excel and get Application object.
excel = new Excel.Application();
excel.Visible = true;
// Get a new workbook.
workbook = excel.Workbooks.Add(Missing.Value);
sheet = (Excel.Worksheet)workbook.ActiveSheet;
// Add table headers going cell by cell.
sheet.Cells[1, 1] = "First Name";
sheet.Cells[1, 2] = "Last Name";
sheet.Cells[1, 3] = "Full Name";
sheet.Cells[1, 4] = "Salary";
// Format A1:D1 as bold, vertical alignment = center.
sheet.get_Range("A1", "D1").Font.Bold = true;
sheet.get_Range("A1", "D1").VerticalAlignment =
Excel.XlVAlign.xlVAlignCenter;
// Create an array to multiple values at once.
string[,] saNames = new string[5, 2];
saNames[0, 0] = "John";
saNames[0, 1] = "Smith";
saNames[1, 0] = "Tom";
saNames[1, 1] = "Brown";
saNames[2, 0] = "Sue";
saNames[2, 1] = "Thomas";
saNames[3, 0] = "Jane";
saNames[3, 1] = "Jones";
saNames[4, 0] = "Adam";
saNames[4, 1] = "Johnson";
// Fill A2:B6 with an array of values (First and Last Names).
sheet.get_Range("A2", "B6").Value2 = saNames;
// Fill C2:C6 with a relative formula (=A2 & " " & B2).
range = sheet.get_Range("C2", "C6");
range.Formula = "=A2 & \" \" & B2";
// Fill D2:D6 with a formula(=RAND()*100000) and apply format.
range = sheet.get_Range("D2", "D6");
range.Formula = "=RAND()*100000";
range.NumberFormat = "$0.00";
// AutoFit columns A:D.
range = sheet.get_Range("A1", "D1");
range.EntireColumn.AutoFit();
// Make sure Excel is visible and give the user control
// of Microsoft Excel's lifetime.
excel.Visible = true;
excel.UserControl = true;
}
catch (Exception e)
{
Console.WriteLine($"Error: {e.Message} Line: {e.Source}");
}
}
}
}