Create .DBF in C# code that is readable from Excel (VFP or not)

穿精又带淫゛_ 提交于 2019-12-13 16:20:49

问题


LANGUAGE: C#, System: Windows7, Excel 2007

I want to create a .DBF from some data, and i want to open it from Excel 2007. It can be either dBase or foxpro. I am currently doing in FoxPro9 (btw this code is from the internet):

 OleDbConnection con = new OleDbConnection("Provider=VFPOLEDB.1;Data Source=C:\\Temp\\;");
            con.Open();
            OleDbCommand cmd1 = new OleDbCommand("Create Table TestDBF (Field1 I, Field2 C(10))", con);
            OleDbCommand cmd2 = new OleDbCommand("Insert Into TestDBF Values (1, 'Hello')", con);
            OleDbCommand cmd3 = new OleDbCommand("Insert Into TestDBF Values (2, 'World')", con);
            cmd1.ExecuteNonQuery();
            cmd2.ExecuteNonQuery();
            cmd3.ExecuteNonQuery();
            con.Close();

This generates the file, which i can open in DbfViewer, however, i cant open it in Excel 2007 nor use in some applications i have. I can manually convert the FoxPro .dbf to a dbaseIII dbf using the DbfViwer, but i want it to be automatic.

Any ideas?

Thanks in advance


回答1:


Here's a little console app that will create a DBF using the CREATE TABLE SQL syntax and then copy it to a previous version of the FoxPro DBF file format that can be opened in Excel.

static void Main(string[] args)
{
    Console.WriteLine("Starting program execution...");

    string connectionString = @"Provider=VFPOLEDB.1;Data Source=C:\YourDirectory\";

    using (OleDbConnection connection = new OleDbConnection(connectionString))
    {
        using (OleDbCommand scriptCommand = connection.CreateCommand())
        {
            connection.Open();

            string vfpScript = @"Create Table TestDBF (Field1 I, Field2 C(10))
                                USE TestDBF
                                COPY TO OpensWithExcel TYPE Fox2x
                                USE";

            scriptCommand.CommandType = CommandType.StoredProcedure;
            scriptCommand.CommandText = "ExecScript";
            scriptCommand.Parameters.Add("myScript", OleDbType.Char).Value = vfpScript;
            scriptCommand.ExecuteNonQuery();
        }
    }

    Console.WriteLine("End program execution...");
    Console.WriteLine("Press any key to continue");
    Console.ReadLine();
}

}




回答2:


From VFP directly, its easy...

use YourTable
Copy To SomeFile type XLS



回答3:


        OleDbConnection con = new OleDbConnection("Provider=VFPOLEDB.1;Data Source=C:\\Temp\\;");
        con.Open();
        OleDbCommand cmd1 = new OleDbCommand("Create Table TestDBF (Field1 I, Field2 C(10))", con);
        OleDbCommand cmd2 = new OleDbCommand("Insert Into TestDBF Values (1, 'Hello')", con);
        OleDbCommand cmd3 = new OleDbCommand("Insert Into TestDBF Values (2, 'World')", con);


来源:https://stackoverflow.com/questions/3563584/create-dbf-in-c-sharp-code-that-is-readable-from-excel-vfp-or-not

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!