简体   繁体   English

如何将数据从SQL表导出到SSIS中的多个excel文件?

[英]How to export data from SQL table to multiple excel files in SSIS?

I created an SSIS package that extracts data from SQL and load it into excel. 我创建了一个SSIS包,它从SQL中提取数据并将其加载到excel中。 I am having problem making this package dynamic and loading data into multiple excel files. 我有问题使这个包动态化并将数据加载到多个excel文件中。

I first created an execute SQL task that holds all the file names with SQL code: SELECT FileName FROM Files and its result set will be stored in a variable FileNameObj . 我首先创建了一个执行SQL任务,它使用SQL代码保存所有文件名: SELECT FileName FROM Files ,其结果集将存储在变量FileNameObj Then I created a Foreach Loop Container and added a Data Flow Task inside. 然后我创建了一个Foreach循环容器并在里面添加了一个数据流任务。

Foreach Loop Container setting: Foreach ADO Enumerator, ADO Object source variable - FileNameObj , Variable mapping - FileName and Index - 0. Under data flow task, I have added an OLE DB Source and Excel Destination pointed to file path: C:\\Test\\ABC.xlsx . Foreach循环容器设置: Foreach ADO枚举器,ADO对象源变量 - FileNameObj ,变量映射 - FileName和Index - 0.在数据流任务下,我添加了一个指向文件路径的OLE DB源和Excel目标: C:\\Test\\ABC.xlsx

Here is the Connection Manager Property: 这是Connection Manager属性:

ConnectionString : Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Test\\ABC.xlsx;Extended Properties="Excel 12.0;HDR=YES"; ConnectionStringProvider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Test\\ABC.xlsx;Extended Properties="Excel 12.0;HDR=YES";

DelayValidation set to True DelayValidation设置为True

ExcelFilePath is C:\\Test\\ABC.xlsx ExcelFilePathC:\\Test\\ABC.xlsx

Then I created an expression with Property ExcelFilePath and Expression: "C:\\\\Test\\\\"+ @[User::FileName]+".xlsx" Then my ConnectionString changes to: Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Test\\.xlsx;Extended Properties="Excel 12.0;HDR=YES"; 然后我用Property ExcelFilePath和Expression创建了一个表达式: "C:\\\\Test\\\\"+ @[User::FileName]+".xlsx"然后我的ConnectionString变为: Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Test\\.xlsx;Extended Properties="Excel 12.0;HDR=YES";

I have created all my files templates (same structure for all) in test folder already. 我已经在测试文件夹中创建了所有文件模板(所有文件都相同)。 I get the following error: 我收到以下错误:

Error at Data Flow Task [Excel Destination [131]]: SSIS Error Code DTS_E_OLEDBERROR. 数据流任务出错[Excel目标[131]]:SSIS错误代码DTS_E_OLEDBERROR。 An OLE DB error has occurred. 发生OLE DB错误。 Error code: 0x80040E37. 错误代码:0x80040E37。

Error at Data Flow Task [Excel Destination [131]]: Opening a rowset for "Sheet1$" failed. 数据流任务出错[Excel目标[131]]:打开“Sheet1 $”的行集失败。 Check that the object exists in the database. 检查数据库中是否存在该对象。

Exception from HRESULT: 0xC02020E8 (Microsoft.SqlServer.DTSPipelineWrap) 来自HRESULT的异常:0xC02020E8(Microsoft.SqlServer.DTSPipelineWrap)

What am I doing wrong? 我究竟做错了什么? Or is it even possible to do this in SSIS? 或者甚至可以在SSIS中执行此操作?

1

2

3]

4]

5]

6]

7]

8]

9]

10

11

12

See here: Dynamically assign filename to excel connection string 请参见此处: 动态为excel连接字符串指定文件名

Use ExcelFilePath , not ConnectionString 使用ExcelFilePath ,而不是ConnectionString

Just to clarify: 只是为了澄清:

  • If you are loading the same data with same columns into multiple excel sheets, it will be much easier to just export once and do a filecopy on the document 如果要将具有相同列的相同数据加载到多个Excel工作表中,则只需导出一次并对文档执行文件复制将更加容易
  • If each sheet has different columns then this is not going to work - each data flow needs to have identical columns each time it runs 如果每个工作表都有不同的列,那么这不会起作用 - 每个数据流在每次运行时都需要具有相同的列

  • If you are loading identical columns but different filtered data then you may want to consider why you are doing this. 如果您要加载相同的列但不同的过滤数据,那么您可能需要考虑为什么要这样做。 Is it to overcome row limits in excel? 是否要克服excel中的行限制? If so, export to CSV instead. 如果是这样,请导出为CSV。 Is it to generate custom reports to users? 它是否为用户生成自定义报告? Consider a reporting tool instead. 请考虑使用报告工具。

Update 1 更新1

Many things you should try: 你应该尝试很多事情:

  1. Install Access Database Engine 安装Access数据库引擎

Download Link: Microsoft Access Database Engine 2010 Redistributable 下载链接: Microsoft Access数据库引擎2010可再发行组件

  1. Run Package in 32-bit mode 以32位模式运行Package

In the Project properties, change the 64-bit Runtime property to False 在“项目”属性中,将64位运行时属性更改为False

  1. Check that the Sheet1 exists in all templates 检查Sheet1存在于所有模板中

  2. Assign a default value for the filepath variable 为filepath变量分配默认值


Initial answer 初步答复

Dynamic Excel connection string 动态Excel连接字符串

First of all, the excel connectionstring for .XLSX format is like the following: 首先, .XLSX格式的excel连接.XLSX如下所示:

"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Test\\"+ @[User::FileName]+ ".xlsx; Extended Properties=\"Excel 12.0 Xml;HDR=YES\";"

And the recommended way is (what @Nick.McDermaid mentioned) , to assign the value to ExcelFilePath property 推荐的方法是(@ Nick.McDermaid提到的) ,将值赋给ExcelFilePath属性

But you must take into consideration many other things: 但你必须考虑许多其他事情:

  1. All excel files must have the same structure, if not this package will always fail . 所有excel文件必须具有相同的结构, 否则此包将始终失败
  2. Set the Data Flow Task Delay Validation property to True 将“数据流任务Delay Validation属性设置为“ True

References 参考

I have found a solution to my own problem: Assign a default value to FileName variable. 我找到了解决自己问题的方法:为FileName变量分配一个默认值。 I assigned ABC and it worked. 我分配了ABC并且它有效。 Generated two files as expected, one for ABC and one for DEF. 按预期生成两个文件,一个用于ABC,一个用于DEF。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM