Overwrite data in excel using SSIS

  • 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'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

  • 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 🙂

  • 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
    The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
    - Phil Parkin

  • 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!

  • 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!

  • 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

  • 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

  • Thanks pss, I'm going to give that a try today.

    Matt

  • 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

  • Right... execute SQL Task. Sorry about that. I'm glad it helped, though!

    PSS

  • Thanks Pss,

    I think I will use your method.

  • 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

  • What is your resultset property set to?

    The absence of evidence is not evidence of absence
    - Martin Rees
    The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
    - Phil Parkin

  • 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