【问题标题】:SSIS Excel source refer to the only Sheet ( Loop thru all Excel)SSIS Excel 源是指唯一的工作表(循环通过所有 Excel)
【发布时间】:2018-06-09 15:51:22
【问题描述】:

现在我有一堆 .xls 文件,我需要对其进行编辑以重命名唯一的工作表 = Sheet1,b'z 它在 Excel 源中设置为硬编码的 Sheet1,有什么技巧可以引用唯一的有效工作表,它可以在我的情况下有不同的名字。我对此使用 For Each 循环。 发送 戴

【问题讨论】:

标签: ssis


【解决方案1】:

我使用跟随脚本任务 (C#),如果总是有一个名称未知的工作表,这将很好用:

变量:

控制流:

脚本任务设置:

确保变量是读/写的

代码:

System.Data.OleDb.OleDbConnection objConn;
DataTable dt;

string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Dts.Variables["fName"] + ";Extended Properties=\"Excel 8.0;HDR=YES\";"
objConn = new System.Data.OleDb.OleDbConnection(ConnStr);
objConn.Open();

dt = objConn.GetOleDbSchemaTable(System.Data.OleDb.OleDbShemaGuid.Tables,null);
objConn.Close();

foreach(DataRow r in dt.Rows)
{
   //for some reason there is always a duplicate sheet with underscore.
   string t = r["TABLE_NAME"].ToString(); 

   //Note if more than one sheet exist this will only capture the last one
   if(t.Substring(t.Length-1)!="_")
   {
       Dts.Variables["SheetName"].Value = t;
   }
}

然后在 SSIS 中,我添加另一个变量 (SQL) 来构建我的 SQL。

添加表达式 SQL = "Select * from [" + @SheetName + "]"

最后将您的数据源设置为 Excel 源中的那个 SQL 变量。

注意:确保在属性中延迟对 Excel 连接的验证。

注意:确保在 Excel Conn 上将 ConnString 的表达式设置为 "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + @fName + ";Extended Properties=\"Excel 8.0;HDR=YES\ ";"

【讨论】:

  • 谢谢,K 和大家,我试过了,但似乎错过了一些要点,所以我 1. 将脚本任务添加到 FOR LOOP BOX 或在其任务中添加脚本组件。
  • 2.有点混淆“新变量“选择* from [”+“你的变量”+“]”,我认为脚本会填充这个Var,它是在脚本中定义的。
  • 我会添加一些带有解释的图片
  • 添加了图片和说明。这对我来说很难,因为我在回答中看不到工作中的图像
猜你喜欢
  • 2015-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多