2012-05-08 31 views
0

我想在我的C++应用程序C++/cli应用程序中实现如下所示的东西。我使用Visual Studio 2010中使用C++/cli访问xlsx表格

static void ReadExcelFileDOM(string fileName) 
{ 
    using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open(fileName, false)) 
    { 
     WorkbookPart workbookPart = spreadsheetDocument.WorkbookPart; 
     WorksheetPart worksheetPart = workbookPart.WorksheetParts.First(); 
     SheetData sheetData = worksheetPart.Worksheet.Elements<SheetData>().First(); 
     string text; 
     foreach (Row r in sheetData.Elements<Row>()) 
     { 
      foreach (Cell c in r.Elements<Cell>()) 
      { 
       text = c.CellValue.Text; 
       Console.Write(text + " "); 
      } 
     } 
     Console.WriteLine(); 
     Console.ReadKey(); 
    } 
} 

我进行如下:

#include "stdafx.h" 
#define S DocumentFormat::OpenXml::Spreadsheet::Sheets 
#define Sheetdata DocumentFormat::OpenXml::Spreadsheet::SheetData 
#define Wsheet DocumentFormat::OpenXml::Spreadsheet::WorkbookPart::WorksheetPart 
#define E DocumentFormat::OpenXml::OpenXmlElement 
#define As DocumentFormat::OpenXml::OpenXmlAttributes 
#define A DocumentFormat::OpenXml::OpenXmlAttribute 
using namespace System; 
using namespace System::Collections::Generic; 
using namespace System::Linq; 
using namespace System::IO; 
using namespace System::Text; 
using namespace System::Diagnostics; 
using namespace DocumentFormat::OpenXml; 
using namespace DocumentFormat::OpenXml::Packaging; 
using namespace DocumentFormat::OpenXml::Spreadsheet; 
int _tmain(int argc, _TCHAR* argv[]) 
{ 
    DocumentFormat::OpenXml::Packaging::SpreadsheetDocument^ mySpreadsheet = SpreadsheetDocument::Open("sample.xlsx", false); 
    WorkbookPart^ wp = mySpreadsheet->WorkbookPart;// 
    IEnumerable<Sheet>^ sheets = wp->GetFirstChild<Sheet>().Elements<Sheet>(); 

    return 0; 
} 

构建失败,以下错误:

1>clitest.cpp(33): error C3225: generic type argument for 'T' cannot be 'DocumentFormat::OpenXml::Spreadsheet::Sheet', it must be a value type or a handle to a reference type 
1>clitest.cpp(33): error C2039: 'GetFirstChild' : is not a member of 'DocumentFormat::OpenXml::Packaging::WorkbookPart' 
1>   c:\program files (x86)\open xml sdk\v2.0\lib\documentformat.openxml.dll : see declaration of 'DocumentFormat::OpenXml::Packaging::WorkbookPart' 
1>clitest.cpp(33): error C2059: syntax error : ')' 

请,如果有人可以帮助我在此。我在这个问题上搁置。

问候 强大的文本

+0

由于Sheet是引用类型,因此您必须正确使用帽子^ IEnumerable 。就像错误信息所述。我建议你使用.NET的能力来混合使用不同语言编写的程序集。 –

回答

0

的泛型类型需要一个类引用类型。

IEnumerable<Sheet^> 
+0

当我试过IEnumerable sheets = wp-> GetFirstChild ().Elements ();我也试过这个。 – Yoku

+0

1> clitest.cpp(33):error C3149:'System :: Collections :: Generic :: IEnumerable ':不能在没有顶级'^'的情况下使用此类型 1>与 1> [ 1 > T = DocumentFormat :: OpenXml :: Spreadsheet :: Sheet 1>] 1> clitest.cpp(33):error C2039:'GetFirstChild':不是'DocumentFormat :: OpenXml :: Packaging ::的成员WorkbookPart' 1> c:\ program files(x86)\ open xml sdk \ v2.0 \ lib \ documentformat.openxml.dll:请参阅'DocumentFormat :: OpenXml :: Packaging :: WorkbookPart'的声明 1> clitest。 cpp(33):错误C2059:语法错误:')' 1> – Yoku

+0

它不只是在那里......任何ref类(.Net类)需要用帽子(^)声明...并且每个泛型类需要一个带帽子的ref类。 –