将列表写入Xlsx文件时出错 [英] Error While Writing A List Into An Xlsx File

查看:66
本文介绍了将列表写入Xlsx文件时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我有一些代码,我需要写一个列表<>整数到xlsx文件中.但是出现错误.
这是代码.

OK, I have some code and I need to write a list<> of integers onto an xlsx file. But am getting an error.
Here is the code.

string fileName = @"C:\rhandx.xlsx";
 string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
          fileName + ";Extended Properties=\"Excel 8.0;HDR=NO;\"";
OleDbConnection con = new System.Data.OleDb.OleDbConnection(connectionString);
con.Open();
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = con;
foreach (int coord in rhandlist)
{
    cmd.CommandText = ("INSERT INTO TABLE [SHEET1$] VALUES (coord)");
    cmd.ExecuteNonQuery();
}
con.Close();



rhandlist是一个未知整数的列表.但是我收到一条错误消息,说我的插入语句的语法错误.



rhandlist is a list with unknown no of int''s. But I get an error saying my insert statement''s syntax is wrong.

推荐答案

VALUES(coord)"); cmd.ExecuteNonQuery(); } con.Close();
VALUES (coord)"); cmd.ExecuteNonQuery(); } con.Close();



rhandlist是一个未知整数的列表.但是我收到一条错误消息,指出我的插入语句的语法是错误的.



rhandlist is a list with unknown no of int''s. But I get an error saying my insert statement''s syntax is wrong.


正如Richcb指出的那样,您需要从SQL语句中删除"TABLE".它应该看起来像这样:
As Richcb pointed out, you need to remove the "TABLE" from your SQL statement. It should look like this:
cmd.CommandText = ("INSERT INTO [SHEET1


VALUES(coord)");
VALUES (coord)");


确保您保持正确的间距.


Make sure you keep the spacing right.


这篇关于将列表写入Xlsx文件时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆