How to open an Excel file in C#?
You need to have installed Microsoft Visual Studio Tools for Office (VSTO).
VSTO can be selected in the Visual Studio installer under Workloads > Web & Cloud > Office/SharePoint Development.
After that create a generic .NET project and add a reference to Microsoft.Office.Interop.Excel
via 'Add Reference... > Assemblies' dialog.
Application excel = new Application();
Workbook wb = excel.Workbooks.Open(path);
Missing.Value
is a special reflection struct for unnecessary parameters replacement
In newer versions, the assembly reference required is called Microsoft Excel 16.0 Object Library
. If you do not have the latest version installed you might have Microsoft Excel 15.0 Object Library
, or an older version, but it is the same process to include.
Opening an Excel file in C# using Process.Start
Normally, Process.Start(@"C:\Users\username\Documents\newTest.xlsx");
would open your document in Excel.
However, you say in a comment that you are doing this from an Excel add-in which runs in the background. The solution needs to take this into account (the code sample assumes that you have a VSTO add-in, otherwise you need to adjust accordingly):
// make the running Excel instance visible
Globals.ThisAddIn.Application.Visible = true;
// open the workbook using Excel interop
Globals.ThisAddIn.Application.Workbooks.Open(fileName);
C# - Proper way to open and close an excel file programmatically
Semi pseudo-code:
using Excel = Microsoft.Office.Interop.Excel;
# declare the application object
Excel.Application xl = new Excel.Application();
# open a file
Excel.Workbook wb = xl.Workbooks.Open("some_file.xlsx");
# do stuff ....
# close the file
wb.Close();
# close the application and release resources
xl.Quit();
How to open an excel document using c#
If excel is set as the default viewer for xls files on the system you can open the file using Process class:
System.Diagnostics.Process.Start("myFile.xls");
Opening an excel file using asp.net C#
If you want users of your website to open an excel file that you've created on server, than you don't need to open it there - just send it to the user. Replace the last two lines of code with this:
string filePath = directory + "Employee_lwpc_Details.xls";
Response.ContentType = "Application/vnd.ms-excel";
Response.AppendHeader("content-disposition",
"attachment; filename=" + "Employee_lwpc_Details.xls");
Response.TransmitFile(filePath);
Response.End();
Related Topics
How to Specify My Explicit Type Comparator Inline
What Is the Fastest Way I Can Compare Two Equal-Size Bitmaps to Determine Whether They Are Identical
The Current Synchronizationcontext May Not Be Used as a Taskscheduler
Getting the "Diff" Between Two Arrays in C#
What Is the Fastest Way to Combine Two Xml Files into One
JSON String to CSV and CSV to JSON Conversion in C#
Creating Delegates Manually VS Using Action/Func Delegates
Could Not Load File or Assembly 'Microsoft.Reportviewer.Common, Version=11.0.0.0
Pulling Data from a Webpage, Parsing It for Specific Pieces, and Displaying It
Exclude Property on Update in Entity Framework
What's the Difference Between Application.Run() and Form.Showdialog()
Fast Creation of Objects Instead of Activator.Createinstance(Type)