将 C# 数据集导出到文本文件

Export a C# DataSet to a text file(将 C# 数据集导出到文本文件)
本文介绍了将 C# 数据集导出到文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

网上有很多关于如何从文本文件中填充数据集的示例,但我想做相反的事情.我唯一能找到的是 this 但看起来……不完整?

There are a lot of examples online of how to fill a DataSet from a text file but I want to do the reverse. The only thing I've been able to find is this but it seems... incomplete?

我希望它采用可读的格式,而不仅仅是逗号分隔,因此如果有意义的话,每行的列之间的间距不相等.这是我的意思的一个例子:

I want it to be in a readable format, not just comma delimited, so non-equal spacing between columns on each row if that makes sense. Here is an example of what I mean:

Column1          Column2          Column3
Some info        Some more info   Even more info
Some stuff here  Some more stuff  Even more stuff
Bits             and              bobs

注意:我的 DataSet 中只有一个 DataTable,因此无需担心多个 DataTable.

Note: I only have one DataTable within my DataSet so no need to worry about multiple DataTables.

当我说可读"时,我的意思是人类可读.

When I said "readable" I meant human-readable.

提前致谢.

推荐答案

这应该很好地隔开固定长度的字体文本,但这确实意味着它将处理完整的 DataTable 两次(第 1 遍:查找每列最长的文本,第 2 遍:输出文本):

This should space out fixed length font text nicely, but it does mean it will process the full DataTable twice (pass 1: find longest text per column, pass 2: output text):

    static void Write(DataTable dt, string outputFilePath)
    {
        int[] maxLengths = new int[dt.Columns.Count];

        for (int i = 0; i < dt.Columns.Count; i++)
        {
            maxLengths[i] = dt.Columns[i].ColumnName.Length;

            foreach (DataRow row in dt.Rows)
            {
                if (!row.IsNull(i))
                {
                    int length = row[i].ToString().Length;

                    if (length > maxLengths[i])
                    {
                        maxLengths[i] = length;
                    }
                }
            }
        }

        using (StreamWriter sw = new StreamWriter(outputFilePath, false))
        {
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                sw.Write(dt.Columns[i].ColumnName.PadRight(maxLengths[i] + 2));
            }

            sw.WriteLine();

            foreach (DataRow row in dt.Rows)
            {
                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    if (!row.IsNull(i))
                    {
                        sw.Write(row[i].ToString().PadRight(maxLengths[i] + 2));
                    }
                    else
                    {
                        sw.Write(new string(' ', maxLengths[i] + 2));
                    }
                }

                sw.WriteLine();
            }

            sw.Close();
        }
    }

这篇关于将 C# 数据集导出到文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)