Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Tuesday, January 22, 2013 1:06 PM
hi all
i am writing output to text file, but output is unaligned, please guide me how to arrange.
my sample unaligned output.
LocationCode ItemCode CFAProductCode Month Year SerialNo UnitWeight Quantity BinCode LotNumber
ABO 11GOZZK001 71 12 2012 13.74 13.740 1 GEP 2GABO15580
ABO 21SAOEYTS215 74 12 2012 2.16 2.160 1 BINO 2B
ABO 21RUOERTS125 74 12 2012 1.24 1.240 1 Wind -STN-BTQDiffSta 2B
here i want to align the output with equal space.
please help me out.
its my coding part.
for header
sw.WriteLine(dt.Columns[0].ColumnName.ToString() + "\t" + dt.Columns[1].ColumnName.ToString() + "\t" + dt.Columns[2].ColumnName.ToString() + "\t" + dt.Columns[3].ColumnName.ToString() + "\t" + dt.Columns[4].ColumnName.ToString() + "\t" + dt.Columns[5].ColumnName.ToString() + "\t" + dt.Columns[6].ColumnName.ToString() + "\t" + dt.Columns[7].ColumnName.ToString() + "\t" + dt.Columns[8].ColumnName.ToString() + "\t" + dt.Columns[9].ColumnName.ToString());
for data
sw.WriteLine(dt.Rows[i][0].ToString() + "\t" + dt.Rows[i][1].ToString()
+ "\t" + dt.Rows[i][2].ToString()
+ "\t" + dt.Rows[i][3].ToString()
+ "\t" + dt.Rows[i][4].ToString()
+ "\t" + dt.Rows[i][5].ToString()
+ "\t" + dt.Rows[i][6].ToString()
+ "\t" + dt.Rows[i][7].ToString()
+ "\t" + dt.Rows[i][8].ToString()
+ "\t" + dt.Rows[i][9].ToString()
);
All replies (3)
Tuesday, January 22, 2013 1:28 PM ✅Answered
selvakumars :
Did you try to use fixed length ?
Try bing/google for String.format with fixed length and hope the below helps as start.
Also you can use a loop rather than appending individual element.
//header
StringBuilder objBuilder = new StringBuilder();
for (int i = 0; i <= 9; i++)
{
objBuilder.Append(String.Format("{0,-25}",dt.Columns[i].ColumnName.ToString());
}
sw.WriteLine(objBuilder.ToString());
//do the same for data
Tuesday, January 22, 2013 3:16 PM ✅Answered | 1 vote
Text files don't have any provision for formatting. To save a file with formatting, use RTF or one of the HTML variants.
Wednesday, January 23, 2013 6:55 AM ✅Answered
i am using padright, i can format using this also.
like
+ "\t" + dt.Columns[2].ColumnName.ToString().PadRight(20)