Quantcast
Channel: Active questions tagged excel - Stack Overflow
Viewing all articles
Browse latest Browse all 88054

Get name of the first Excel sheet dynamically and affect it to a variable

$
0
0

I use SSIS to load xlsx files wich having different sheets names but the same structure. I need to load only the first sheet of each file, but the name can't be the same each time, so I need to always point on the first sheet regardless it's name. I think that in data access mode in Excel source task is not possible to use the index(1st) that's why I'm trying to use Task script to get the name of the first sheet of each file and put it in a variable Sheet_name and use it in the data access mode for each file.

I have an exception with the code above and I don't know how to solve it.

I've tried to look for a solution without using script but I didn't find it, that's why I'm trying to get my code work.

 public void Main()
 {
        String FolderPath = 
  Dts.Variables["User::Folder_To_Be_Processed_Path"].Value.ToString();
  String File_Name = Dts.Variables["User::File_Name"].Value.ToString();
  string fileFullPath = "";
  fileFullPath = FolderPath + "\\" + File_Name;
  string connString = "Provider=Microsoft.Jet.OLEDB.12.0;Data Source=" + 
 fileFullPath + ";Extended Properties=\"Excel 12.0;HDR=YES\";";  

 using (OleDbConnection conn = new OleDbConnection(connString))
{
conn.Open();
MessageBox.Show(connString);
dtSchema = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { 
null, null, null, "TABLE" });
Dts.Variables["User::Sheet_Name"].Value= dtSchema.Rows[0].Field<string> 
("TABLE_NAME");
}
    Dts.TaskResult = (int)ScriptResults.Success;
}

Viewing all articles
Browse latest Browse all 88054

Trending Articles



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