Reputation: 175
The export needs to be fixed length file that means each field will have predefined location and length like below:
Field A: 20 char
Field B: 15 char
Field C: 10 char
For following values:
Field A Field B Field C Test 1 Test 2 Test 3 This is value 1 This is value 2 This is va
Flat file contents should be:
Test 1 Test 2 Test 3 This is value 1 This is value 2This is va
like Test 1 field have all 20 character including space up to the field Test 2 and same as Test 2 field have 15 character up to field Test 3 and Test 3 field also have 15 character up to end including spaces.
please tell me solution for this either i want to generate it from sql server database table or by c# coding any way.
Upvotes: 2
Views: 1911
Reputation: 13523
This code shows how to do it:
// 1 reading data from database
var data = new List<Tuple<string, string, string>>();
for (var i = 0; i < 10; i++)
{
data.Add(new Tuple<string, string, string>(i.ToString(), (i + 1).ToString(), (i * 3000).ToString()));
}
// 2 writing data to file
foreach (var record in data)
{
var str = string.Format("{0,-20}{1,-15}{2,-10}", record.Item1, record.Item2, record.Item3);
Console.WriteLine(str); // <- or file.WriteLine(...)
}
Since I have no idea what are your model (or domain) objects, I have used a simple Tuple<string, string, string>
instead.
Upvotes: 1