脚本任务在SSIS导入excel电子表格

我已经回顾了可能有我的答案的问题,不幸的是,他们似乎并不适用。 这是我的情况。 我必须从我的客户端导入工作表。 在A,C,D和AA列客户有我需要的信息。 列的平衡有什么我是毫无价值的信息。 列标题在我需要的四列中是一致的,但是在列中不一致很重要。 例如单元格A1包含分区。 所有电子表格都是如此。 细胞B1可以包含任何从套pipe长度到全长以适应。 我需要做的是只导入我需要的列并将其映射到SQL 2008 R2表。 我已经在当前正在调用SSIS函数的存储过程中定义表。

问题是,当我尝试导入具有不同列名称的电子表格时,SSIS将失败,我必须重新手动运行以使字段设置正确。

我无法想象我以前从未做过的事情。 就这样,规模不会丢失,我有170个用户谁拥有超过120个不同的电子表格模板。

我迫切需要一个可行的解决scheme。 在SQL中将文件存入我的表之后,我可以做所有的事情。 我甚至编写了将文件移回FTP服务器的代码。

我把一篇文章放在一起描述我如何使用脚本任务来parsingExcel 。 它允许我将明确的非表格数据导入到数据stream中。

核心概念是您将使用JET或ACE提供程序,只需从Excel工作表/命名范围中查询数据即可。 一旦你有了,你有一个数据集,你可以逐行走,并执行任何你需要的逻辑。 在你的情况下,你可以跳过第1行的头,然后只导入列A,C,D和AA。

该逻辑将在ExcelParser类中进行。 所以,第71行的Foreach循环可能会被蒸馏成类似(代码近似)

// This gets the value of column A current = dr[0].ToString(); // this assigns the value of current into our output row at column 0 newRow[0] = current; // This gets the value of column C current = dr[2].ToString(); // this assigns the value of current into our output row at column 1 newRow[1] = current; // This gets the value of column D current = dr[3].ToString(); // this assigns the value of current into our output row at column 2 newRow[2] = current; // This gets the value of column AA current = dr[26].ToString(); // this assigns the value of current into our output row at column 3 newRow[3] = current; 

你显然可能需要进行types转换等,但这是parsing逻辑的核心。