Avatar of GD_GRAY
GD_GRAY
Flag for United States of America asked on

How would I export a DataTable to a text file and define the string length and the start position of each column ?

My issue is I need to build a text file with a specific format. Each row (line) in the text file can only be 111 characters long and each row (line) in the text file will represent a single row from the DataTable.

DataTable:
Column1 | Column2 | Column3 | Column4
My            |Phone #   |is                |8000000000
My            |Phone #   |is                |9990000000


TextFile:

Column1 starts at position 0, Column2 starts at position 2, Column3 starts at position 40, Column4 starts at position 90,

MyPhone#                                is                                                8000000000          
MyPhone#                                is                                                9990000000          


I am able to use
public void ExportDataTabletoFile(DataTable datatable, string delimited, bool exportcolumnsheader, string file)

{
    StreamWriter str = new StreamWriter(file, false, System.Text.Encoding.Default);
    if (exportcolumnsheader)
    {
        string Columns = string.Empty;
        foreach (DataColumn column in datatable.Columns)
        {
            Columns += column.ColumnName + delimited;
        }
        str.WriteLine(Columns.Remove(Columns.Length - 1, 1));
    }
    foreach (DataRow datarow in datatable.Rows)
    {
        string row = string.Empty;
 
        foreach (object items in datarow.ItemArray)
        {
 
            row += items.ToString() + delimited;
        }
        str.WriteLine(row.Remove(row.Length - 1, 1));
 
    }
    str.Flush();
    str.Close();
 
}

Open in new window


And call it like
ExportDataTabletoFile(GetTable(), " ,", false, file);

Open in new window


but that is all I can come up with.

result:

My,Phone#,is,8000000000
My,Phone#,is,9990000000

Can any one expand on this for me.

Thanks.....
C#.NET Programming

Avatar of undefined
Last Comment
Fernando Soto

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Fernando Soto

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
David Johnson, CD

you will have to build your strings by yourself
Do you want
my,phone,                    is,           ######## or
column1start = 0
column2start=4
column3start=40
column4start-90
output = column1.text + ","
spaces = column2start - strlen(output)
spacer = ""
for (I=0;i<spaces;i++){spacer += " ";}
output= output + spacer + column2.txt + ",";
spaces = column3start - strlen(output);
spacer = ""
for (I=0;i<spaces;i++){spacer += " ";}
output= output + spacer + column3.txt + ",";
spaces = column4start - strlen(output);
spacer = ""
for (I=0;i<spaces;i++){spacer += " ";}
output= output + spacer + column4.txt;
write(output);
GD_GRAY

ASKER
Wow......

Thank you !
GD_GRAY

ASKER
Soto Sir,

Any way to get the string format values from a list, or a datatable column ?
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Fernando Soto

To your followup question, "Any way to get the string format values from a list, or a datatable column ?", the format specifier is just a string and can be stored in a list or any object that can store a string in. When you are ready to use it retrieve it from where you are storing it and place it in a string variable and use that string variable where the specifier is now.