Excel' RefreshAll'不在托管的ASP.NET应用程序中工作

时间:2015-04-24 14:38:32

标签: c# asp.net excel c#-4.0

在ASP.NET中刷新Excel工作表时遇到问题。我有一个Excel工作表获取新数据但工作簿没有刷新。代码在IIS中创建和部署:

    /// <summary>
    /// Refresh code
    /// </summary>
    /// <param name="Filename"></param>
    private void ExcelRefresh(string Filename)
    {
        try
        {
            object missing = Type.Missing;
            // object NullValue = System.Reflection.Missing.Value;

            Microsoft.Office.Interop.Excel._Application excelApp = new Microsoft.Office.Interop.Excel.Application();
            excelApp.DisplayAlerts = false;
            Microsoft.Office.Interop.Excel.Workbook Workbook = excelApp.Workbooks.Open(
               Filename, missing,
      false,
      missing,
      missing,
      missing,
      true,
      missing,
      missing,
      true,
      missing,
      missing,
      missing,
      missing,
      missing);
            Workbook.RefreshAll();





            string tmpName = Path.GetTempFileName();
            File.Delete(tmpName);
            Workbook.SaveAs(tmpName, missing, missing, missing, missing, missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, missing, missing, missing, missing, missing);



            Workbook.Close(false, missing, missing);
            excelApp.Quit();
            File.Delete(Filename);
            File.Move(tmpName, Filename);
            Workbook = null;
            System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);

            Label1.Text = "Success !!";
        }
        catch (Exception ex)
        {
            Label1.Text = "Error !!" + ex.Message;
        }
    }

    /// <summary>
    /// Button click handler
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    public void Btn_Click(object sender, EventArgs e)
    {
        ExcelRefresh(@"E:\mypath\file.xlsx"); // This is server side static file path 
    }

在本地调试模式下进行测试时,它工作得很好,但部署的代码失败并出错:

  

Microsoft Excel无法访问该文件&#39; E:\ mypath \ file.xlsx&#39;。有几个可能的原因:
   •文件名或路径不存在    •该文件正由另一个程序使用    ?您尝试保存的工作簿与当前打开的工作簿具有相同的名称。

0 个答案:

没有答案