简体   繁体   中英

Adding column to worksheet in Excel with OLEDB

Hello
I'm trying to add new column to the Excel worksheet by command

ALTER TABLE [MyTable] ADD COLUMN Field_dest nvarchar

But on execution of the command got exception "Invalid operation" I tried table name with and without $ at the end , but got the same result
My questions are
1) Is there some wrong in the command above?
2) Is command ALTER table supported for excel table ?
3) Is the alternative way to add column into excel worksheet - preferable via OLEDB ?

Thanks in advance

Alter table will not work, AFAIK, however, you can Create Table or Select Into, which will allow you to create a new sheet. I cannot get this to run against an open sheet.

Dim cn As Object
Dim scn As String
Dim sSQL As String

strFile = "C:\Docs\test.xls"

scn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
strFile & ";Extended Properties=""Excel 8.0;HDR=Yes;"""

Set cn = CreateObject("ADODB.Connection")

cn.Open scn

''Note that there is no $ on the sheet to be created
sSQL = "SELECT *,'' As NewField INTO [Sheet17] FROM [Sheet4$]"

''Jet data types
sSQL = "CREATE TABLE [Sheet8] (AText text, ANother text)"

cn.Execute sSQL

If you run against an open file, you will get an error to the effect that Sheetn does not exist.

You can use Create Table instead of Alter Table. Just use your existing table name, then your columns adds to existing sheet

CREATE TABLE [ExistingSheet$] (ID char(255), oldField1 char(255), newField2 char(255))

it's work!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM