Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Overwrite data in excel using SSIS Expand / Collapse
Author
Message
Posted Wednesday, April 1, 2009 12:43 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, March 4, 2014 3:40 AM
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.
Post #687664
Posted Thursday, April 2, 2009 8:38 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, June 30, 2014 12:03 PM
Points: 20, Visits: 207
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
Post #688979
Posted Thursday, April 2, 2009 9:10 AM


Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Thursday, December 4, 2014 11:04 AM
Points: 739, Visits: 3,809
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
Post #689036
Posted Thursday, April 2, 2009 10:01 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 2:15 AM
Points: 5,317, Visits: 12,354
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.

When you ask a question (and please do ask a question: "My T-SQL does not work" just doesn't cut it), please provide enough information for us to understand its context.

It is better to keep your mouth shut and appear stupid than to open it and remove all doubt. (Mark Twain)
Post #689106
Posted Thursday, April 2, 2009 1:56 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Yesterday @ 8:43 AM
Points: 47, Visits: 202
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!
Post #689325
Posted Thursday, April 2, 2009 5:42 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Sunday, December 7, 2014 12:32 PM
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!
Post #689494
Posted Thursday, April 2, 2009 5:47 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, June 30, 2014 12:03 PM
Points: 20, Visits: 207
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
Post #689497
Posted Thursday, April 2, 2009 6:02 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Sunday, December 7, 2014 12:32 PM
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
Post #689500
Posted Friday, April 3, 2009 10:34 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, June 30, 2014 12:03 PM
Points: 20, Visits: 207
Thanks pss, I'm going to give that a try today.

Matt
Post #690082
Posted Friday, April 3, 2009 2:13 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, June 30, 2014 12:03 PM
Points: 20, Visits: 207
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
Post #690227
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse