使用Oledb导出到没有标题c#的excel文件中

Dim*_*nev 4 c# oledb oledbconnection oledbcommand

我正在使用OleDB,我想将我的对象导出到excel表中.工作表中的每一行都是我的一个对象.问题是,当工作表中没有列标题时,我不知道如何插入数据.

这个:

commandString = "Insert into [Sheet1$] values('test1', 'test2')"
Run Code Online (Sandbox Code Playgroud)

抛出此异常:

Number of query values and destination fields are not the same.
Run Code Online (Sandbox Code Playgroud)

我的连接字符串是:

"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+filename+";Extended Properties='Excel 8.0;HDR=No'"
Run Code Online (Sandbox Code Playgroud)

Ste*_*eve 8

如果连接字符串包含HDR = NO,则Jet OLE DB提供程序会自动为您指定字段(F1表示第一个字段,F2表示第二个字段,依此类推).我将尝试以这种方式更改您的查询

commandString = "Insert into [Sheet1$] (F1, F2) values('test1', 'test2')" 
Run Code Online (Sandbox Code Playgroud)

这只有在您创建了excel文件并在Sheet1的第一行的前两个单元格中插入了某些内容后才有效