Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Excel从字符串数据到整数_C#_String_Excel - Fatal编程技术网

C# Excel从字符串数据到整数

C# Excel从字符串数据到整数,c#,string,excel,C#,String,Excel,我使用insert into将文本中的数据添加到excel中,但它始终是显示字符串格式。我希望它是浮动格式(0.2,1,20),我不能在excel中更改它们(例如一次更改一个列,只能对所有单元格逐个更改)。我试过tryparse或converttoint32 func。但是excel中没有任何变化,数字仍然是文本格式 public void ExcelWrite(string date, string station_name, string station_no, string xvaluee

我使用insert into将文本中的数据添加到excel中,但它始终是显示字符串格式。我希望它是浮动格式(0.2,1,20),我不能在excel中更改它们(例如一次更改一个列,只能对所有单元格逐个更改)。我试过tryparse或converttoint32 func。但是excel中没有任何变化,数字仍然是文本格式

public void ExcelWrite(string date, string station_name, string station_no, string xvaluee) 

{

        try
        {   float j;
            xvaluee=xvaluee.Trim();
            float.TryParse(Valuee, out j);
            string szConn = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='C://data.xls';Extended Properties='Excel 8.0;HDR=YES'";
            OleDbConnection conn = new OleDbConnection(szConn);

            conn.Open();
            OleDbCommand cmd = new OleDbCommand("INSERT INTO [Sayfa1$]([Station_No],[Station_Name],[Date],[Valuee]) VALUES('" + station_no + "','" + station_name + "','" + date + "','" + j  + "')", conn);
            cmd.ExecuteNonQuery();


            conn.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }



     }
由于您为每个参数添加了
'
,所有参数都将作为
字符串

使用以下参数

using(OleDbConnection cn = new OleDbConnection(szConn ))
{
    cn.Open();
    OleDbCommand cmd1 = new OleDbCommand("INSERT INTO [Sayfa1$]([Station_No],[Station_Name],[Date],[Valuee]) VALUES(?,?,?,?)", cn);
   cmd1.Parameters.AddWithValue("@p1", station_no );
   cmd1.Parameters.AddWithValue("@p2", station_name );
   cmd1.Parameters.AddWithValue("@p3",date );
   cmd1.Parameters.AddWithValue("@p4", j);
   cmd1.ExecuteNonQuery();
}

非常感谢,我还将mixformat的IMEX值更改为1。问题解决了。