0
我正在使用oledb和c#将数据写入excel。 我面临一个问题,我打算存储在Excel文件中的一些数据是数字(Int变量),但是在我尝试将其插入到Excel中后,它变成了文本。另外,为了确保数据是Int,我使用Convert.ToInt32方法将其转换。c# - excel - 如何将数字变量存储为数字字段
当数据是最终数据时,这意味着我在代码中编写了特定的数字,它保留在Excel中的数字,但其他数据变成文本。
结果对象(代码)是一本字典(字符串,对象)
public void insertIntoDb()
{
string connectionString = String.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source={0};Extended Properties='Excel 12.0;HDR=YES;IMEX=0'", Globals.path);
OleDbConnection dbConnection = new OleDbConnection(connectionString);
String query = "INSERT INTO [SHEET1$] ([Id],[Name],[Email],[Phone],[Complete Date],[Destination],[Passengers],[Depart],[End],[Nights],[Price]) VALUES (@value1,@value2,@value3,@value4,@value5,@value6,@value7,@value8,@value9,@value10,@value11)";
dbConnection.Open();
OleDbCommand cmd = new OleDbCommand(query, dbConnection);
cmd.Parameters.AddWithValue("@value1", (this.getLastId()+1));
cmd.Parameters.AddWithValue("@value2", this.results["Name"]);
//TODO: Add email to MsgToText
cmd.Parameters.AddWithValue("@value3", this.results["Email"]);
cmd.Parameters.AddWithValue("@value4", this.results["Phone"]);
cmd.Parameters.AddWithValue("@value5", this.results["Date"]);
cmd.Parameters.AddWithValue("@value6", Convert.ToInt32(this.results["Destination"]));
cmd.Parameters.AddWithValue("@value7", Convert.ToInt32(this.results["Passengers"]));
cmd.Parameters.AddWithValue("@value8", this.results["Departure Date"]);
cmd.Parameters.AddWithValue("@value9", this.results["End Date"]);
cmd.Parameters.AddWithValue("@value10", Convert.ToInt32(this.results["Nights"]));
cmd.Parameters.AddWithValue("@value11", Convert.ToInt32(this.results["Price"]));
if(cmd.ExecuteNonQuery() > 0)
{
MessageBox.Show("Added to DB");
dbConnection.Close();
}
}
嗨,我的问题代码是写在Excel中的代码,当我尝试定义特定行的字段的类型时,它只是跳过行 – ronfl