Visual Studio を使用して Excel ワークブックからデータを読み取る C++/CLI を使用したプログラムを作成しようとしています。プロジェクト プロパティの参照に Microsoft.Office.Interop.Excel (v12) を追加しました。私の基本的な目標は、セルの値を文字列として取得することだけです (ワークブックにはテキスト値のみが含まれています)。私の現在のコードは次のとおりです(もちろん、主要部分のみが含まれています):
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))
ワークブックを開こうとすると、xls ファイルと xlsx ファイルの両方で発生します (「Workbook^ wb = exApp->Workbooks->Open」で始まる行で、残りが機能しているかどうかも確認できませんでした)。助けてください、私は何を見逃していますか/間違っていますか?
前もって感謝します。