gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 02:55:36 25 4
gpt4 key购买 nike

我已经查看了可能有我的答案的问题,但不幸的是它们似乎并不适用。这是我的情况。我必须从客户那里导入工作表。在 A、C、D 和 AA 列中,客户有我需要的信息。各列的余额对我来说是毫无值(value)的信息。列标题在我需要的四列中是一致的,但在无关紧要的列中非常不一致。例如,单元格 A1 包含 Division。所有电子表格都是如此。单元格 B1 可以包含从袖长到总长度的任何内容以适合。我需要做的是仅导入我需要的列并将它们映射到 SQL 2008 R2 表。我已经在当前正在调用 SSIS 函数的存储过程中定义了该表。

问题是,当我尝试导入具有不同列名称的电子表格时,SSIS 失败,我必须返回手动运行它才能正确设置字段。

我无法想象我正在尝试做的事情以前没有人做过。为了不丢失数量级,我有 170 个用户,他们拥有 120 多个不同的电子表格模板。

我迫切需要一个可行的解决方案。将文件放入 SQL 中的表后,我可以执行所有操作。我什至编写了将文件移回 FTP 服务器的代码。

最佳答案

我整理了一篇文章,描述了我如何使用 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;

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

关于excel - SSIS中的脚本任务导入Excel电子表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15980580/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com