C# 使用Microsoft.Office.Interop Word和Excel

C# 使用Microsoft.Office.Interop Word和Excel,c#,asp.net,C#,Asp.net,我正在使用Microsoft.Office.Interop,为了使用C#扫描word和excel文件,我将文件作为用户的输入,保存在本地目录中,然后扫描Interop库。 它在我的本地发布上运行良好,但当我在服务器上发布它时,它并没有扫描Word或Excel文件。 我的服务器上安装了MS Office,与我的PC上安装的版本相同。 我还需要做什么?我已在bin目录中复制了所需的DLL。 这就是我阅读word文件的方式 public static string word_to_text(strin

我正在使用Microsoft.Office.Interop,为了使用C#扫描word和excel文件,我将文件作为用户的输入,保存在本地目录中,然后扫描Interop库。 它在我的本地发布上运行良好,但当我在服务器上发布它时,它并没有扫描Word或Excel文件。 我的服务器上安装了MS Office,与我的PC上安装的版本相同。 我还需要做什么?我已在bin目录中复制了所需的DLL。 这就是我阅读word文件的方式

public static string word_to_text(string source)
{
  Word.Application newApp = new Word.Application();

  object Source = source;
  object Unknown = Type.Missing;

  object miss = System.Reflection.Missing.Value;
  object readOnly = true;
  Microsoft.Office.Interop.Word.Document docs = newApp.Documents.Open(ref Source, ref miss, ref readOnly, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss);
  string totaltext = "";
  for (int j = 0; j < docs.Paragraphs.Count; j++)
  {
    totaltext += " \r\n " + docs.Paragraphs[j + 1].Range.Text.ToString();
  }
  docs.Close();
  newApp.Quit();
  return totaltext;
}
我阅读解决方案以授予文件夹的权限,并在system32中创建桌面文件夹,这两种解决方案都不适用于我:(
只有在我发布Word时才会出现此问题。您不应该在服务器中使用Office automation。一次只能有一个线程访问Word,与其他选项相比速度非常慢,而且Word不适合服务器端执行

使用OpenXML SDK更好:

或其他包装:


如果您觉得必须在服务器上使用Office automation(您不应该这样做),然后至少将其放在一个单独的进程中,并向另一个进程发出请求。这样,您可以确保一次只处理一个请求,您可以安全地向另一个进程授予所需的权限,如果Word停止,您可以杀死它和第二个进程。

您是否收到任何错误?我们可以看到一些示例代码吗?不,我是我没有收到任何错误?异常?何处?@Francescodelis我看到了我更新的问题您是否正在使用ASP.NET应用程序,可能正在IIS上运行?@Syedsalmanrazazidi,我看到了您的更新,但我仍然支持我的强烈建议,即永远不要在服务器上使用Office automation。请改用OpenXML SDK。
Microsoft Office Excel cannot access the file 'myfile path'. 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 workbook.