C# 不使用.linq将筛选结果输出为excel可读格式

C# 不使用.linq将筛选结果输出为excel可读格式,c#,list,export,C#,List,Export,我有一个.dat文件,其中包含大量多余的数据(几千行中,我只需要几百行)。我目前有一段代码,可以读取和过滤它。以前,我使用此工具将摘要数据输出到控制台(当前代码就是这样做的),但是现在我需要能够将相关的每一行输出到excel可读文件中。包含的数据为ASCII格式 如果可能的话,我会尽量避免使用linq,因为我并不真正理解它,我只是想应付不使用它所带来的困难 研究表明,最有效的方法似乎是让C#读取文件,将所有内容作为列表存储到内存中,然后将整个列表写入excel。我很乐意手动完成其余的工作,一旦这

我有一个.dat文件,其中包含大量多余的数据(几千行中,我只需要几百行)。我目前有一段代码,可以读取和过滤它。以前,我使用此工具将摘要数据输出到控制台(当前代码就是这样做的),但是现在我需要能够将相关的每一行输出到excel可读文件中。包含的数据为ASCII格式

如果可能的话,我会尽量避免使用linq,因为我并不真正理解它,我只是想应付不使用它所带来的困难

研究表明,最有效的方法似乎是让C#读取文件,将所有内容作为列表存储到内存中,然后将整个列表写入excel。我很乐意手动完成其余的工作,一旦这个列表在excel中,它只是从c#中获取列表并将其转储到excel列中。考虑到所有的数据都有固定的长度,我对如何实现这一点没有太大的限制,但如果可能的话,我更希望将每个变量写入一个新的列,而不是将其全部放在列a中

当前代码:

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using NsExcel = Microsoft.Office.Interop.Excel;


class Program
{
    static void Main(string[] args)
    {
        List<string> myList = new List<string>();

        int i = 1;
        using (StreamReader reader = new StreamReader("file.dat"))
        {
            string line;
            var locations = new Dictionary<string, int[]>() {
                {"210", new [] {405, 4, 128, 12, 141, 12, 247, 15, 121, 3}},
                {"310", new [] {321, 4, 112, 12, 125, 12, 230, 15, 105, 3}}, 
                {"410", new [] {477, 4, 112, 12, 125, 12, 360, 15, 105, 3}} 
            };

            while ((line = reader.ReadLine()) != null)
            {


                var lineStart = line.Substring(0, 3);

                if (lineStart == "210" || lineStart == "310" || lineStart == "410")
                {
                    var currentLocations = locations[lineStart];
                    var letters = line.Substring(currentLocations[0], currentLocations[1]);

                    var tvolume =
                        int.Parse(line.Substring(currentLocations[2], currentLocations[3])) +
                        int.Parse(line.Substring(currentLocations[4], currentLocations[5]));

                    var tprice = long.Parse(line.Substring(currentLocations[6], currentLocations[7]));
                    var mvolume = tprice * tvolume * 0.01 * 0.0000001;
                    var currency = line.Substring(currentLocations[8], currentLocations[9]);


                    // double total = myList.

                    Console.WriteLine(i);
                    Console.WriteLine(lineStart);
                    Console.WriteLine(letters);
                    Console.WriteLine(tvolume * 0.01);
                    Console.WriteLine(tprice * 0.0000001);
                    Console.WriteLine("{0:N}", mvolume);
                    Console.WriteLine(currency + "\n");
                    i = i + 1;
                }

            }

            //Dictionary<string, int> tvolumeDictionary = new Dictionary<string, int>();
            //Dictionary<string, double> mvolumeDictionary = new Dictionary<string, double>();



            }
        }
    }
使用系统;
使用System.Collections.Generic;
使用System.IO;
使用系统文本;
使用System.Text.RegularExpressions;
使用NsExcel=Microsoft.Office.Interop.Excel;
班级计划
{
静态void Main(字符串[]参数)
{
List myList=新列表();
int i=1;
使用(StreamReader=newstreamreader(“file.dat”))
{
弦线;
var locations=newdictionary(){
{“210”,新[{405,4,128,12,141,12,247,15,121,3},
{“310”,新[{321,4112,1215,1230,15105,3},
{“410”,新[{477、4112、12512、12360、15105、3}}
};
而((line=reader.ReadLine())!=null)
{
var lineStart=line.Substring(0,3);
如果(lineStart==“210”| | lineStart==“310”| | lineStart==“410”)
{
var currentLocations=位置[lineStart];
变量字母=行。子字符串(currentLocations[0],currentLocations[1]);
变量体积=
int.Parse(line.Substring(currentLocations[2],currentLocations[3]))+
int.Parse(line.Substring(currentLocations[4],currentLocations[5]);
var tprice=long.Parse(line.Substring(currentLocations[6],currentLocations[7]);
var mvolume=tprice*tvolume*0.01*0.0000001;
var currency=line.Substring(currentLocations[8],currentLocations[9]);
//双倍总计=我的列表。
控制台写入线(i);
控制台写入线(lineStart);
控制台。书写线(字母);
控制台写入线(tvolume*0.01);
控制台写入线(tprice*0.0000001);
Console.WriteLine(“{0:N}”,mvolume);
Console.WriteLine(货币+“\n”);
i=i+1;
}
}
//Dictionary tvolumeDictionary=新字典();
//Dictionary mvolumeDictionary=新字典();
}
}
}

您可以使用CSV格式,这将比编写xls或xlsx文件(Microsoft Excel格式)容易得多。 您可以手动写入csv文件或使用任何现成的csv.net库,例如:


如果您只有一个数据文件,您只需解析数据(就像您已有的那样),然后将数据写入一个简单的文本文件,并将粘贴内容从该文件直接复制到Excel中即可。我不是Excel用户,但如果您使用制表符('\t')拆分数据,它会自动将行中的内容放入单独的列中

然后,只需打开output.txt文件并将粘贴内容复制到Excel中


编辑:就像VoidMain指出的那样,您可以用分号(“;”)替换制表符(“/t”),并将文件保存为output.cvs,然后,希望Excel能够按原样打开它。

您好,我正在尝试使用他拥有的代码,但我正在努力让DataTable正确创建。我一直得到一个类型或名称空间名称DataTable找不到错误消息,我已经用谷歌搜索了我需要的,并尝试了几乎所有的方法都没有用。你知道如何让这个讨厌的东西创建一个数据表吗?复制/粘贴并不是最好的解决方案,你可以简单地创建一个csv文件,甚至是一个XML文档,并根据具体情况将其保存为csv或xls。
// Construct a tab-separated string with your variables:
StringBuilder str = new StringBuilder();
str.Append(i.ToString());
str.Append("\t");
str.Append(lineStart.ToString());
str.Append("\t");
str.Append(letters.ToString());
...

// Push the string into your list:
myList.Add(str.ToString());
...

// Finally, you can write the contents of your list into a text file:
System.IO.File.WriteAllLines("output.txt", myList.ToArray());