Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Overwrite data in excel using SSIS


Overwrite data in excel using SSIS

Author
Message
Princess
Princess
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 69
Hi,

I have creted a package that gets data from a table and adds it to a excel worksheet. The problem is everytime I run the packge it appends to the worksheet instead of overwriting the data in the worksheet, is it possible to overwrite the data in the same worksheet?

Thanks in advance.
MattQ-1003536
MattQ-1003536
Grasshopper
Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)

Group: General Forum Members
Points: 20 Visits: 222
Hi,

I've just embarked on this same road myself (yesterday), and I know very little at this point. It's possible to programmatically manipulate worksheets from a Script task, but you need a reference that may or may not be readily available.
Some discussion here:
http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/8c04708c-ac33-41e8-914a-27adfe353c33.

Once the assembly is available, properties of the workbook and worksheet can be accessed, e.g. opening and closing the workbooks, selecting cells and ranges and entering data.
My quest is to clear data from a worksheet vs. overwriting prior to sending in new contents but I haven't figured out the syntax yet.

Regards
SQLQuest29
SQLQuest29
Right there with Babe
Right there with Babe (746 reputation)Right there with Babe (746 reputation)Right there with Babe (746 reputation)Right there with Babe (746 reputation)Right there with Babe (746 reputation)Right there with Babe (746 reputation)Right there with Babe (746 reputation)Right there with Babe (746 reputation)

Group: General Forum Members
Points: 746 Visits: 4311
Princess (4/1/2009)
Hi,

I have creted a package that gets data from a table and adds it to a excel worksheet. The problem is everytime I run the packge it appends to the worksheet instead of overwriting the data in the worksheet, is it possible to overwrite the data in the same worksheet?

Thanks in advance.


Hi ,

I am not sure if SSIS offers any option to overwrite the existing file for EXCEL. I have gone across to a good article and it may help you:

http://social.technet.microsoft.com/Forums/en-US/sqlintegrationservices/thread/9c21b7ee-5737-469d-8b96-62e0cad41616/

Thanks,

\\K

______________________________________________________________________________________________________________________________________________________________________________________
HTH !
Kin
MCTS : 2005, 2008
Active SQL Server Community Contributor :-)
Phil Parkin
Phil Parkin
SSCrazy Eights
SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)

Group: General Forum Members
Points: 8348 Visits: 19491
Is it possible to come at this problem from another angle? Just have a blank version of the spreadsheet living somewhere accessible, then do a delete (of old spreadsheet) and copy (from blank template), then do the inserting of records.


Help us to help you. For better, quicker and more-focused answers to your questions, consider following the advice in this link.

If the answer to your question can be found with a brief Google search, please perform the search yourself, rather than expecting one of the SSC members to do it for you.

Please surround any code or links you post with the appropriate IFCode formatting tags. It helps readability a lot.
Famson
Famson
SSC Rookie
SSC Rookie (47 reputation)SSC Rookie (47 reputation)SSC Rookie (47 reputation)SSC Rookie (47 reputation)SSC Rookie (47 reputation)SSC Rookie (47 reputation)SSC Rookie (47 reputation)SSC Rookie (47 reputation)

Group: General Forum Members
Points: 47 Visits: 230
You cannot overwrite the data in an excel worksheet with
the functionalities offered by SSIS to manipulate excel file.

I will suggest you include a cleanup process in the application
that uses the data in the excel file that will either delete
the file or rename it when done with the data.

If the data in the excel file is used by another SSIS package, then you can
include a File system task in the package to do the cleanup.

Cheers!
pss
pss
SSC-Enthusiastic
SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)

Group: General Forum Members
Points: 107 Visits: 158
There IS a way to do this in SSIS. I assume you already have a connection defined for your Excel file. Using this connection, you can clear a worksheet in an existing spreadsheet.

On the Data Control tab, drag a Data Flow Task onto the surface. Edit the task. For the Connection Type, use "EXCEL", for the Connection, use your existing Excel connection. For SQL Source Type, use "Direct input". And for the SQL Statement:

drop table `yourtable`
GO

('yourtable' is the name of the worksheet).

Have this task flow into another Data Flow Task, using the same parameters, above, except for the SQL Statement:


CREATE TABLE `yourtable`(
`column1` VarChar(50),
`column2` VarChar(15),
`column3` VarChar(25),
`column4` DateTime
)
GO

(with your columns defined).

Have this task flow into your extract Data Flow Task.

Hope this helps!
MattQ-1003536
MattQ-1003536
Grasshopper
Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)

Group: General Forum Members
Points: 20 Visits: 222
All good suggestions. I like the template idea, seems the cleanest way to do it. I saw another method whereby you select a huge range of cells and clear the contents. OK if you can predict a range you'll never exceed.

The Excel destination task doesn't like you to delete the file as it creates the workbook during design then expects it to be there afterwords on subsequent executions.
There is a way to make it create a new one each time but I haven't tried it yet.

http://social.technet.microsoft.com/Forums/en-US/transactsql/thread/7f79cfde-2f1f-4cad-94f8-07397b998296/

I did try this, and it completed the Script task without error but didn't actually do anything other than open and close the spreadsheet.
OpenExcelWorkbook(FileName)
Try
_sheet = CType(_sheets(Tab1), Microsoft.Office.Interop.Excel.Worksheet)
_sheet.Select(Type.Missing)
_sheet.Delete()
Catch ex As Exception
MsgBox(ex.ToString)
CloseExcelWorkbook()
End Try
Without the Try/Catch it would leave the file in an open state and Excel would have to recover it if I tried to open it in the app.
There may be a way to do it, perhaps the interop assembly has some bug in this regard; it seems like a straightforward method. But I will likely go with templating and archiving.

Thanks all
pss
pss
SSC-Enthusiastic
SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)

Group: General Forum Members
Points: 107 Visits: 158
Matt,

Read my post, above. Basically, a spreadsheet is like a database, and a worksheet is like a table. With an Excel connection in SSIS, you can pretty much do anything with an Excel spreadsheet that you can do with a database. You don't need to access the Excel object model.

pss
MattQ-1003536
MattQ-1003536
Grasshopper
Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)

Group: General Forum Members
Points: 20 Visits: 222
Thanks pss, I'm going to give that a try today.

Matt
MattQ-1003536
MattQ-1003536
Grasshopper
Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)

Group: General Forum Members
Points: 20 Visits: 222
OK, finally got this working but with one minor correction.
That is, I needed to use an Execute SQL task vs. a Data Flow task to be able to specify the connection and SQL source type. I'm sure that was just a typo. But I dropped and recreated the worksheet table using two separate Execute SQL tasks, then ran the Data Flow task to an Excel destination.
Initially I had problems using my existing DFT to create the Excel output so I recreated the DFT and associated tasks; probably some internal metadata reference in the package was confusing it.
From there it worked as advertised.

1. Execute SQL task to drop the worksheet name. This cleared the worksheet although it still exists with that name. SSIS, however, believes that it's gone (try writing to it) ;-)
2. Execute SQL task to create the worksheetname.
3. Data Flow task to an Excel destination via the same Excel connection.

There are several variations on this theme out there but this is what I wanted to do.

Thanks pss!

Matt
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search