本帖最后由 xiongzhiqiang123 于 2010-01-13 09:06:42 编辑

解决方案 »

  1.   


    public static string path = @"TempExcel\STemp.xls";   
           public static string path2 = "TestUser.xls";   
           public static string PreFilePath = @"C:\Excel\";   
           public static void DataSetToLocalExcel(DataSet ds, string srcPath, string outputPath, bool deleteOldFile)   
           {   
               if (ds == null || ds.Tables[0] == null && ds.Tables[0].Rows.Count == 0) { return; }   
               if (deleteOldFile)   
               {   
                   if (System.IO.File.Exists(outputPath)) { System.IO.File.Delete(outputPath); }   
               }   
               System.IO.File.Copy(srcPath, outputPath, true);   
               ExcelReader exr = new ExcelReader();   
               exr.ExcelFilename = outputPath;   
               exr.Headers = true;   
               exr.MixedData = true;   
               exr.KeepConnectionOpen = true;   
               string[] sheetnames = exr.GetExcelSheetNames();   
               exr.SheetName = sheetnames[0];   
               DataTable dt = exr.GetTable();   
               if (dt == null) return;   
               exr.SetPrimaryKey(0);   
               //dt.PrimaryKey = new DataColumn[] { dt.Columns["编号"] };   
               DataTable dt2 = ds.Tables[0].Copy();   
               dt.Rows.Clear();   
               for (int i = 0; i < dt2.Rows.Count; i++)   
               { // Copy the values to the object array   
                   DataRow dr = dt.NewRow();   
                   for (int col = 0; col < dt.Columns.Count; col++)   
                   {   
                       dr[col] = dt2.Rows[i][col];   
                   }   
                   dt.Rows.Add(dr);   
               }   
               exr.SetTable(dt);   
               WriteFile#region WriteFile  
                
               #endregion   
               exr.Close();   
               exr.Dispose();   
               exr = null;   
           }   
           private DataSet Get_AllPrices()   
           {   
               try  
               {   
                   // Get the employee details   
                   string strSql = "SELECT [CustomID] as 编号,[C_Name] as 品名,0 as 最高价格,0 as 最低价格,0 as 平均价格,'元/公斤' as 计量单位,'' as 备注  FROM [PriceCategory] WHERE ( 1=1 AND ([Puser] = 'tuser') )";   
                   SqlConnection objConn = new SqlConnection(@"Data Source=AP6;Initial Catalog=testdb2009 ;Persist Security Info=True;User ID=sa;Password=sa");   
                   SqlDataAdapter daEmp = new SqlDataAdapter(strSql, objConn);   
                   daEmp.Fill(dsPrice, "price");   
                   return dsPrice;   
               }   
               catch (Exception Ex)   
               {   
                   throw Ex;   
               }   
           }   
           DataSet dsPrice = new DataSet();   
           protected void btnGetData_Click(object sender, EventArgs e)   
           {   
               DataSetToLocalExcel(Get_AllPrices(), PreFilePath + path, PreFilePath + path2, true);   
           }  
    本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/downmoon/archive/2009/01/16/3796652.aspx
      

  2.   

    我比较懒,就不给你把代码写出来了,我把方法告诉你,你先打开EXCEL,然后使用录制宏,然后点另存,然后点停止录制宏。这时候EXCEL里就有一个宏了,你可以打开查看里面的代码,那里面的代码就是实现“另存为DBF”的代码。把他转换成C#的就可以了~~呵呵~!希望对您有帮助