Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 保存列表<;字符串>;和列表<;双倍>;到.txt文件';s特定列_C#_Excel_Text - Fatal编程技术网

C# 保存列表<;字符串>;和列表<;双倍>;到.txt文件';s特定列

C# 保存列表<;字符串>;和列表<;双倍>;到.txt文件';s特定列,c#,excel,text,C#,Excel,Text,我有一个C#WinForm应用程序,它有许多List和List。我需要创建一个新的.txt文件,并将每个列表保存到文本文件的特定列中 我尝试了WriteAllines函数,但它只写了一个列表 我还尝试创建一个Excel文件,以便指定要将列表保存到哪个列。但我很难将临时excel文件保存为.txt文件 我知道这段代码可以将现有的excel文件保存为PDF格式,但不存在将excel保存为文本文件的类似功能 NewExcelWorkBook.ExportAsFixedFormat(Excel.XlFi

我有一个C#WinForm应用程序,它有许多
List
List
。我需要创建一个新的.txt文件,并将每个
列表
保存到文本文件的特定列中

我尝试了WriteAllines函数,但它只写了一个
列表

我还尝试创建一个Excel文件,以便指定要将
列表保存到哪个列。但我很难将临时excel文件保存为.txt文件

我知道这段代码可以将现有的excel文件保存为PDF格式,但不存在将excel保存为文本文件的类似功能

NewExcelWorkBook.ExportAsFixedFormat(Excel.XlFixedFormatType.xlTypePDF, "Holdings in BE Import Format.txt", Excel.XlFixedFormatQuality.xlQualityStandard, true,
                                                          false, 1, 10, true);
请告诉我如何将多个
列表
写入特定的.txt文件列,或者您可以告诉我如何将excel文件保存为.txt文件。跳过临时excel文件将是理想的,但如果直接写入.txt很困难,则此解决方案是可以接受的

非常感谢你

List list1=新列表();
        List<string> list1 = new List<string>();
        List<int> list2 = new List<int>();
        //...
        string separator = "\t";
        using (StreamWriter writer = new StreamWriter(fileName)){
            for (int i = 0; i<Math.Max(list1.Count, list2.Count); i++){
                var element1 = i < list1.Count ? list1[i] : "";
                var element2 = i < list2.Count ? list2[i].ToString() : "";
                writer.Write(element1);
                writer.Write(separator);
                writer.WriteLine(element2);
            }
        }
List list2=新列表(); //... 字符串分隔符=“\t”; 使用(StreamWriter=newstreamwriter(文件名)){ 对于(int i=0;i
List list1=new List();
List list2=新列表();
//...
字符串分隔符=“\t”;
使用(StreamWriter=newstreamwriter(文件名)){
对于(int i=0;i
List list1=new List();
List list2=新列表();
//...
字符串分隔符=“\t”;
使用(StreamWriter=newstreamwriter(文件名)){
对于(int i=0;i
List list1=new List();
List list2=新列表();
//...
字符串分隔符=“\t”;
使用(StreamWriter=newstreamwriter(文件名)){

对于(int i=0;i如果希望第一列的大小固定为20个字符,可以尝试以下操作:

List<string> stringList = new List<string>
    {
        "ABCDEF",
        "DEF",
        "GHIAAAAAAAAAAAAAA",
        "SOMETHNG LONGER THAN 20 characters",
    };

List<double> doubleList = new List<double>
    {
        1d,
        2,
        3,
        4
    };

List<string> combined = new List<string>();
int count = stringList.Count >= doubleList.Count ? stringList.Count : doubleList.Count;
for (int i = 0; i < count; i++)
{
    string firstColumn = stringList.Count <= i ? "" : stringList[i];
    string secondColumn = doubleList.Count <= i ? "" : doubleList[i].ToString();
    if (firstColumn.Length > 20)
    {
        //truncate rest of the values
        firstColumn = firstColumn.Substring(0, 20);
    }
    else
    {
        firstColumn = firstColumn + new string(' ', 20 - firstColumn.Length);
    }
    combined.Add(string.Format("{0} {1}", firstColumn, secondColumn));
}

File.WriteAllLines("yourFilePath.csv", combined);

如果希望第一列的固定大小为20个字符,可以尝试以下操作:

List<string> stringList = new List<string>
    {
        "ABCDEF",
        "DEF",
        "GHIAAAAAAAAAAAAAA",
        "SOMETHNG LONGER THAN 20 characters",
    };

List<double> doubleList = new List<double>
    {
        1d,
        2,
        3,
        4
    };

List<string> combined = new List<string>();
int count = stringList.Count >= doubleList.Count ? stringList.Count : doubleList.Count;
for (int i = 0; i < count; i++)
{
    string firstColumn = stringList.Count <= i ? "" : stringList[i];
    string secondColumn = doubleList.Count <= i ? "" : doubleList[i].ToString();
    if (firstColumn.Length > 20)
    {
        //truncate rest of the values
        firstColumn = firstColumn.Substring(0, 20);
    }
    else
    {
        firstColumn = firstColumn + new string(' ', 20 - firstColumn.Length);
    }
    combined.Add(string.Format("{0} {1}", firstColumn, secondColumn));
}

File.WriteAllLines("yourFilePath.csv", combined);

如果希望第一列的固定大小为20个字符,可以尝试以下操作:

List<string> stringList = new List<string>
    {
        "ABCDEF",
        "DEF",
        "GHIAAAAAAAAAAAAAA",
        "SOMETHNG LONGER THAN 20 characters",
    };

List<double> doubleList = new List<double>
    {
        1d,
        2,
        3,
        4
    };

List<string> combined = new List<string>();
int count = stringList.Count >= doubleList.Count ? stringList.Count : doubleList.Count;
for (int i = 0; i < count; i++)
{
    string firstColumn = stringList.Count <= i ? "" : stringList[i];
    string secondColumn = doubleList.Count <= i ? "" : doubleList[i].ToString();
    if (firstColumn.Length > 20)
    {
        //truncate rest of the values
        firstColumn = firstColumn.Substring(0, 20);
    }
    else
    {
        firstColumn = firstColumn + new string(' ', 20 - firstColumn.Length);
    }
    combined.Add(string.Format("{0} {1}", firstColumn, secondColumn));
}

File.WriteAllLines("yourFilePath.csv", combined);

如果希望第一列的固定大小为20个字符,可以尝试以下操作:

List<string> stringList = new List<string>
    {
        "ABCDEF",
        "DEF",
        "GHIAAAAAAAAAAAAAA",
        "SOMETHNG LONGER THAN 20 characters",
    };

List<double> doubleList = new List<double>
    {
        1d,
        2,
        3,
        4
    };

List<string> combined = new List<string>();
int count = stringList.Count >= doubleList.Count ? stringList.Count : doubleList.Count;
for (int i = 0; i < count; i++)
{
    string firstColumn = stringList.Count <= i ? "" : stringList[i];
    string secondColumn = doubleList.Count <= i ? "" : doubleList[i].ToString();
    if (firstColumn.Length > 20)
    {
        //truncate rest of the values
        firstColumn = firstColumn.Substring(0, 20);
    }
    else
    {
        firstColumn = firstColumn + new string(' ', 20 - firstColumn.Length);
    }
    combined.Add(string.Format("{0} {1}", firstColumn, secondColumn));
}

File.WriteAllLines("yourFilePath.csv", combined);



所以,当你说你有多个列时,你的意思是你有一个csv文件(列用逗号或制表符分隔)?列表的长度是否相等?列是以制表符分隔的,例如我需要将列表test1保存到第1列,将列表test2保存到第75列。因此,这些列之间会有制表符。列表的长度不相等。文件是否已经存在,并且您要“放置”特定列中的列表?我需要创建一个新的.txt文件。因此,当您说您有多个列时,您的意思是您有一个csv文件(列以逗号或制表符分隔)?列表的长度是否相等?列是以制表符分隔的,例如我需要将列表test1保存到第1列,将列表test2保存到第75列。因此,这些列之间会有制表符。列表的长度不相等。文件是否已经存在,并且您要“放置”特定列中的列表?我需要创建一个新的.txt文件。因此,当您说您有多个列时,您的意思是您有一个csv文件(列以逗号或制表符分隔)?列表的长度是否相等?列是以制表符分隔的,例如我需要将列表test1保存到第1列,将列表test2保存到第75列。因此,这些列之间会有制表符。列表的长度不相等。文件是否已经存在,并且您要“放置”特定列中的列表?我需要创建一个新的.txt文件。因此,当您说您有多个列时,您的意思是您有一个csv文件(列以逗号或制表符分隔)?列表的长度是否相等?列是以制表符分隔的,例如我需要将列表test1保存到第1列,将列表test2保存到第75列。因此,这些列之间会有制表符。列表的长度不相等。文件是否已经存在,并且您要“放置”特定列中的列表?我需要创建一个新的.txt文件。我现在可以解决不同长度的问题。请告诉我如何将每个列表保存到特定列。我不能用逗号分隔每个列。比如说StringList从第1列开始,doubleList从第20列开始。谢谢!@Eddie,我想你想写一个文本文件,如果列到目前为止是一部分,那么您是否正在尝试更新现有文件?您是否需要excel或文本文件中的解决方案?文本文件中没有列的概念。您可以在两个列表之间添加20个逗号,但我认为这没有意义。我需要创建一个新的文本文件。解决方案文件格式为.txt。您可以查看状态在文本文件中添加条,然后查看行和列#。我可以在行和列之间加上20个空格,但这使得第二个列表不是从某一列开始的,因为第1列中的字符串的字符数不同。实际上,我可以使用第一列创建一个新的文本文件。然后通过添加一个新的20个空格的列来更新文件除了第一列。你认为这样行吗?@Eddie,我不确定你的要求,但你可以试试我刚才在回答中所做的新编辑。我现在可以解决不同长度的问题。请告诉我如何将每个列表保存到特定列。我不能用逗号分隔每个列。比如说StringList从第1列开始,然后doubleList从第20列开始。谢谢!@Eddie,我猜你想写一个文本文件,如果到目前为止,这些列是一个部分,那么你是想更新一个现有的文件吗?你还需要excel或文本文件中的解决方案吗?文本文件中没有列的概念。你可以在两个列表之间加20个逗号,但我认为这样做没有意义有意义。我需要创建一个新的文本文件。解决方案文件格式为.txt。您可以查看文本文件中的状态栏,并查看行和列。我可以在两者之间放置20个空格,但这会使第二个列表不会从某一列开始,因为列1中的字符串的字符数不同。实际上,我可以创建