views:

112

answers:

3

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

A: 

From VFP directly, its easy...

use YourTable
Copy To SomeFile type XLS
DRapp
What do you mean from VFP directly? I don't understand your code. I'm know very little of VFP.
WoF_Angel
type XLS doesn't create a DBF
Stuart Dunkeld
Yeah, no kidding... but if he wanted the results in Excel, its easy to dump FROM a DBF into something Excel can easily read. If you really want to take it back a bit, you can create and ODBC connection to a free table or database, then Excel can open via that connection an query the data. His final result was to get access to via Excel, and due to Microsoft's wisdom and removing such access, you have to go through hoops.
DRapp
@WoF_Angel, from your comment that you are "...currently doing in FoxPro9...", that implied to me that you have VFP9 development environment to work with. If you get to the command window and type the two commands it will USE (open) a file (wherever your .DBF is). Then Copy to (some other file name) type XLS tells VFP to copy all the records into a format directly readable by Excel.
DRapp
Well, i can use a .xls file, although i prefer a DBF DBASE III one. Is there no way to convert a VFP 9.0 DBF file to DBASE III (through code)?Anyway, i'm looking into your suggestion.
WoF_Angel
A: 
        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);
Ankit Moradiya
+2  A: 

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();
}

}

DaveB
Excellent, thanks man
WoF_Angel