Reverse engineer and get c# code from report text file

tapdev72
tapdev72 used Ask the Experts™
on
We are using .NET to create various reports (in .txt format) - so files created are text files. Using streamwriter and corresponding code like writer.WriteLine(.......)'s to format the output on the report.  Lot of reports, so lot of manual work. Wondering if anyone has a faster way to do this - from the foo.txt report file, can we create the c# code - basically get back the write.writeLine code blocks.  

So the foo.txt file ->  gets passed to this converter -> which would output all the write.writelines with corresponding spaces and other formatted text in the .txt file. If any of the experts could help.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2012
Top Expert 2014

Commented:
Report files in txt files?

Report files do not have any code in them right? If I understand the question correctly, I think you can't.
How about;
1. Take your example report and replace areas you want inserted with {1},{2},{3}, etc.
2. Read the sample to a string like strTemplate
3. Use String.Format(strTemplate,1, 2, 3) to return your new string to be written to file.

This will allow you to use the existing reports as templates and not worry about the writeline problems. I will attach a sample in a moment.
Using this sample text file;
Test Report
Name: {0}
Address: {1}
Phone: {2}

Open in new window


and this c# code
 
//Read Template to strTemplate
            StreamReader streamReader = new StreamReader(@"c:\temp\template.txt");
            string strTemplate = streamReader.ReadToEnd();
            streamReader.Close();
            //create string array of new values and output formatted string
            string[] values = { "Doe, John", "111 Main St", "(111)111-1111" };
            string strFormattedReport = string.Format(strTemplate,values);

Open in new window


I can create this
Test Report
Name: Doe, John
Address: 111 Main St
Phone: (111)111-1111

Open in new window

Author

Commented:
But what about multiple lines in a report.  Above scenario can be applied to a 1 line report. or can this be adjusted for a typical report.
You can do repeating lines simply by substituting one of your string array items with a fixed string containing Environment.NewLine.



If you were to add {3} to the end of the template file (or in the middle, wherever you like really) you could do this

an example from a datatable
string strRepeatingRow;
string tab="     "; //this is just to keep my tabs consistent. 
foreach(DataRowView row in myTable.DefaultView)
{
       strRepeatingRow = strRepeatingRow +
           tab+
           row[0].ToString()+
           tab+
           row[2].ToString()+
           tab+
           row[3].ToString()+
           Environment.NewLine;
       
}
string[] values = { "Doe, John", "111 Main St", "(111)111-1111",strRepeatingRow };

Open in new window

You could get creative and build a method to return a string value from a DataTable if you like.
let me know if you need a more functional example.
You could also get crafty and revise the code here
http://www.mycsharpcorner.com/Post.aspx?postID=15
to extract and refine the text between to tags like <repeat></repeat>. Just use the Environment.NewLine and you can repeat as much as you like.

Author

Commented:
wingatesl, great idea for the detailed lines of report. Could you also post the functional example.
Here ya go

Template File
Test Report
Name: {0,-50}
Address: {1,-50}
Phone: {2}
Books On Loan
===============================================================================
Title				   |Date Checked Out
-------------------------------------------------------------------------------
{3}
===============================================================================

Open in new window


The Code Example
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ReportTemplateTest
{
    class Program
    {
        static void Main(string[] args)
        {

            //I am using a list of string[] to emulate a data table to insert into the report.
            //In real life this would most likely be a DataTable as in my previous example
            List<string[]> books = new List<string[]>();
            books.Add(new string[] { "Rabbits", "12/12/12" });
            books.Add(new string[] { "Dogs", "12/10/12" });
            //
            //Read Template to strTemplate
            StreamReader streamReader = new StreamReader(@"c:\temp\template.txt");
            string strTemplate = streamReader.ReadToEnd();
            streamReader.Close();
            //create string array of new values and output formatted string
            string[] values = { "Doe, John", "111 Main St", "(111)111-1111", booksOnLoan(books) };
            string strFormattedReport = string.Format(strTemplate,values);
            string bt = "";
        }
        static string booksOnLoan(List<string[]> books)
        {
            string retval = "";
            //I have created a string to format for each of the rows in my repeating table. This allows me 
            //to specify a fixed width for my columns (-35 for left justified, 35 for right)
            string bookrow = "{0,-35}|{1}"+Environment.NewLine;
            //we now go through each row in our table and add it to our return value. 
            foreach (string[] book in books)
            {
                retval = retval + string.Format(bookrow, book[0], book[1]);
            }
            
            return retval;
        }
    }
}

Open in new window


and the end result

Test Report
Name: Doe, John                                         
Address: 111 Main St                                       
Phone: (111)111-1111
Books On Loan
===============================================================================
Title				   |Date Checked Out
-------------------------------------------------------------------------------
Rabbits                            |12/12/12
Dogs                               |12/10/12

===============================================================================

Open in new window

I don't have a functional example for the tag replacement as I have never done that one. It was really just an idea if you needed to extend the functionality.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial