(VS Team Services) 构建解决方案:命名空间 'Microsoft' 中不存在类型或命名空间名称 'Office'

(VS Team Services) Building solution: The type or namespace name 'Office' does not exist in the namespace 'Microsoft'

我们有一个读取 Microsoft excel 文件(.xls、.xlsx、.xlsm)的应用程序。

它工作正常:

Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
Excel.Range range;
int rCnt;
int cCnt;
int rw = 0;
int cl = 0;
xlApp = new Excel.Application();
xlWorkBook = xlApp.Workbooks.Open(full_filename, 0, true, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
range = xlWorkSheet.UsedRange;
rw = range.Rows.Count;
cl = range.Columns.Count;
for (rCnt = 1; rCnt <= rw; rCnt++)
{
    List<object> rowList = new List<object>();
    for (cCnt = 1; cCnt <= cl; cCnt++)
    {
        // do stuff
    }
}
xlApp.Quit();
Marshal.ReleaseComObject(xlWorkSheet);
Marshal.ReleaseComObject(xlWorkBook);
Marshal.ReleaseComObject(xlApp);

问题出在 Visual Studio Team Services 中,它在构建过程中导致以下错误:

The type or namespace name 'Office' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?)

我们应该在 Visual Studio 上设置什么,以便 Team Services 能够构建您的应用程序?谢谢

错误之前,您应该收到关于缺少程序集(特别是Microsoft.Office.Interop.Excel)的警告,一个程序集在 VSTS 的托管生成代理上(在 GAC 上)不可用。

解决方案是利用此 Nuget package Microsoft.Office.Interop.Excel 并在您的项目中引用该 Nuget 包提供的程序集。

然后只要您在实际构建之前使用 Nuget Restore 构建任务,构建就会在任何地方运行。