How to open an Excel file in C#?

2019-01-04 10:28发布

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.Open("C:\\csharp\\error report1.xls");
    objsht = ("sheet1");

标签: c# .net excel vsto
2楼-- · 2019-01-04 10:44
Microsoft.Office.Interop.Excel.Application excapp;

excapp = new Microsoft.Office.Interop.Excel.Application();

object misval=System.Reflection.Missing.Value;

Workbook wrkbuk = new Workbook();

Worksheet wrksht = new Worksheet();

wrkbuk = excapp.Workbooks._Open(@"C:\Users\...\..._template_v1.0.xlsx", misval, misval, 
misval, misval, misval, misval, misval, misval, misval, misval, misval, misval);

wrksht = (Microsoft.Office.Interop.Excel.Worksheet)wrkbuk.Worksheets.get_Item(2);
3楼-- · 2019-01-04 10:46

For opening a file, try this:

    missing, missing, missing, missing, missing, missing, missing,
    missing, missing, missing, missing, missing,missing, missing);

You must supply those stupid looking 'missing' arguments. If you were writing the same code in VB.Net you wouldn't have needed them, but you can't avoid them in C#.

4楼-- · 2019-01-04 10:48

It's easier to help you if you say what's wrong as well, or what fails when you run it.

But from a quick glance you've confused a few things.

The following doesn't work because of a couple of issues.

if (Directory("C:\\csharp\\error report1.xls") = "")

What you are trying to do is creating a new Directory object that should point to a file and then check if there was any errors.

What you are actually doing is trying to call a function named Directory() and then assign a string to the result. This won't work since 1/ you don't have a function named Directory(string str) and you cannot assign to the result from a function (you can only assign a value to a variable).

What you should do (for this line at least) is the following

FileInfo fi = new FileInfo("C:\\csharp\\error report1.xls");
    // Create the xl file here
    // Open file here

As to why the Excel code doesn't work, you have to check the documentation for the Excel library which google should be able to provide for you.

5楼-- · 2019-01-04 10:49

open Excel file

Summer. ? 凉城
6楼-- · 2019-01-04 10:52
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;
小情绪 Triste *
7楼-- · 2019-01-04 10:52

For editing Excel files from within a C# application, I recently started using NPOI. I'm very satisfied with it.

登录 后发表回答