locked
workbooks.open() method error RRS feed

  • Question

  • User1323156323 posted

    I am trying to open an Excel workbook in C#.NET. I found a great tutorial on how to do this, but when I tried to run it I got an error: "No overload for method 'open' takes '15' arguments."

    I consulted the documentation, but no matter how many or how few arguments I use I still get an error.

    Code:

    Excel.Workbook theWorkbook = ExcelObj.Workbooks.Open( openFileDialog1.FileName, 0, true, 5,
         "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false,0, true);
    
    Why do I keep getting an error?
    Monday, March 19, 2007 4:26 PM

Answers

  • User1657653501 posted

    Try this code  in your button_Click event. Here I used 15 arguments, this code is working.

    this.openFileDialog1.FileName = "*.xls";

    if (this.openFileDialog1.ShowDialog() == DialogResult.OK)

    {

    Excel.Workbook theWorkbook = ExcelObj.Workbooks.Open(openFileDialog1.FileName, 0, true, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 0, true);

    Excel.Sheets sheets = theWorkbook.Worksheets;

    Excel.Worksheet worksheet = (Excel.Worksheet)sheets.get_Item(1);

    for (int i = 1; i <= 4; i++)

    {

    Excel.
    Range range = worksheet.get_Range("A" + i.ToString(), "E" + i.ToString()); System.Array myvalues = (System.Array)range.Cells.Value2;

    string[] strArray = ConvertToStringArray(myvalues);

    this.dataGridView1.DataSource = strArray;

    }

    }

    // and also create stub for  ConvertToString write your code in it

    string[] ConvertToStringArray(Array myvalues)

    {

    // create a new string array

    string[] theArray = new string[myvalues.Length];

    // loop through the 2-D System.Array and populate the 1-D String Array

    for (int i = 1; i <= myvalues.Length; i++)

    {

    if (myvalues.GetValue(1, i) == null) theArray[i - 1] = "";

    else

    theArray[i - 1] = (string)myvalues.GetValue(1, i).ToString();

    }

    return theArray;

    }

    Cheers,

    Pradeep.G.I, PreludeSys

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 13, 2007 2:07 AM

All replies

  • User1657653501 posted

    Try this code  in your button_Click event. Here I used 15 arguments, this code is working.

    this.openFileDialog1.FileName = "*.xls";

    if (this.openFileDialog1.ShowDialog() == DialogResult.OK)

    {

    Excel.Workbook theWorkbook = ExcelObj.Workbooks.Open(openFileDialog1.FileName, 0, true, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 0, true);

    Excel.Sheets sheets = theWorkbook.Worksheets;

    Excel.Worksheet worksheet = (Excel.Worksheet)sheets.get_Item(1);

    for (int i = 1; i <= 4; i++)

    {

    Excel.
    Range range = worksheet.get_Range("A" + i.ToString(), "E" + i.ToString()); System.Array myvalues = (System.Array)range.Cells.Value2;

    string[] strArray = ConvertToStringArray(myvalues);

    this.dataGridView1.DataSource = strArray;

    }

    }

    // and also create stub for  ConvertToString write your code in it

    string[] ConvertToStringArray(Array myvalues)

    {

    // create a new string array

    string[] theArray = new string[myvalues.Length];

    // loop through the 2-D System.Array and populate the 1-D String Array

    for (int i = 1; i <= myvalues.Length; i++)

    {

    if (myvalues.GetValue(1, i) == null) theArray[i - 1] = "";

    else

    theArray[i - 1] = (string)myvalues.GetValue(1, i).ToString();

    }

    return theArray;

    }

    Cheers,

    Pradeep.G.I, PreludeSys

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 13, 2007 2:07 AM
  • User-71488910 posted

    You might try:

     

    Excel.Workbook theWorkbook = ExcelObj.Workbooks.Open( openFileDialog1.FileName, 0, true, 5,
         Type.Missing, Type.Missing, true, Excel.XlPlatform.xlWindows, "\t", false, false,0, true);

     Type.Missing is used for the parameters you don't want to set.  So to simply set the first parameter you need still to use Type.Missing as a "placeholder":

    Excel.Workbook theWorkbook = ExcelObj.Workbooks.Open( openFileDialog1.FileName,
         Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
         Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
         Type.Missing, Type.Missing, Type.Missing, Type.Missing);

    I struggled a lot with not being to open a file until I realized the Type.Missing was "Missing" from my Open statement.

    I hope that helped!

     Pete

    Friday, August 22, 2008 11:38 AM