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


With OLE Automation how to Transfer data from Excel to SQL Table


With OLE Automation how to Transfer data from Excel to SQL Table

Author
Message
sententiarum
sententiarum
Forum Newbie
Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)

Group: General Forum Members
Points: 8 Visits: 180
Hi,

My code works fine for copy from one sheet to another and then saving a new Excel file or CSV.

My real problem is how to copy ranges from Excel to an SQL Table with OLE Automation.

--   
--
DECLARE @Object AS INT
DECLARE @HResult AS INT
DECLARE @HResult_OUT AS INT
DECLARE @File AS INT
DECLARE @FileSize AS INT
DECLARE @NetworkObject AS INT
DECLARE @FileSystemObject AS INT

DECLARE @Workbooks AS INT
DECLARE @Worksheets AS INT
DECLARE @SheetsORG AS INT
DECLARE @SheetsDST AS INT
DECLARE @Ranges AS INT


DECLARE @PATH AS NVARCHAR(128)
DECLARE @Nome AS VARCHAR(60)

DECLARE @xlExcel12 AS INT
DECLARE @xlLocalSessionChanges AS INT

SET @xlExcel12 = 50
SET @xlLocalSessionChanges = 2

--
--   Criar uma instancia do objecto Excel.Application
--
EXEC @HResult = sp_OACreate 'Excel.Application', @Object OUT
PRINT 'Create Excel.Application: ' + BD_GESTAOKPI.dbo.ufn_HexToChar(@HResult)
--
--   Suprimir quaisquer alertas
--
EXEC @HResult = sp_OASetProperty @Object, 'DisplayAlerts', 'false'
PRINT 'Not To DisplayAlerts: ' + BD_GESTAOKPI.dbo.ufn_HexToChar(@HResult)
--
--   Ficheiro de Teste
--
SET @PATH = N'C:\DPC_IRP\20130914_EXT_DETALHE.csv'
--
--   Obter um objecto do tipo collection de Workbooks associado ao ficheiro de teste
--
EXEC @HResult = sp_OAMethod @Object, 'Workbooks.Open', @Workbooks OUT, @PATH
PRINT 'Workbooks Object: ' + BD_GESTAOKPI.dbo.ufn_HexToChar(@HResult)
--
--   Ativar o 1º Woorkbook
--
EXEC @HResult = sp_OAMethod @Workbooks, 'Activate', NULL
PRINT 'Activate Workbooks(1): ' + BD_GESTAOKPI.dbo.ufn_HexToChar(@HResult)
--
--   Ativar a 1ª sheet
--
EXEC @HResult = sp_OAMethod @Workbooks, 'Sheets.Item(1)', @SheetsORG OUTPUT
PRINT 'Sheets Object: ' + BD_GESTAOKPI.dbo.ufn_HexToChar(@HResult)

EXEC @HResult = sp_OAMethod @Workbooks, 'Sheets.Item(2)', @SheetsDST OUTPUT
PRINT 'Sheets Object: ' + BD_GESTAOKPI.dbo.ufn_HexToChar(@HResult)

EXEC @HResult = sp_OAMethod @SheetsORG, 'Copy', NULL, @before=NULL, @after=@SheetsDST
PRINT 'Sheets Object: ' + BD_GESTAOKPI.dbo.ufn_HexToChar(@HResult)

SET @PATH = N'C:\DPC_IRP\Teste_SQL_vs_EXCEL_COPIA_PP.csv'

EXEC @HResult = sp_OAMethod @Workbooks, 'SaveAs', NULL, @Filename=@PATH
PRINT 'SaveAs Workbook: ' + BD_GESTAOKPI.dbo.ufn_HexToChar(@HResult)
--
--   Fechar o Workbook
--
EXEC @HResult = sp_OAMethod @Workbooks, 'Close', null
PRINT 'Close Workbook: ' + BD_GESTAOKPI.dbo.ufn_HexToChar(@HResult)
--
--   Ativar novamente a produção de alertas para o objecto Excel.Application
--
EXEC @HResult = sp_OASetProperty @Object, 'DisplayAlerts', 'True'
PRINT 'Yes To DisplayAlerts: ' + BD_GESTAOKPI.dbo.ufn_HexToChar(@HResult)
--
--   Destruir o objecto Excel.Application
--
EXEC @HResult = sp_OAMethod @Object , 'Application.Quit'
PRINT 'Quit Excel.Apllication: ' + BD_GESTAOKPI.dbo.ufn_HexToChar(@HResult)


My example copy's from one sheet to another, but I need to copy from Excel to SQL Tables.

Thanks
Vikash Kumar Singh
Vikash Kumar Singh
SSC Journeyman
SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)

Group: General Forum Members
Points: 81 Visits: 243
To read range from Excel in SSIS you can refer this:
SSIS: Read single cell values in excel using RANGE name

Vikash Kumar Singh || www.singhvikash.in
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