C++ 用C+打开并读取Excel文件+/CLI

C++ 用C+打开并读取Excel文件+/CLI,c++,.net,excel,command-line-interface,C++,.net,Excel,Command Line Interface,我正在尝试使用C++/CLI创建一个程序,该程序使用Visual Studio从Excel工作簿中读取一些数据。 我已将Microsoft.Office.Interop.Excel(v12)添加到项目属性中的引用中。 我的基本目标是将单元格的值作为字符串获取(工作簿仅包含文本值)。 我目前的代码如下(当然只包括主要部分): 当我试图打开工作簿时,xls和xlsx文件都会发生这种情况(在以“工作簿^wb=exApp->Workbooks->open”开头的行中),因此我甚至无法检查其余文件是否正常

我正在尝试使用C++/CLI创建一个程序,该程序使用Visual Studio从Excel工作簿中读取一些数据。 我已将Microsoft.Office.Interop.Excel(v12)添加到项目属性中的引用中。 我的基本目标是将单元格的值作为字符串获取(工作簿仅包含文本值)。 我目前的代码如下(当然只包括主要部分):

当我试图打开工作簿时,xls和xlsx文件都会发生这种情况(在以“工作簿^wb=exApp->Workbooks->open”开头的行中),因此我甚至无法检查其余文件是否正常工作)。 请帮忙,我错过了什么/做错了什么


提前谢谢。

好的,我终于找到了主要问题,如果其他人遇到同样的问题:

打开Excel文件时,Windows和安装的Excel程序的区域设置必须相同。我住在匈牙利,Windows中有匈牙利地区设置[注:我使用英文Windows,只是地区设置不同],我有英文版的Excel。当我将Windows中的区域设置切换到我们(与Excel的区域相同),清理并重新构建解决方案后,一切都开始正常工作

(我不确定messagebox部分是否在我的代码中工作,我同时更改了它,主要问题是打开文件)

using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace Microsoft::Office::Interop::Excel;

start(void){
        Microsoft::Office::Interop::Excel::Application^ exApp= gcnew Microsoft::Office::Interop::Excel::ApplicationClass();
        String^ filename="e:\\test.xls";
        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);
        Worksheet^  exWs  = safe_cast<Worksheet^>(exApp->ActiveSheet);
        int row=1;
        int col=1;
        String^ tmp=((Microsoft::Office::Interop::Excel::Range^)exWs->Cells[(System::Object^)row, (System::Object^)col])->Value2->ToString();
        MessageBox::Show(tmp);
}
An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in exc2.exe

Additional information: Old format or invalid type library. (Exception from HRESULT: 0x80028018 (TYPE_E_INVDATAREAD))