使用可从Excel读取的C#代码创建.DBF(是否可以使用VFP) [英] Create .DBF in C# code that is readable from Excel (VFP or not)

查看:137
本文介绍了使用可从Excel读取的C#代码创建.DBF(是否可以使用VFP)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

语言:C#,系统:Windows7,Excel 2007

LANGUAGE: C#, System: Windows7, Excel 2007

我想从一些数据中创建一个.DBF,我想从Excel 2007中打开它。可以是dBase或foxpro。我目前正在FoxPro9中进行操作(此代码来自互联网):

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

这将生成文件,可以在DbfViewer中打开该文件,但是无法在Excel 2007中打开它也没有在我拥有的某些应用程序中使用。我可以使用DbfViwer手动将FoxPro .dbf转换为dbaseIII dbf,但我希望它是自动的。

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.

有什么想法吗?

预先感谢

推荐答案

下面是一个小型控制台应用程序,它将使用CREATE TABLE SQL创建DBF语法,然后将其复制到可以在Excel中打开的FoxPro DBF文件格式的早期版本。

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

}

这篇关于使用可从Excel读取的C#代码创建.DBF(是否可以使用VFP)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆