witchqueen
witchqueen

Reputation: 73

Excel from string data to integer

I add data from a text to excel with insert into, but it's always show string format. I want it float format(0.2, 1, 20) and I cant change them in excel (for example a coloumn in one time, only it could be changed by one by for all cells). I tried tryparse or converttoint32 func. but nothing change in excel, the numbers are still in text format..

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);
        }



     }

Upvotes: 0

Views: 611

Answers (1)

Damith
Damith

Reputation: 63065

since you adding '' for each parameter all will take as strings

use Parameters as below

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();
}

Upvotes: 1

Related Questions