Refresh Excel (asp.net,C#)

#region NameSpace
using System;
using System.Linq;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using System.Transactions;
using VAU.Dao;
using VAU.Dto;
using VAU.Domain;
using VAU.EnumType;
using VAU.Dao.NHibernate;
using System.Web.UI;
using Excel = Microsoft.Office.Interop.Excel;
using System.Configuration;
using System.IO;
using System.Diagnostics;

#endregion

public partial class Pages_AtAGlanceReport_BookingView : PageBase
{

    public void RefreshExcel(string execelLocation)
    {
        try
        {
            object _missingValue = System.Reflection.Missing.Value;
            Excel.Application excel = new Excel.Application();
            excel.DisplayAlerts = false;
            Excel.Workbook theWorkbook = excel.Workbooks.Open(execelLocation, _missingValue, false, _missingValue, _missingValue, _missingValue, _missingValue, _missingValue, _missingValue, _missingValue, _missingValue, _missingValue, _missingValue);

            lock (theWorkbook)
            {
                theWorkbook.RefreshAll();
            }

            System.Threading.Thread.Sleep(5 * 1000);// Make sure correct save 

            theWorkbook.Save();
            theWorkbook.Close();
            excel.Quit();
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }
    }



}

  

posted @ 2014-04-02 16:20  shzy2012  阅读(218)  评论(0编辑  收藏  举报