Reputation: 13
So, originally my program used a static spreadsheet named test, but to further enhance my program, I wanted it to make it to where the name within the textbox is the name of the file to use for the program; named: test.xlsx
So I believed I called the code properly here:
public static string nameofexcelfile;
public Form1()
{
InitializeComponent();
nameofexcelfile = textBox4.Text + ".xlsx";
}
private void Form1_Load(object sender, EventArgs e)
{
OpenFile();
}
public void OpenFile()
{
//Excel excel = new Excel(@"Test.xlsx", 1);
Excel excel = new Excel(nameofexcelfile, 1);
textBox1.Text = excel.ReadCell(1, 0);
textBox2.Text = excel.ReadCell(1, 1);
textBox3.Text = excel.ReadCell(3, 2);
}
Here is my Excel.cs class file:
internal class Excel
{
string path = "";
_Application excel = new _Excel.Application();
Workbook wb;
Worksheet ws;
public Excel(string path, int Sheet)
{
//this.path = path;
//wb = excel.Workbooks.Open(path);
this.path = Form1.nameofexcelfile;
wb = excel.Workbooks.Open(Form1.nameofexcelfile);
ws = wb.Worksheets[Sheet];
}
public string ReadCell(int i, int j)
{
i++;
j++;
if (ws.Cells[i, j].Value2 != null)
return ws.Cells[i, j].Value2;
else
return "";
}
}
}
Within my Excel.cs class; I named a string and gave it the default location "" as the Documents folder, and this was named:
string path = "";
This function below within my Excel.cs file is giving me an error of file not found:
public Excel(string path, int Sheet)
{
//this.path = path;
//wb = excel.Workbooks.Open(path);
this.path = Form1.nameofexcelfile;
wb = excel.Workbooks.Open(Form1.nameofexcelfile);
ws = wb.Worksheets[Sheet];
}
originally, the function called the path as you see the 2 lines above commented it out. Since I want the Excel file to be found during the Form1 Textbox, how can I make this work. My error is shown below:
System.Runtime.InteropServices.COMException: 'Sorry, we couldn't find .xlsx. Is it possible it was moved, renamed or deleted?'
Upvotes: 1
Views: 536