I am quite new to C++/CLI, and I am having trouble with opening an Excel file. This is my following code sample.
#using <system.dll>
using namespace System;
using namespace Microsoft::Office::Interop::Excel;
String ^filename = gcnew String(L"Test.xlsx");
try
{
Application^ exapp = gcnew ApplicationClass();
Workbook^ wb = exapp->Workbooks->Open(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);
Worksheet^ exws = safe_cast<Worksheet^>(exapp->ActiveSheet);
exws->Cells[1, 1] = "Hello world";
return true;
Console::WriteLine("File Read Successfully");
}
catch (Exception ^e)
{
Console::WriteLine("Failed to read File");
Console::WriteLine(e);
return false;
};
Whenever I run it, it will always throw FileNotFoundException
, even though the Excel file is right next to Test.exe, unless I change filename back to fullpath like C:\Users\NGU0085\Documents\Visual Studio 2013\Projects\Test\Debug\Test.xlsx
Is there any way to open an Excel file using relative path?