2011-04-12 42 views
2

我有一个使用Office PIA的Excel包装类。我也有一个以前写在NUnit上运行的有限测试套件。我们正在工作中迁移到TFS2010,所以我也将NUnit测试迁移到MSTest。在Team Build 2010下运行针对Excel的单元测试的异常

测试套件在我的开发机器上运行良好,并且如果在运行构建代理的机器上使用MSTest命令行实用程序手动执行。但是,当通过Team Build执行时,所有与磁盘I/O(打开,保存等)有关的测试都会失败。我的构建代理正在域帐户上运行,并且该域帐户也是同一台计算机上的本地管理员。少数不做任何磁盘I/O的测试运行良好,所以我知道Excel正在被解雇和可用。只是看起来像一个权限问题或团队建设过程的限制。

所以这里是一个示例函数。这就是我认为这是Excel I/O问题的原因。 File.Exists检查传递正常。我在测试运行中没有收到FileNotFoundException,而是直接从Interop层收到COMException。

public void OpenXLS(string workbookFilePath) 
{ 
    // Make sure given file path exists 
    if (!File.Exists(workbookFilePath)) 
    { 
     throw new FileNotFoundException(String.Format(CultureInfo.CurrentCulture, 
     "File '{0}' cannot be found.", workbookFilePath)); 
    } 

    // Open the Workbook 
    _xlsWorkbook = _xlsWorkbooks.Open(workbookFilePath, 0, false, Missing.Value, 
     "", Missing.Value, true, Missing.Value, Missing.Value, true, false, 
     Missing.Value, Missing.Value, Missing.Value, Missing.Value); 
} 

例外:

System.Runtime.InteropServices.COMException: Microsoft Excel cannot access the file 
'C:\BuildPath\TestResults\TestRun\Out\TestBook.xls'. There are several possible reasons: 

• The file name or path does not exist. 
• The file is being used by another program. 
• The workbook you are trying to save has the same name as a currently open 
+0

请发布您收到的全部例外。 – 2011-04-15 20:05:27

回答

4

我有类似的问题,这个解决方案为我工作: http://blogs.msdn.com/b/sqlserverfaq/archive/2010/04/30/unable-to-open-excel-files-using-a-cscript-in-sql-server-jobs.aspx

A“桌面”文件夹,似乎就要在“systemprofile”文件夹中。

  • 创建下转位置C的Windows 2008服务器(x64)的“桌面”文件夹:\ WINDOWS \ Syswow64资料\ CONFIG \ systemprofile

  • 而对于一个32位的Windows 2008 Server中创建了“桌面“文件夹下的位置C:\ Windows \ System32 \ config \ systemprofile

+0

请在您的回答中引用相关的解析行。博客文章似乎随着时间消失;) – oleschri 2011-05-24 11:39:18

+0

@agnes感谢您的答案!看起来像完全相同的例外。我明天会在我的构建服务器上试试这个。 – harlam357 2011-10-28 02:25:44

0

Harlam357,你让我的一天!

但是,即使在64位的机器上,您也需要在C:\ Windows \ System32 \ config \ systemprofile中创建“Desktop”文件夹。我已经在两个目录中创建它以确保。并且不要忘记完全控制服务帐户,也许这是必要的。

相关问题