Manipulating Excel data
/*Manipulating Excel data using OPENDATASOURCE and OPENROWSET -------------------------------------------------------------
If you don't want to do the linking, you can also read the data like this*/
SELECT * FROM OPENDATASOURCE( 'Microsoft.Jet.OLEDB.4.0', 'Data Source="C:\CambridgePubs.xls"; Extended properties=Excel 8.0')...CambridgePubs --and write to it
UPDATE OPENDATASOURCE ('Microsoft.Jet.OleDB.4.0', 'Data Source="C:\CambridgePubs.xls"; extended Properties=Excel 8.0')...CambridgePubs SET Address='St. Kilda Road, Cambridge' WHERE Pubname = 'Jenny Wren'
INSERT INTO OPENDATASOURCE ('Microsoft.Jet.OleDB.4.0', 'Data Source="C:\CambridgePubs.xls"; extended Properties=Excel 8.0')...CambridgePubs (Pubname,Address,Postcode ) SELECT 'The St George','65 Cavendish Road','CB2 4RT'
--You can read and write toExcel Sheet using OpenRowSet, --if the mood takes you
SELECT * FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0', 'Excel 8.0;DATABASE=C:\CambridgePubs.xls', 'Select * from CambridgePubs')
UPDATE OPENROWSET('Microsoft.Jet.OLEDB.4.0', 'Excel 8.0;DATABASE=c:\CambridgePubs.xls', 'Select * from CambridgePubs') SET Address='34 Glemsford Road' WHERE Address = '65 Cavendish Road'
INSERT INTO OPENROWSET ('Microsoft.Jet.OLEDB.4.0', 'Excel 8.0;DATABASE=c:\CambridgePubs.xls', 'Select * from CambridgePubs') (Pubname, Address, Postcode) SELECT 'The Bull', 'Antioch Road','CB2 5TY'
/*Creating Excel Spreadsheets using sp_makewebtask --------------------------------------------------
Instead of creating the Excel spreadsheet with OLEDB One can use the sp_makewebtask
Users must have SELECT permissions to run a specified query and CREATE PROCEDURE permissions in the database in which the query will run. The SQL Server account must have permissions to write the generated HTML document to the specified location. Only members of the sysadmin server role can impersonate other users. */
sp_makewebtask @outputfile = 'c:\CambridgePubsHTML2.xls', @query = 'Select * from ##CambridgePubs', @colheaders =1, @FixedFont=0,@lastupdated=0,@resultstitle='Cambridge Pubs', @dbname ='MyDatabaseName'
/* This is fine for distributing information from databases but no good if you subsequently want to open it via ODBC.*/
/*OLE Automation ----------------
So far, so good. However, we really want rather more than this. When we create an excel file for a business report, we want the data and we also want nice formatting, defined ranges, sums, calculated fields and pretty graphs. If we do financial reporting, we want a pivot table and so on in order to allow a degree of data mining by the recipient. A different approach is required.
We can, of course, use Excel to extract the data from the database. However, in this example, we'll create a spreadsheet, write the data into it, fit the columns nicely and define a range around the data |
手机版|小黑屋|BC Morning Website ( Best Deal Inc. 001 )
GMT-8, 2026-4-10 00:50 , Processed in 0.017303 second(s), 21 queries .
Supported by Weloment Group X3.5
© 2008-2026 Best Deal Online