How to open an Excel file in C#?

357,457

Solution 1

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.

enter image description here

Solution 2

FileInfo fi = new FileInfo("C:\\test\\report.xlsx");
if(fi.Exists)
{
    System.Diagnostics.Process.Start(@"C:\test\report.xlsx");
}
else
{
    //file doesn't exist
}

Solution 3

private void btnChoose2_Click(object sender, EventArgs e)
{
  OpenFileDialog openfileDialog1 = new OpenFileDialog();
  if (openfileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  {
    this.btnChoose2.Text = openfileDialog1.FileName;
    String filename = DialogResult.ToString();

    var excelApp = new Excel.Application();
    excelApp.Visible = true;
    excelApp.Workbooks.Open(btnChoose2.Text);
  }
}

Solution 4

Imports

 using Excel= Microsoft.Office.Interop.Excel;
 using Microsoft.VisualStudio.Tools.Applications.Runtime;

Here is the code to open an excel sheet using C#.

    Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
    Microsoft.Office.Interop.Excel.Workbook wbv = excel.Workbooks.Open("C:\\YourExcelSheet.xlsx");
    Microsoft.Office.Interop.Excel.Worksheet wx = excel.ActiveSheet as Microsoft.Office.Interop.Excel.Worksheet;

    wbv.Close(true, Type.Missing, Type.Missing);
    excel.Quit();

Here is a video mate on how to open an excel worksheet using C# https://www.youtube.com/watch?v=O5Dnv0tfGv4

Solution 5

you should open like this

        Excel.Application xlApp ;
        Excel.Workbook xlWorkBook ;
        Excel.Worksheet xlWorkSheet ;
        object misValue = System.Reflection.Missing.Value;

        xlApp = new Excel.ApplicationClass();
        xlWorkBook = xlApp.Workbooks.Open("csharp.net-informations.xls", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
        xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

source : http://csharp.net-informations.com/excel/csharp-open-excel.htm

ruden

Share:
357,457
tksy
Author by

tksy

Updated on July 05, 2022

Comments

  • tksy
    tksy almost 2 years

    I am trying to convert some VBA code to C#. I am new to C#. Currently I am trying to open an Excel file from a folder and if it does not exist then create it. I am trying something like the following. How can I make it work?

    Excel.Application objexcel;
    Excel.Workbook wbexcel;
    bool wbexists;
    Excel.Worksheet objsht;
    Excel.Range objrange;
    
    objexcel = new Excel.Application();
    if (Directory("C:\\csharp\\error report1.xls") = "")
    {
        wbexcel.NewSheet();
    }
    
    else
    {
        wbexcel.Open("C:\\csharp\\error report1.xls");
        objsht = ("sheet1");
    }
    objsht.Activate();
    
  • Callum Rogers
    Callum Rogers about 14 years
    Unless you're using VS2010 :) Oh, and you want Missing.Value
  • abatishchev
    abatishchev over 12 years
    @Saravanan: See MSDN: Object ReadOnly. I guess you need to pass true.
  • Saravanan
    Saravanan over 12 years
    :Please See my Question... stackoverflow.com/questions/8906670/…
  • shaosh
    shaosh over 9 years
    The excel does not show without excelApp.Visible = true. Thanks!
  • Nad
    Nad about 8 years
    when I add that I get error as CS0104: 'DataTable' is an ambiguous reference between 'System.Data.DataTable' and 'Microsoft.Office.Interop.Excel.DataTable'
  • abatishchev
    abatishchev about 8 years
    @coder: remove using System.Data; or use "using DataTable = Microsoft.Office.Interop.Excel.DataTable;
  • Nad
    Nad about 8 years
    @abatishchev: when I remove using System.Data; i get namespace error for dataset which I am using for another set of code..
  • abatishchev
    abatishchev about 8 years
    @coder: use full name (namespace.class.name) in either case
  • Nad
    Nad about 8 years
    when I write System.Data.DataTable ObjDt = ObjPriDal.ExecuteSystem.Data.DataTable(strquery); it gives error for ExecuteSystem as DataAccessLayer does not contain a definition for ExecuteSystem
  • abatishchev
    abatishchev about 8 years
    @coder: you don't need it in the right expression. Use full name as you usually declare variable's type
  • Nad
    Nad about 8 years
  • Nad
    Nad about 8 years
    @abatishchev: I tried like this var directory = Server.MapPath("~/Attachment/"); ExcelLibrary.DataSetHelper.CreateWorkbook(directory + "Employee_lwpc_Details.xls", DS_lwpc); Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application(); Workbook wb = excel.Workbooks.Open(directory + "Employee_lwpc_Details.xls", DS_lwpc)); getting error as cannot implicitly convert type Microsoft.Office.Interop.Excel to Excellibrary.office.Excel.Workbook
  • ThomasMX
    ThomasMX almost 8 years
    @abatishchev I tested your code, it's running. I'm wondering though, how is it possible to instantiate Application when it is actually an interface?
  • abatishchev
    abatishchev almost 8 years
    @ThomasMX: I was wondering this for a while too. Since VSO is a COM wrapper, .NET makes it possible to instantiate an interface. Each is decorated with Guid and that's how it's being read from registry and instantiated.
  • Chjquest
    Chjquest over 7 years
    private object _value = Missing.Value; replace the missing with _value. And it works for me.
  • Roland
    Roland over 5 years
    Works for me. Certainly simpler than the accepted 2009 answer because no additional tools are needed, and it is essential a one-liner.
  • Matthew
    Matthew almost 5 years
    See here for a full example of creating and saving an Excel file using the interop stackoverflow.com/a/23102523/8595398
  • Eda
    Eda over 3 years
    Great, just what I was looking for, thanks! I simplified it by just passing the fi inside the start as System.Diagnostics.Process.Start(fi.ToString());
  • shahsani
    shahsani almost 3 years
    This is by far, the most easy way to do it. Thank you