求救 如何将EXCEL另存为dbf (Dbase)文件? 本帖最后由 xiongzhiqiang123 于 2010-01-13 09:06:42 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 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 我比较懒,就不给你把代码写出来了,我把方法告诉你,你先打开EXCEL,然后使用录制宏,然后点另存,然后点停止录制宏。这时候EXCEL里就有一个宏了,你可以打开查看里面的代码,那里面的代码就是实现“另存为DBF”的代码。把他转换成C#的就可以了~~呵呵~!希望对您有帮助 同一个解决方案内,引用程序集失败?? C#调用oracle存储过程问题 关于socket的一个小疑问 在C#中可否用循环的方法写出算24点的程序 为什么在窗体类中 带数据库 访问 的 方法 不能连续执行? ASP.NET中如何让客户发图片到数据库? 【100分求解】如何在 asp.net 中将客户端连接过来的请求,创建新的request 到其他服务器? 打包安装后,运行时,C#程序出错,大家看看是什么原因呀??急急!!!!!!! 如何判断两个对象的类型一致 在VS2013中新建SQL Server项目,如何使用? C# 获取MPG文件信息 求一探索字典匹配方法(分词)
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