关于C#中读写分隔符文件的说明

文本流方式读写

一、读取DataTable数据:

// 存储要读取的数据表
DataTable dataTable = new DataTable();
// 分隔符文件路径
string file = "C:/data.text";
// 分隔符类型
// delimited = "\t";
// delimited = ",";
// delimited = " ";
string delimited = ",";
// 打开分隔符文件
StreamReader streamReader = new StreamReader(file, Encoding.Default);
// 读取表格列名
string line = streamReader.ReadLine();
string[] cols = line.Split(new string[] { delimited }, StringSplitOptions.None);
foreach (string col in cols)
{
    dataTable.Columns.Add(col);
}
// 读取表格行数据
while (!streamReader.EndOfStream)
{
    line = streamReader.ReadLine();
    string[] row = line.Split(new string[] { delimited }, StringSplitOptions.None);
    if (row.Length == cols.Length)
    {
        List<object> list = new List<object>();
        foreach (string item in row)
        {
            list.Add(item);
        }
        dataTable.Rows.Add(list.ToArray());
    }
}
// 关闭分隔符文件
streamReader.Close();
return dataTable;

二、写入DataTable数据:

// 要写入的数据表
DataTable dataTable = new DataTable();
// 分隔符文件保存路径
string file = "C:/data.text";
// 分隔符类型
// delimited = "\t";
// delimited = ",";
// delimited = " ";
string delimited = ",";
// 创建分隔符文件
StreamWriter streamWriter = new StreamWriter(file, false, Encoding.Default);
List<string> buffer = new List<string>();
// 写入表格列名
foreach (DataColumn dataColumn in dataTable.Columns)
{
    buffer.Add(dataColumn.ColumnName);
}
streamWriter.Write(string.Join(delimited, buffer));
// 写入表格行数据
foreach (DataRow dataRow in dataTable.Rows)
{
    buffer.Clear();
    streamWriter.WriteLine();
    foreach (DataColumn dataColumn in dataTable.Columns)
    {
        object value = dataRow[dataColumn];
        buffer.Add(value == null ? "" : value.ToString());
    }
    streamWriter.Write(string.Join(delimited, buffer));
}
// 关闭分隔符文件
streamWriter.Flush();
streamWriter.Close();

ADO.NET方式读写

读取DataTable数据:

// 存储要读取的数据表
DataTable dataTable = new DataTable();
// 分隔符文件路径
string file = "C:/data.text";
// 获取架构文件路径
string directory = Path.GetDirectoryName(file);
string schema = Path.Combine(directory, "schema.ini");
// 生成架构文件
StreamWriter streamWriter = new StreamWriter(schema, false, Encoding.Default);
// 分隔符文件名称
string fileName = Path.GetFileName(file);
streamWriter.WriteLine("[" + fileName + "]");
// 首行是否为列名
streamWriter.WriteLine("ColNameHeader=True");
// 分隔符类型
// 字段以Tab字符分割:TabDelimited
// 字段以逗号字符分割:CSVDelimited
// 除双引号外的其他分隔符(一个字符长度):Delimited(自定义分隔符)
// 字段为固定长度:FixedLength
string delimited = "CSVDelimited";
streamWriter.WriteLine("Format=" + delimited);
// 系统扫描多少行数据来决定字段类型
streamWriter.WriteLine("MaxScanRows=88");
// 字符集类型:OEM或ANSI
streamWriter.WriteLine("CharacterSet=OEM");
streamWriter.Close();
// OldDb链接字符串
string format = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Text;HDR=Yes;FMT={1};IMEX=1';Persist Security Info=False";
// 打开OldDb链接
OleDbConnection connection = new OleDbConnection(string.Format(format, directory, delimited));
connection.Open();
// 查询分隔符文件数据
OleDbCommand command = new OleDbCommand("select * from [" + fileName + "]", connection);
// 读取数据到数据表对象
OleDbDataAdapter adapter = new OleDbDataAdapter(command);
adapter.Fill(dataTable);
adapter = null;
// 关闭OldDb链接
connection.Close();
return dataTable;

更多信息请参阅:Schema.ini File (Text File Driver)

发表评论