0

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



     }

1 Answer 1

1

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();
}
Sign up to request clarification or add additional context in comments.

1 Comment

thanks a lot, I also change IMEX value to 1 for mixformat. the problem is solved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.