locked
write formatted text into text file RRS feed

  • Question

  • Hello !

    i want to create formatted text file as follows from c#.

    All columns should be aligned properly.

    ---------------------------------------------------------------------------------------------------------------------

                                                               ABC Enterprise

    ---------------------------------------------------------------------------------------------------------------------

    Sr No  |     Product Name                 |      Qty      |        Rate       |     Total                                 |

    ---------------------------------------------------------------------------------------------------------------------

    1        | Keyboard                            | 2               |    400.00       |    800.00                               |

    2        | Monitor                               | 1               |  5000.00       |  5000.00                               |

    ---------------------------------------------------------------------------------------------------------------------

                                                                                        Total      |  5800.00

    ---------------------------------------------------------------------------------------------------------------------


    Mayur Dabhi

    Saturday, July 14, 2012 1:50 PM

Answers

All replies

  • Not something that can be done with a text file.  Format your text in a RichTextBox and save the RichTextBox contents as RTF.
    Saturday, July 14, 2012 2:03 PM
  • You could write the fields and use PadLeft or PadRight to get the alignment for each field. However that assumes the font is a non-proportional one. If you use a proportional font then they won't align because of the different character widths.


    Regards David R
    ---------------------------------------------------------------
    The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones.
    Object-oriented programming offers a sustainable way to write spaghetti code. - Paul Graham.
    Every program eventually becomes rococo, and then rubble. - Alan Perlis
    The only valid measurement of code quality: WTFs/minute.

    Saturday, July 14, 2012 8:10 PM
  • Hello !

    i have used following code to create file.

    StreamWriter writer = new StreamWriter("c:\\test.txt");
    
                Tuple<string, DateTime, int, DateTime, int>[] cities = 
              { Tuple.Create("Los Angeles", new DateTime(1940, 1, 1), 1504277, 
                             new DateTime(1950, 1, 1), 1970358),
                Tuple.Create("New York", new DateTime(1940, 1, 1), 7454995, 
                             new DateTime(1950, 1, 1), 7891957),  
                Tuple.Create("Chicago", new DateTime(1940, 1, 1), 3396808, 
                             new DateTime(1950, 1, 1), 3620962),  
                Tuple.Create("Detroit", new DateTime(1940, 1, 1), 1623452, 
                             new DateTime(1950, 1, 1), 1849568) };
    
                // Display header
                string header = String.Format("{0,-12}{1,-8}{2,-12}{1,-8}{2,-12}{3,-14}\n",
                                              "City", "Year", "Population", "Change (%)");
                //Console.WriteLine(header);
                writer.WriteLine(header);
                string output;
    
                StringBuilder sb = new StringBuilder();
    
                foreach (var city in cities)
                {
                    writer.Write("{0,-12}{1,-8:yyyy}{2,-12:N0}{3,-8:yyyy}{4,-12:N0}{5,-14:P1}{6}",
                                           city.Item1, city.Item2, city.Item3, city.Item4, city.Item5,
                                           (city.Item5 - city.Item3) / city.Item3 * 1.0, Environment.NewLine);
    
    
    
    
                }
                // writer.WriteLine(sb.ToString());
                writer.Close();


    If i open file in notepad , the text does not aligned as expected.

    For example ,

    City        Year    Population  Year    Population  Change (%)    

    Los Angeles 1940    1,504,277   1950    1,970,358   0.0 %         
    New York    1940    7,454,995   1950    7,891,957   0.0 %         
    Chicago     1940    3,396,808   1950    3,620,962   0.0 %         
    Detroit     1940    1,623,452   1950    1,849,568   0.0 %         

    But if i open file in Notedpad++ or in DOS , it shows aligned output as expected.

    For example,

    City              Year    Population  Year    Population  Change (%)    

    Los Angeles 1940    1,504,277   1950    1,970,358   0.0 %         
    New York     1940    7,454,995   1950    7,891,957   0.0 %         
    Chicago       1940    3,396,808   1950    3,620,962   0.0 %         
    Detroit         1940    1,623,452   1950    1,849,568   0.0 %         

    Why ?


    Mayur Dabhi

    Sunday, July 15, 2012 1:23 AM
  • Can you to the Format menu of notepad and uncheck the Word Wrap.

    Regards,
    Ahmed Ibrahim
    SQL Server Setup Team
    My Blog
    This posting is provided "AS IS" with no warranties, and confers no rights. Please remember to click "Mark as Answer" and "Vote as Helpful" on posts that help you.
    This can be beneficial to other community members reading the thread.

    Sunday, July 15, 2012 1:27 AM
  • Use Graphics.DrawString statements to format your text and record to a metafile.  Play back the metafile for display or printing.
    • Proposed as answer by Lisa Zhu Tuesday, July 17, 2012 7:54 AM
    • Marked as answer by Lisa Zhu Thursday, July 19, 2012 10:46 AM
    • Unmarked as answer by Mayur Dabhi Sunday, July 29, 2012 2:57 AM
    • Unproposed as answer by Lisa Zhu Thursday, August 2, 2012 9:21 AM
    Sunday, July 15, 2012 1:48 AM
  • Can you to the Format menu of notepad and uncheck the Word Wrap.

    Regards,
    Ahmed Ibrahim
    SQL Server Setup Team
    My Blog
    This posting is provided "AS IS" with no warranties, and confers no rights. Please remember to click "Mark as Answer" and "Vote as Helpful" on posts that help you.
    This can be beneficial to other community members reading the thread.


    I have tried it also but not worked.

    Mayur Dabhi

    Sunday, July 29, 2012 2:51 AM
  • What could be the best way to create formatted text file for printing reports ?

    Mayur Dabhi

    Sunday, July 29, 2012 2:53 AM
    • Proposed as answer by Lisa Zhu Thursday, August 2, 2012 9:21 AM
    • Marked as answer by Lisa Zhu Friday, August 3, 2012 1:37 AM
    Monday, July 30, 2012 4:57 AM