简体   繁体   中英

Save List<string> and List<double> to a .txt file's specific column

I have a C# WinForm application that has many List<string> and List<double> . I need to create a new .txt file and save each List<> to a specific column of the text file.

I tried WriteAllLines function but it write one List<> .

I also tried to create an Excel file so I can specify which column I want the List<> save to. But I have a hard time to save the temporary excel file as a .txt file.

I know this code can save an existing excel file as a PDF, but similar functions that save the excel as a text file doesn't exist.

NewExcelWorkBook.ExportAsFixedFormat(Excel.XlFixedFormatType.xlTypePDF, "Holdings in BE Import Format.txt", Excel.XlFixedFormatQuality.xlQualityStandard, true,
                                                          false, 1, 10, true);

Please tell me a way to write multiple List<> to specific .txt file column, or you can tell me how to save the excel file as a .txt file. Skipping the temporary excel file would be ideal, but this solution is acceptable if writing to .txt directly is hard.

Thank you so much!

If you want your first column to be of fixed size of 20 characters, you can try the following:

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);

Ouput file would be like:

ABCDEF               1
DEF                  2
GHIAAAAAAAAAAAAAA    3
SOMETHNG LONGER THAN 4
        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);
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM