April 1, 2009 at 12:43 am
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.
April 2, 2009 at 8:38 am
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
April 2, 2009 at 9:10 am
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:
Thanks,
\\K
______________________________________________________________________________________________________________________________________________________________________________________
HTH !
Kin
MCTS : 2005, 2008
Active SQL Server Community Contributor 🙂
April 2, 2009 at 10:01 am
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.
The absence of evidence is not evidence of absence.
Martin Rees
You can lead a horse to water, but a pencil must be lead.
Stan Laurel
April 2, 2009 at 1:56 pm
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!
April 2, 2009 at 5:43 pm
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!
April 2, 2009 at 5:47 pm
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.
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
April 2, 2009 at 6:02 pm
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
April 3, 2009 at 10:34 am
Thanks pss, I'm going to give that a try today.
Matt
April 3, 2009 at 2:13 pm
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
April 3, 2009 at 3:20 pm
Right... execute SQL Task. Sorry about that. I'm glad it helped, though!
PSS
April 5, 2009 at 6:50 am
Thanks Pss,
I think I will use your method.
August 20, 2009 at 3:18 am
Hi,
I tried to delete the worksheet using excecute sql task. But i m getting a error "sysntax error in drop table or drop index". possible failure reasons" problems with the query,"resultset" property not set correctly,parameters not set correctly.....",when i execute the data manager package.
Syntax i was using to delete is drop table 'members'. "members" is the work sheet name, i want to delete.
Any help on this will be appreciated.
cheers,
Ashok
August 20, 2009 at 3:27 am
What is your resultset property set to?
The absence of evidence is not evidence of absence.
Martin Rees
You can lead a horse to water, but a pencil must be lead.
Stan Laurel
August 22, 2009 at 12:02 am
Hi !
It is possible to insert the values into the same excel sheet without appending. U need to delete the records and then insert the values. Please let me know detail about the MS Excel
Thanks
Viewing 15 posts - 1 through 15 (of 15 total)
You must be logged in to reply to this topic. Login to reply