English 中文(简体)
Batch换算成直观的正统 d,以 c
原标题:Batch convert visual foxpro dbf tables to csv
最佳回答
问题回答

将福克斯福克斯福克斯公司档案清单列入一个阵列/名单,然后将ConvertDbf各卷从福克斯福克斯福克斯·福克斯改为茨夫。 参见下文的c# console 应用法。

页: 1 用于数据交换功能的数据表。

using System;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.Linq;
using System.Text;

namespace SO8843066
{
    class Program
    {
        static void Main(string[] args)
        {
            string connectionString = @"Provider=VFPOLEDB.1;Data Source=C:";
            string dbfToConvert = @"C:yourdbffile.dbf";
            ConvertDbf(connectionString, dbfToConvert, dbfToConvert.Replace(".dbf", ".csv"));

            Console.WriteLine("End of program execution");
            Console.WriteLine("Press any key to end");
            Console.ReadKey();
        }

        static void DataTableToCSV(DataTable dt, string csvFile)
        {
            StringBuilder sb = new StringBuilder(); 
            var columnNames = dt.Columns.Cast<DataColumn>().Select(column => column.ColumnName).ToArray(); 
            sb.AppendLine(string.Join(",", columnNames)); 
            foreach (DataRow row in dt.Rows) 
            { 
                var fields = row.ItemArray.Select(field => field.ToString()).ToArray(); 
                for (int i =0;i < fields.Length;i++)
                {
                    sb.Append(""" + fields[i].Trim() );
                    sb.Append((i != fields.Length - 1) ? ""," : """);
                }
                sb.Append("
");
            } 
            File.WriteAllText(csvFile, sb.ToString());
        }

        static void ConvertDbf(string connectionString, string dbfFile, string csvFile)
        {
            string sqlSelect = string.Format("SELECT * FROM {0}", dbfFile);
            using (OleDbConnection connection = new OleDbConnection(connectionString))
            {
                using (OleDbDataAdapter da = new OleDbDataAdapter(sqlSelect, connection))
                {
                    DataSet ds = new DataSet();
                    da.Fill(ds);
                    DataTableToCSV(ds.Tables[0], csvFile);
                }
            }
        }
    }
}

This works very well and thanks for the solution. I used this to convert some visual foxpro dbf tables to flat files. With these tables, there is the additional challenge of converting fields of type Currency. Currency fields are a 64-bit (8 byte) signed integer amidst a 36 element byte array starting at the 27th position. The integer is then divided by 1000 to get 4-decimal precision equivalent.

如果你有这样的领域,就在田里尝试这样做,以便进行住宿。

if (("" + fields[i]).Equals("System.Byte[]"))
{
    StringBuilder db = new StringBuilder();
    byte[] inbytes = new byte[36];
    inbytes = ObjectToByteArray(fields[i]);
    db.Append("" + (double)BitConverter.ToInt64(inbytes,27)/1E4);
    sb.Append(""" + db);
}

采用以下帮助方法

private static byte[] ObjectToByteArray(Object obj)
    {
        BinaryFormatter bf = new BinaryFormatter();
        using (var ms = new MemoryStream())
        {
            bf.Serialize(ms, obj);
            return ms.ToArray();
        }
    }




相关问题
Styling rows in table that uses CSV data through PHP

I ve been working on this simple code that puts a CSV file into a nice table. But because the data is imported, styling ODD rows is a pretty hard thing to do. All I need would be a method to address ...

PHP - Sanitise a comma separated string

What would be the most efficient way to clean a user input that is a comma separated string made entirely on numbers - e.g 2,40,23,11,55 I use this function on a lot of my inputs function clean($...

marking duplicates in a csv file

I m stumped with a problem illustrated in the sample below: "ID","NAME","PHONE","REF","DISCARD" 1,"JOHN",12345,, 2,"PETER",6232,, 3,"JON",12345,, 4,"PETERSON",6232,, 5,"ALEX",7854,, 6,"JON",12345,, ...

Interactive heat map in Flex

I’m at a very basic level with Flex and with programming in general. I am working on a project where I have data in an Excel (.csv) format, it’s a large Excel plot/matrix where each cell has a ...

热门标签