我的程序每次会得出三个数据,我想把这三个数据添加到excel文件的一行,
下一次运行程序时,还要向原来的excel文件写入,需要换行,以前的数据不能被覆盖。
向高手们求教,我是个新手,只要满足我上面的要求就可以,谢谢,希望能讲解的清楚些

解决方案 »

  1.   

    private void DSToExecl(string s1,string s2,string s3)
        {
            string strFile = "";
            string path = "";
            strFile = strFile + "GCOutReport_";
            strFile = strFile + DateTime.Now.ToString("yyyyMMddHHmm");
            strFile = strFile + ".xls";
            string fileName = strFile;
            path = Server.MapPath(strFile);
            //path = "D:\\" + strFile;
            System.IO.FileStream fs = new FileStream(path, System.IO.FileMode.Create, System.IO.FileAccess.Write);
            StreamWriter sw = new StreamWriter(fs, new System.Text.UnicodeEncoding());
            sw.Write("表頭1");
            sw.Write("\t");
            sw.Write("表頭2");
            sw.Write("\t");
            sw.Write("表頭3");
            sw.Write("\t");
                   sw.WriteLine("");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                sw.Write(s1);
                sw.Write("\t");
                sw.Write(s2);
                sw.Write("\t");
                sw.Write(s3);
                sw.Write("\t");
                sw.WriteLine("");
            }
            sw.Flush();
            sw.Close();
        }
      

  2.   

    首先在Excel文件的Sheet1里的第一行第一列输入“数据1”,第二列输入"数据2",第三列输入"数据3”。存盘退出。try
    {   
    string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Excel文件名 + ";" + "Extended Properties=Excel 8.0;";
            OleDbConnection conn = new OleDbConnection(strConn);
            conn.Open();
           System.Data.OleDb.OleDbCommand cmd = new OleDbCommand();
          cmd.Connection = conn;     cmd.CommandText ="insert into [sheet1$](数据1,数据2,数据3) values(实际数据)";
         cmd.ExecuteNonQuery();
    }catch(Exception ex)
    {
       MessageBox.Show(string.Format("写入Excel出错:{0}",ex.Message));      
    }finally
    {
       conn.Close();
    }