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

时间:2013-04-12 20:47:58

标签: excel ssis

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

问题在于,当我尝试导入具有不同列名的电子表格时,SSIS会失败,我必须手动返回以使字段设置正确。

我无法想象我之前所做的事情还没有完成。只是这样,幅度不会丢失,我有170个用户拥有超过120种不同的电子表格模板。

我迫切需要一个可行的解决方案。在SQL中将文件放入我的表后,我可以做任何事情。我甚至编写了代码来将文件移回FTP服务器。

1 个答案:

答案 0 :(得分:3)

我整理了一篇描述我如何使用Script task to parse Excel的帖子。它允许我将明确的非表格数据导入数据流。

核心概念是您将使用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;

你显然可能需要进行类型转换等,但这是解析逻辑的核心。