zckui
2018-04-04 01:30
采纳率: 100%
浏览 1.1k

c#中excel文件怎么转换为dbf文件

c#中excel文件怎么转换为dbf文件?wpf中datagrid中的数据怎么导出到dbf中?请求各位大神帮帮忙

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

3条回答 默认 最新

  • netlocks 2018-04-04 04:33
    最佳回答

    使用System.Collections.Generic;
    使用System.Text;
    使用System.Data;
    使用System.Data.OleDb;
    使用System.IO;

    命名空间TransDBFClass
    {
    public class TransDBFClass
    {

    ///
    ///将Access数据库转换为Excel表格
    /// </ summary>
    /// Access数据库所有文件路径</ param>

    公共无效AccessToExcel(字符串DbPath)

    {
    // OleDbConnection conn = new OleDbConnection(@“Provider = Microsoft.Jet.OLEDB.4.0; Data Source = D:\ project \ TransDBF \ db.mdb;”);
    OleDbConnection conn = new OleDbConnection(“Provider = Microsoft.Jet.OLEDB.4.0; Data Source =”+ DbPath +“;”);
    尝试
    {
    conn.Open();

    DataTable dt = conn.GetSchema(“Tables”); //获取ACCESS数据库中所有的表\查询\宏\窗体\模块

    字符串p =“c:\ dbf”;

    如果(!System.IO.Directory.Exists(p))
    {
    System.IO.Directory.CreateDirectory(p);
    }
    string [] vFiles = Directory.GetFiles(p);
    foreach(vFile中的字符串vFile)
    File.Delete(vFile);

    for(int i = 0; i <dt.Rows.Count; i ++)
    {
    try
    {
    string tablename =“”;
    if(dt.Rows [i] .ItemArray [3] .ToString()==“TABLE”)//判断是否是用户表
    {
    tablename = dt.Rows [i] .ItemArray [2] .ToString(); / /获取数据表名

    }
    //转换访问为dbf格式数据sql语句
    // String sql =“SELECT * INTO [dBASE 5.0; Database = C:\ dbf;]。” + tablename +“.dbf FROM”+ tablename;
    String sql =“SELECT * INTO [Excel 5.0; Database = C:\ dbf \ dbf.xls]”。+“[”+ tablename +“] FROM”+ tablename;
    OleDbCommand cmd = new OleDbCommand(sql,conn);

    cmd.ExecuteNonQuery();

    }
    赶上(异常前)
    {
    }
    }
    }
    赶上(异常前)
    {
    File.AppendAllText(@ “C:\的1.txt”,ex.ToString());
    }
    finally
    {
    conn.Close();
    }

    }

    ///
    ///将已经转换了的Excel表转换为Dbf格式方法
    /// </ summary>
    public void ExcelToDbf()
    {
    OleDbConnection conn = new OleDbConnection(@“Provider = Microsoft.Jet.OLEDB。 4.0; Data Source = c:\ dbf \ dbf.xls; Extended Properties = Excel 8.0;“);
    尝试
    {
    conn.Open();
    DataTable dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,new object [] {null,null,null,“TABLE”});

    for(int i = 0; i <dt.Rows.Count; i ++)
    {
    try
    {
    string tablename =“”;
    if(dt.Rows [i] .ItemArray [3] .ToString()==“TABLE”)//判断是否是用户表
    {

    tablename = dt.Rows [i] .ItemArray [2] .ToString(); //获取数据表名

    }
    //转换Excel为dbf格式数据sql语句
    String sql =“SELECT * INTO [dBASE 5.0; Database = C:\ dbf;]。DBF”+ i +“.dbf FROM”+ tablename;
    // String sql =“SELECT * INTO [Excel 5.0; Database = C:\ dbf \ dbf.xls]”。+“[”+ tablename +“] FROM”+ tablename;
    if(tablename.IndexOf('$')> = 0)
    {
    continue;
    }
    else
    {
    OleDbCommand cmd = new OleDbCommand(sql,conn);

    cmd.ExecuteNonQuery();
    }
    }
    catch(Exception ex)
    {
    }

    }

    }
    catch(Exception ex)
    {
    File.AppendAllText(@“c:\ 2.txt”,ex.ToString());
    }
    finally
    {
    conn.Close();
    }
    }

    }
    }

    还有一种带密码访问的

    使用系统;
    使用System.Collections.Generic;
    使用System.Text;
    使用System.Data;
    使用System.Data.OleDb;
    使用System.IO;

    命名空间TransDBFClass
    {
    public class TransDBFClass
    {

    ///
    ///将Access数据库转换为Excel表格
    /// </ summary>
    /// Access数据库所有文件路径</ param>

    公共无效AccessToExcel(字符串DbPath)

    {
    // OleDbConnection conn = new OleDbConnection(@“Provider = Microsoft.Jet.OLEDB.4.0; Data Source = D:\ project \ TransDBF \ db.mdb;”);
    // OleDbConnection conn = new OleDbConnection(“Provider = Microsoft.Jet.OLEDB.4.0; Data Source =”+ DbPath +“; Persist Security Info = False; Jet OLEDB:Database Password = sa;”);
    OleDbConnection conn = new OleDbConnection(“Provider = Microsoft.Jet.OLEDB.4.0; Data Source =”+ DbPath +“; Jet OLEDB:Database Password = sa;”);
    尝试
    {
    conn.Open();

    DataTable dt = conn.GetSchema(“Tables”); //获取ACCESS数据库中所有的表\查询\宏\窗体\模块

    字符串p =“d:\ dbf”;

    如果(!System.IO.Directory.Exists(p))
    {
    System.IO.Directory.CreateDirectory(p);
    }
    string [] vFiles = Directory.GetFiles(p);
    foreach(vFile中的字符串vFile)
    File.Delete(vFile);

    for(int i = 0; i <dt.Rows.Count; i ++)
    {
    try
    {
    string tablename =“”;
    if(dt.Rows [i] .ItemArray [3] .ToString()==“TABLE”)//判断是否是用户表
    {
    tablename = dt.Rows [i] .ItemArray [2] .ToString(); / /获取数据表名

    }
    //转换访问为dbf格式数据sql语句
    // String sql =“SELECT * INTO [dBASE 5.0; Database = C:\ dbf;]。” + tablename +“.dbf FROM”+ tablename;
    String sql =“SELECT * INTO [Excel 5.0; Database = d:\ dbf \ dbf.xls]”。+“[”+ tablename +“] FROM”+ tablename;
    OleDbCommand cmd = new OleDbCommand(sql,conn);

    cmd.ExecuteNonQuery();

    }
    赶上(异常前)
    {
    }
    }
    }
    赶上(异常前)
    {
    File.AppendAllText(@ “C:\的1.txt”,ex.ToString());
    }
    finally
    {
    conn.Close();
    }

    }

    ///
    ///将已经转换了的Excel表转换为Dbf格式方法
    /// </ summary>
    public void ExcelToDbf()
    {
    OleDbConnection conn = new OleDbConnection(@“Provider = Microsoft.Jet.OLEDB。 4.0; Data Source = d:\ dbf \ dbf.xls; Extended Properties = Excel 8.0;“);
    尝试
    {
    conn.Open();
    DataTable dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,new object [] {null,null,null,“TABLE”});

    for(int i = 0; i <dt.Rows.Count; i ++)
    {
    try
    {
    string tablename =“”;
    if(dt.Rows [i] .ItemArray [3] .ToString()==“TABLE”)//判断是否是用户表
    {

    tablename = dt.Rows [i] .ItemArray [2] .ToString(); //获取数据表名

    }
    //转换Excel为dbf格式数据sql语句
    String sql =“SELECT * INTO [dBASE 5.0; Database = d:\ dbf;]。DBF”+ i +“.dbf FROM”+ tablename;
    // String sql =“SELECT * INTO [Excel 5.0; Database = C:\ dbf \ dbf.xls]”。+“[”+ tablename +“] FROM”+ tablename;
    if(tablename.IndexOf('$')> = 0)
    {
    continue;
    }
    else
    {
    OleDbCommand cmd = new OleDbCommand(sql,conn);

    cmd.ExecuteNonQuery();
    }
    }
    catch(Exception ex)
    {
    }

    }

    }
    catch(Exception ex)
    {
    File.AppendAllText(@“c:\ 2.txt”,ex.ToString());
    }
    finally
    {
    conn.Close();
    }
    }

    }
    }

    评论
    解决 无用
    打赏 举报
查看更多回答(2条)

相关推荐 更多相似问题