Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9036257
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T08:56:48+00:00 2026-06-16T08:56:48+00:00

Due to some concurrency issues, I have been forced to create a workbook with

  • 0

Due to some concurrency issues, I have been forced to create a workbook with multiple sheets. At the end of the execution, some sheets will have data and some wont. I create the sheets using an Execute SQL Task.

I am trying to loop through the workbook and delete the sheets which dont have more than a single row. In other words, delete the sheet if row count is not greater than 1. Any pointers on this question will be appreciated. Please let me know if you need more details on my question. Thank you in advance.

EDIT

Following is the script task that I got from MSDN. I modified it to a point where it can get to the excel sheet and make a count of the rows, now all I want to do is when the count = 1 then delete the sheet. Can someone help me here?

using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.Data.OleDb;
using System.IO;

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

        #region VSTA generated code
        enum ScriptResults
        {
            Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
            Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
        };
        #endregion



            public void Main()
        {
            string count = "";
            string fileToTest;
            string tableToTest;
            string connectionString;

            fileToTest = Dts.Variables["ExcelFile"].Value.ToString();
            tableToTest = Dts.Variables["ExcelTable"].Value.ToString();

            Dts.Variables["ExcelTableExists"].Value = false;

                connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
                "Data Source=" + fileToTest + ";Extended Properties=Excel 8.0";
                string SQL = "SELECT COUNT (*) FROM [" + tableToTest + "$]";
                using (OleDbConnection conn = new OleDbConnection(connectionString))
                {
                    conn.Open();
                    using (OleDbCommand cmd = new OleDbCommand(SQL, conn))
                    {
                        using (OleDbDataReader reader = cmd.ExecuteReader())
                        {
                            reader.Read();
                            count = reader[0].ToString();
                            //if (count = 1)

                        }
                    }
                    conn.Close();
                }
                //return count;
            }

       }
}

EDIT

On further investigation, I found that I would need to add the excel interop assembly for this to work. I dont have that option because this solution will be ported to 140 different machines.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T08:56:49+00:00Added an answer on June 16, 2026 at 8:56 am

    Sadly you cannot delete a sheet in an Excel using OLEDB, the best you can do is clear it of data using the DROP Table command

    connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
                         "Data Source=" + fileToTest + 
                         ";Mode=ReadWrite;Extended Properties=Excel 8.0";
    
    string SQL = "SELECT COUNT(*) FROM [" + tableToTest + "$]";
    
    using (OleDbConnection conn = new OleDbConnection(connectionString))
    {
        conn.Open();
        using (OleDbCommand CountCmd = new OleDbCommand(SQL, conn))
        {
            int RecordCount = (int)CountCmd.ExecuteScalar();
    
            if (RecordCount == 1)
            {
                SQL = "DROP TABLE [" + tableToTest + "$]";
                using (OleDbCommand DropCmd = new OleDbCommand(SQL, conn))
                {
                    DropCmd.ExecuteNonQuery();
                }
            }
        }
        conn.Close();
    }
    

    NOTE: The use of Mode=ReadWrite. You may include/exclude the HDR=Yes/No but you must not include IMEX=1 if you want read/write access to the Workbook

    BTW: There’s no need to use a OleDbDataReader to read a single scalar result, use ExecuteScalar() instead.

    The only way around this using OleDB is to copy the data you want to retain into a new excel file and replace the original one. If you do this however you will lose any formulas or formatting.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Due to some deployment issues I stopped tracking schema.rb in git. Somehow I have
Due to some changes on AD-level I have to change a lookup in AD
I have some idea that it is due to some complex calculation, but i
I'm having some issues with my code. This is probably due to some design
I am unable to run the resque-web on my server due to some issues
I have a very simple HTML. Due to some limitations, I cannot modify the
I have a device to acquire XRay images. Due to some technical constrains, the
i have Visual Studio 2010. Recently I was using MVC2 and due to some
Here's my situation: i have a dialog form that inserts some data on my
I have a simple application that fetches some data from ONE table on db

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.