create the spreadsheet with the correct headings
There are two possible ways one might do this. The most obvious way is using the CREATE statement to create the worksheet and define the columns, but there seems to be no way of doing this by linking the excel FILE, unless the Excel file already exists. We need a utility stored procedure to get at ADODB in order to create databases and execute DDL and SQL against it. */
CREATE PROCEDURE spExecute_ADODB_SQL @DDL VARCHAR(2000), @DataSource VARCHAR(100), @Worksheet VARCHAR(100)=NULL, @ConnectionString VARCHAR(255) = 'Provider=Microsoft.Jet.OLEDB.4.0; Data Source=%DataSource; Extended Properties=Excel 8.0' AS DECLARE @objExcel INT, @hr INT, @command VARCHAR(255), @strErrorMessage VARCHAR(255), @objErrorObject INT, @objConnection INT, @bucket INT
SELECT @ConnectionString =REPLACE (@ConnectionString, '%DataSource', @DataSource) IF @Worksheet IS NOT NULL SELECT @DDL=REPLACE(@DDL,'%worksheet',@Worksheet)
SELECT @strErrorMessage='Making ADODB connection ', @objErrorObject=NULL EXEC @hr=sp_OACreate 'ADODB.Connection', @objconnection OUT IF @hr=0 SELECT @strErrorMessage='Assigning ConnectionString property "' + @ConnectionString + '"', @objErrorObject=@objconnection IF @hr=0 EXEC @hr=sp_OASetProperty @objconnection, 'ConnectionString', @ConnectionString IF @hr=0 SELECT @strErrorMessage ='Opening Connection to XLS, for file Create or Append' IF @hr=0 EXEC @hr=sp_OAMethod @objconnection, 'Open' IF @hr=0 SELECT @strErrorMessage ='Executing DDL "'+@DDL+'"' IF @hr=0 EXEC @hr=sp_OAMethod @objconnection, 'Execute', @Bucket out , @DDL IF @hr<>0 BEGIN DECLARE @Source VARCHAR(255), @Description VARCHAR(255), @Helpfile VARCHAR(255), @HelpID INT
EXECUTE sp_OAGetErrorInfo @objErrorObject, @source output, @Description output,@Helpfile output,@HelpID output SELECT @strErrorMessage='Error whilst ' +COALESCE(@strErrorMessage,'doing something')+', ' +COALESCE(@Description,'') RAISERROR (@strErrorMessage,16,1) END EXEC @hr=sp_OADestroy @objconnection GO -------------------------------------- /* Now we have it, it is easy */
spExecute_ADODB_SQL @DDL='Create table CambridgePubs (Pubname Text, Address Text, Postcode Text)', @DataSource ='C:\CambridgePubs.xls' --the excel file will have been created on the Database server of the -- database you currently have a connection to
--We could now insert data into the spreadsheet, if we wanted spExecute_ADODB_SQL @DDL='insert into CambridgePubs (Pubname,Address,Postcode) values (''The Bird in Hand'', ''23, Marshall Road, Cambridge CB4 2DQ'', ''CB4 2DQ'')', @DataSource ='C:\CambridgePubs.xls'
--you could drop it again! spExecute_ADODB_SQL @DDL='drop table CambridgePubs', @DataSource ='c:\CambridgePubs.xls'
/* Manipulating Excel data via a linked server ----------------------------------------------
We can now link to the created excel file as follows */
EXEC sp_addlinkedserver 'CambridgePubDatabase', @srvproduct = '', @provider = 'Microsoft.Jet.OLEDB.4.0', @datasrc = 'C:\CambridgePubs.xls', @provstr = 'Excel 8.0;' GO
EXEC sp_addlinkedsrvlogin 'CambridgePubDatabase', 'false' GO
--to drop the link, we do this! --EXEC sp_dropserver 'CambridgePubDatabase', 'droplogins'
-- Get the spreadsheet data via OpenQuery SELECT * FROM OPENQUERY (CambridgePubDatabase, 'select * from [CambridgePubs]') GO --or more simply, do this SELECT * FROM CambridgePubDatabase...CambridgePubs
--so now we can insert our data into the Excel Spreadsheet INSERT INTO CambridgePubDatabase...CambridgePubs (Pubname, Address, postcode) SELECT Pubname, Address, postcode FROM ##CambridgePubs
/*Synchronizing the Spreadsheet with SQL Server tables ------------------------------------------------------
As we are directly manipulating the Excel data in the worksheet as if it was a table we can do joins.
What about synchronising the table after editing the excel spreadsheet?
To try this out, you'll need to delete, alter and insert a few rows from the excel spreadsheet, remembering to close it after you've done it */
--Firstly, we'll delete any rows from ##CambridgePubs -- that do not exist in the Excel spreadsheet
DELETE FROM ##CambridgePubs FROM ##CambridgePubs c LEFT OUTER JOIN CambridgePubDatabase...CambridgePubs ex ON c.address LIKE ex.address AND c.pubname LIKE ex.pubname AND c.postcode LIKE ex.postcode WHERE ex.pubname IS NULL
-- then we insert into #CambridgePubs any rows in the spreadsheet -- that don't exist in #CambridgePubs
INSERT INTO ##CambridgePubs (Pubname,Address,Postcode) SELECT ex.Pubname,ex.Address,ex.Postcode FROM CambridgePubDatabase...CambridgePubs ex LEFT OUTER JOIN ##CambridgePubs c ON c.address LIKE ex.address AND c.pubname LIKE ex.pubname AND c.postcode LIKE ex.postcode WHERE c.pubname IS NULL
--all done (reverse syncronisation would be similar) |
手机版|小黑屋|BC Morning Website ( Best Deal Inc. 001 )
GMT-8, 2026-4-10 00:50 , Processed in 0.036872 second(s), 22 queries .
Supported by Weloment Group X3.5
© 2008-2026 Best Deal Online