简体   繁体   English

使用SSIS将数据导出到Excel文件

[英]Export data to Excel file using SSIS

I have SSIS Package which Exports Data from table to Excel file 我有SSIS包,它将数据从表导出到Excel文件

Control Flow :- 控制流 :-

控制流

Data Flow :- 数据流 :-

在此输入图像描述

This is My Step :- 这是我的步骤: -

  1. Drop Excel Table 删除Excel表
  2. Create Excel Table with format as of my Select Query which i used to retrieve data from database 创建Excel表格,格式为我的Select Query,用于从数据库中检索数据
  3. Insert Data from Database to Excel file 将数据从数据库插入Excel文件

I Used Query Like Select * From Table Where --Some Condition 我使用查询像Select * From Table Where --Some Condition

I retrieve 3000 rows out of 10000 rows and put that 3000 rows in my excel sheet. 我从10000行中检索3000行,并将这3000行放入我的Excel工作表中。 But when open my excel sheet i saw scrollbar which goes till 10000th row and ends hence my excel sheet size also increses . 但是当打开我的Excel工作表时,我看到滚动条直到第10000行并且结束,因此我的Excel工作表大小也增加了。 how can i reduce my excel sheet size ? 如何减少我的Excel表格尺寸? my excel sheet contains only 3000 rows then why blank cells which goes till 10000th row ? 我的excel表只包含3000行,那么为什么空白单元格会持续到第10000行?

SQL Server 2008 & Visual Studio 2008 with BIDS 带有BIDS的SQL Server 2008和Visual Studio 2008

I believe your issue is around the method in which you are using to create the file. 我相信您的问题与您用于创建文件的方法有关。 You have two alternatives and both should fix your issue: 您有两种选择,两者都应该解决您的问题:

Solution #1: 解决方案#1:

You can create an Excel file with those predefined columns, essentially your empty output file - this would act as your 'Template File'. 您可以使用这些预定义列创建Excel文件,基本上是空输出文件 - 这将充当您的“模板文件”。 Your flow would then be this: 你的流程将是这样的:

  1. File System Task - Copy template file to output or working directory (rename if necessary) 文件系统任务 - 将模板文件复制到输出或工作目录(必要时重命名)
  2. OLEDB Source Task - Query your source for the data (3000) OLEDB源任务 - 查询数据来源(3000)
  3. Data Conversion Task 数据转换任务
  4. Excel Destination Task - Put data into new Excel file Excel目标任务 - 将数据放入新的Excel文件中

Note: You already have steps 2 thru 3 complete, you just need to make sure you are connecting to the new Excel file. 注意:您已完成第2步到第3步,只需确保连接到新的Excel文件即可。 Also, to clarify, step 1 is outside the Control Flow Task. 另外,为了澄清,步骤1在控制流程任务之外。

This way is helpful because you always have a blank and consistently formatted Excel file to copy and work with. 这种方式很有用,因为您始终有一个空白且格式一致的Excel文件可供复制和使用。

Solution #2: 解决方案#2:

The other option is to use a Script Task and create the Excel file - you could also load the data into the file in this task. 另一个选项是使用脚本任务并创建Excel文件 - 您还可以在此任务中将数据加载到文件中。 This requires some basic understanding of VB.NET or C#. 这需要对VB.NET或C#有一些基本的了解。 Basically you would need to get a XLS library (like NPOI ). 基本上你需要一个XLS库(如NPOI )。 This is more complicated, but gives you the best functionality. 这更复杂,但为您提供最佳功能。

I recommend you try solution #1 and see how that works for you. 我建议您尝试解决方案#1,看看它对您有何帮助。

Drop table SheetName doesn't delete the sheet instead it just deletes the row . Drop table SheetName不会删除该表,而只是deletes the row If for the 1st time you have loaded 10K rows and then again executed the package by restricting the number of rows to 3K ,the excel file will still contain those 10K empty rows as it retains the sheet along with the empty spaces . 如果第一次加载10K行然后通过将行数限制为3K再次执行包,则excel文件仍将包含那些10K空行,因为它保留了工作表和empty spaces

You can use script task to delete the sheet using COM obects .But for that you need to place the Excel PIA(Primary Interop Assemply) to make it visible for VSA or else create a new excel file every time the package runs 您可以使用脚本任务使用COM对象删除工作表。但是,您需要放置Excel PIA(主Interop Assemply)以使其对VSA可见,或者每次包运行时创建新的Excel文件

Else as suggested by Nicarus use File System Task to delete the existing file and create a new Excel file on every execution . 另外,按照Nicarus的建议,使用File System Task删除现有文件,并在每次执行时创建一个新的Excel文件。

Diagram : 图:

在此输入图像描述

File System Task : 文件系统任务:

在此输入图像描述

Use the same components and the query for Create Table using Execute SQL task and your DFT 使用相同的组件和使用Execute SQL taskDFT Create Table的查询

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

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