如何使用SSIS包将大数据集拆分为多个Excel电子表格?

我正面临一个SSIS包的问题。

  • 执行查询以从DataBase(SQL Server 2008)(执行的数据stream任务)获取一些数据
  • 使用Excel Destination将提取的数据导出到Excel 97-2003电子表格(.xls)

正如你们大多数人知道的xls文件是有限的每张到65,536行×256列。 因此,当查询提取超出logging限制(65,536)时,Excel目标步骤失败。

我收到以下错误消息。

Error: 0xC0202009 at Calidad VIDA, Excel Destination [82]: SSIS Error Code DTS_E_OLEDBERROR. An OLE DB error has occurred. Error code: 0x80004005.

Error: 0xC0209029 at Calidad VIDA, Excel Destination [82]: SSIS Error Code DTS_E_INDUCEDTRANSFORMFAILUREONERROR. The "input "Excel Destination Input" (93)" failed because error code 0xC020907B occurred, and the error row disposition on "input "Excel Destination Input" (93)" specifies failure on error. An error occurred on the specified object of the specified component. There may be error messages posted before this with more information about the failure. Error: 0xC0047022 at Calidad VIDA, SSIS.Pipeline: SSIS Error Code DTS_E_PROCESSINPUTFAILED. The ProcessInput method on component "Excel Destination" (82) failed with error code 0xC0209029 while processing input "Excel Destination Input" (93). The identified component returned an error from the ProcessInput method. The error is specific to the component, but the error is fatal and will cause the Data Flow task to stop running. There may be error messages posted before this with more information about the failure.

Error: 0xC02020C4 at Calidad VIDA, OLE DB Source [1]: The attempt to add a row to the Data Flow task buffer failed with error code 0xC0047020.

Error: 0xC0047038 at Calidad VIDA, SSIS.Pipeline: SSIS Error Code DTS_E_PRIMEOUTPUTFAILED. The PrimeOutput method on component "OLE DB Source" (1) returned error code 0xC02020C4. The component returned a failure code when the pipeline engine called PrimeOutput(). The meaning of the failure code is defined by the component, but the error is fatal and the pipeline stopped executing. There may be error messages posted before this with more information about the failure.

该文件需要采用这种格式,因为客户端没有更新的版本。 而且他们不想购买许可证。 有谁知道如何解决这个问题? 我应该使用一个脚本任务,并由我自己的Excel,或者我应该做一个循环,创造各种优秀woorkbooks?

以下是一个可能的选项,您可以根据您要为每个Excel工作表编写多less条logging,使用SSISdynamic创buildExcel工作表。 这不涉及脚本任务。 以下示例描述了如何使用“执行SQL任务”,“For循环”容器和“数据stream任务”来实现此目的。 该示例是使用SSIS 2008 R2创build的。

循序渐进的过程:

  1. 在SQL Server数据库中,运行“ SQL脚本”部分下提供的脚本 。 这些脚本将创build一个名为dbo.SQLData的表,然后使用1 x 120 x 40乘法数据填充表,从而创build800条logging。 该脚本还创build一个名为dbo.FetchData的存储过程,该存储过程将在SSIS包中使用。

  2. 在SSIS包中,创build9个variables,如屏幕截图# 1所示。 以下步骤描述如何configuration每个variables。

  3. 设置值为80的variablesExcelSheetMaxRows 。 该variables表示每个Excel工作表要写入的行数。 您可以将其设置为您select的值。 在你的情况下,这将是65535 (你可能想留下1行标题列名称)。

  4. 使用值SELECT COUNT(Id) AS TotalRows FROM dbo.SQLData设置variablesSQLFetchTotalRows 。 此variables包含从表中获取总行数的查询。

  5. selectvariablesStartIndex并按F4select属性。 将属性EvaluateAsExpression设置为True ,并将属性Expression设置为值(@[User::Loop] * @[User::ExcelSheetMaxRows]) + 1 。 请参阅截图# 2

  6. selectvariablesEndIndex并按F4select属性。 将属性EvaluateAsExpression设置为True ,将属性Expression设置为值(@[User::Loop] + 1) * @[User::ExcelSheetMaxRows] 。 请参阅截图# 3

  7. selectvariablesExcelSheetName并通过按F4select属性。 将属性EvaluateAsExpression设置为True ,将属性Expression设置为值"Sheet" + (DT_WSTR,12) (@[User::Loop] + 1) 。 请参阅截图# 4

  8. selectvariablesSQLFetchData并按F4select属性。 将属性EvaluateAsExpression设置为True ,将属性Expression设置为值"EXEC dbo.FetchData " + (DT_WSTR, 15) @[User::StartIndex] + "," + (DT_WSTR, 15) @[User::EndIndex] 。 请参阅截图# 5

  9. selectvariablesExcelTable并按F4select属性。 将属性EvaluateAsExpression设置为True ,将Expression属性设置为ExcelTable Variable Value部分提供的 。 请参阅截图# 6

  10. 在SSIS包的控制stream选项卡上,放置一个执行SQL任务并configuration它,如屏幕截图# 7和# 8所示 。 此任务将获取logging计数。

  11. 在SSIS包的控制stream选项卡上,放置一个For循环容器并configuration它,如屏幕截图# 9所示。 请注意这是For循环而不是Foreach循环。 此循环将根据每个Excel表中显示的logging数与表中find的logging总数一起执行。

  12. 创build包含.xls扩展名的Excel 97-2003格式的Excel电子表格,如截屏# 10所示。 我在** C:\ temp **中创build了该文件

  13. 在SSIS包的连接pipe理器上,创build一个名为SQLServer指向SQL Server的OLE DB连接和一个名为Excel的Excel连接,指向新创build的Excel文件。

  14. 点击Excel连接并select属性。 将属性DelayValidation从False更改为True,以便在数据stream任务中切换为使用variables创build图纸时,不会收到任何错误消息。 请参阅截图# 11

  15. 在For循环容器中,放置一个执行SQL任务,并按照屏幕截图# 12所示进行configuration。 该任务将根据需求创buildExcel工作表。

  16. 在For循环容器中,放置一个数据stream任务。 一旦这些任务被configuration完毕,控制stream选项卡应该如屏幕截图# 13所示。

  17. 在数据stream任务内部,放置一个OLE DB源,使用存储过程从SQL Server读取数据。 如屏幕截图# 14和# 15所示configurationOLE DB Source。

  18. 在数据stream任务内部,放置一个Excel目标以将数据插入到Excel工作表中。 如屏幕截图# 16和# 17所示configurationExcel目标。

  19. 一旦数据stream任务被configuration,它应该看起来像截屏# 18所示。

  20. 删除在第12步中创build的Excel文件,因为程序包将在执行时自动创build该文件。 如果不删除,程序包将抛出Sheet1已经存在的exception。 此示例使用pathC:\ temp \,屏幕截图# 19显示该path中没有文件。

  21. 屏幕截图# 20和# 21显示了控制stream和数据stream任务中的包的执行情况。

  22. 屏幕截图# 22显示文件ExcelData.xls已在pathC:\ temp中创build。 请记住,早些时候这条路是空的。 由于我们在表格中有800行,并且我们将包variablesExcelSheetMaxRows设置为每个表格创build80行。 因此,Excel文件有10张。 请参阅屏幕截图# 23

  23. NOTE:在这个例子中,我没有做的一件事是检查文件ExcelData.xls是否已经存在于pathC:\ temp中。 如果存在,那么在执行任务之前应该删除该文件。 这可以通过创build一个保存Excel文件path的variables并使用文件系统任务在执行第一个执行SQL任务之前删除该文件来实现。

希望有所帮助。

ExcelTablevariables值:

 "CREATE TABLE `" + @[User::ExcelSheetName] + "`(`Id` Long, `Number1` Long, `Number2` Long, `Value` Long)" 

SQL脚本:

 --Create table CREATE TABLE [dbo].[SQLData]( [Id] [int] IDENTITY(1,1) NOT NULL, [Number1] [int] NOT NULL, [Number2] [int] NOT NULL, [Value] [int] NOT NULL, CONSTRAINT [PK_Multiplication] PRIMARY KEY CLUSTERED ([Id] ASC)) ON [PRIMARY] GO --Populate table with data SET NOCOUNT ON DECLARE @OuterLoop INT DECLARE @InnerLoop INT SELECT @OuterLoop = 1 WHILE @OuterLoop <= 20 BEGIN SELECT @InnerLoop = 1 WHILE @InnerLoop <= 40 BEGIN INSERT INTO dbo.SQLData (Number1, Number2, Value) VALUES (@OuterLoop, @InnerLoop, @OuterLoop * @InnerLoop) SET @InnerLoop = @InnerLoop + 1 END SET @OuterLoop = @OuterLoop + 1 END SET NOCOUNT OFF --Create stored procedure CREATE PROCEDURE [dbo].[FetchData] ( @StartIndex INT , @EndIndex INT ) AS BEGIN SELECT Id , Number1 , Number2 , Value FROM ( SELECT RANK() OVER(ORDER BY Id) AS RowNumber , Id , Number1 , Number2 , Value FROM dbo.SQLData ) T1 WHERE RowNumber BETWEEN @StartIndex AND @EndIndex END GO 

屏幕截图#1:

1

屏幕截图#2:

2

屏幕截图3:

3

屏幕截图#4:

4

屏幕截图5:

五

屏幕截图#6:

6

屏幕截图#7:

7

屏幕截图#8:

8

屏幕截图#9:

9

屏幕截图#10:

10

屏幕截图#11:

11

屏幕截图#12:

12

屏幕截图#13:

13

屏幕截图#14:

14

屏幕截图#15:

15

屏幕截图#16:

16

屏幕截图#17:

17

屏幕截图#18:

18

屏幕截图#19:

19

屏幕截图#20:

20

截图#21:

21

屏幕截图#22:

22

屏幕截图#23:

23

股票excel渲染选项允许选项分页到单独的选项卡上。 如果您在适当的行数之后强制分页,则会为输出中的每个“页面”获取一个新选项卡。 我没有过去使用过的设置,但是如果你需要我的话,我可以在明天查看它。

也为我工作是使用SSIS包导出到CSV文件,然后手动将数据导入到Excel中。 请注意,这与Excel中的“打开”不同,因为那也将停止在65536行。 创build一个新的xlsx文件,然后点击“数据” – >“从文本”。 它将导入并显示所有行。 testing了750,000行。

但是,不确定csv – > xlsx转换在SSIS包中是否容易编写脚本。 很可能通过使用Excel COM对象的脚本任务。