Quantcast
Channel: SQL Server Integration Services forum
Viewing all articles
Browse latest Browse all 24688

Refresh All Excel file using C# code

$
0
0

Hi,

Please find the below C# code for Excel  Refresh All. I am not familiar in C# code. Here I have Included Thread.Sleep time. Is there any possibilities to Refresh excel workbook without using thread.sleep, because some excel file refreshed within 5 sec but its wait 30 seconds (thread.sleep) to go for next excel. Please provide any suggestions.

using System;
using System.Data;
using System.Collections.Generic;

using System.Text;
using Excel = Microsoft.Office.Interop.Excel;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;
using System.Data.OleDb;
using System.Xml;

namespace ST_5f600caa86a04a4b8080eef2563c791e.csproj
{
    [System.AddIn.AddIn("ScriptMain", Version = "1.0", Publisher = "", Description = "")]
    public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
    {

        public void Main()
        {

            Excel.Application xlapp = new Excel.Application();
            xlapp.Visible = false;
            string path = Dts.Variables["User::rfilename"].Value.ToString();
            //string path = "C:\\SSISDev\\1510001_Cluster Penetration\\Final_File\\AE Assigned & BP Details - Template.xls";
            Excel.Workbook excelwb = xlapp.Workbooks.Open(path, 0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);

            OleDbDataAdapter A = new OleDbDataAdapter();
            System.Data.DataTable dt = new System.Data.DataTable();
            A.Fill(dt, Dts.Variables["User::SName"].Value);

            Excel._Worksheet oSheetGame;
            foreach (DataRow row in dt.Rows)
            {
                string Name;
                object[] array = row.ItemArray;
                Name = array[0].ToString();

                 Excel._Worksheet oSheet;
            oSheet = (Excel._Worksheet)
                excelwb.ActiveSheet;
            oSheet.Copy(oSheet, Type.Missing);
            oSheetGame = (Excel._Worksheet)excelwb.Worksheets["Temp"];
            oSheetGame.Name = Name;
            Excel._Worksheet rsheet = (Excel._Worksheet)excelwb.Worksheets["Temp (2)"];
            rsheet.Name = "Temp";  
            }

             oSheetGame = (Excel._Worksheet)excelwb.Worksheets["Temp"];
             xlapp.DisplayAlerts = false;
             oSheetGame.Delete();

             excelwb.RefreshAll();
             //excelwb.SaveCopyAs(Dts.Variables["User::rfilename"].Value.ToString());
             System.Threading.Thread.Sleep(Convert.ToInt32(60000));
            excelwb.Save();
            excelwb.Close(false, Type.Missing, Type.Missing);
            xlapp.Quit();

            GC.Collect();
            GC.WaitForPendingFinalizers();

            //Dts.TaskResult = (int)ScriptResults.Success;
        }
        

    }
}


Viewing all articles
Browse latest Browse all 24688


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>