日期:2014-05-17  浏览次数:20862 次

从txt读入string中文错误
private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                string strConn;
                strConn = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source = c:\Users\Administrator\Desktop\cstest.xlsx;Extended Properties='Excel 8.0;HDR=no;IMEX=1'";
                OleDbConnection OleConn = new OleDbConnection(strConn);
                OleConn.Open();
                String sql = "SELECT * FROM  [Sheet1$]";
                OleDbDataAdapter OleDaExcel = new OleDbDataAdapter(sql, OleConn);
                DataSet OleDsExcle = new DataSet();
                OleDaExcel.Fill(OleDsExcle, "Sheet1");
                DataTable dt = OleDsExcle.Tables[0];

                string fn = @"C:\Users\Administrator\Desktop\txt.txt";
                StreamReader sr = new StreamReader(fn, Encoding.GetEncoding("gb2312"));
                string s = sr.ReadToEnd();
                sr.Dispose();
                StreamWriter sw = new StreamWriter(fn);

                int w;
                int i;
                string t;
                for (i = 0; i <= 6; i++)
                {

                    t = dt.Rows[i][3].ToString();
                    w = s.IndexOf(t);
                    s.Remove(w, t.Length);

                }