首页 > 技术文章 > Refresh Excel (asp.net,C#)

2zhyi 2014-04-02 16:20 原文

#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();
        }
    }



}

  

推荐阅读