Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an application that converts .xlsx files into .txt files. On one of my files, there are multiple agencies I need to separate and write the similar ones to their own file. For example, There is an agency 55 and agency 99. There are several entries of each and not necessarily in order. I need a file for each agency with only entries in each file that have the correlating agency number.

Now the kicker is I won't know what the agency number is or how long it will be on any given row. I have figured out how to get the agency number for each row I have, but I need help on how to store those values and use them to write a file that has only those agency numbers in them.

Here is what I have so far to get the agency number I need and my initial code that just wrote to one file.

C#
if (ddlType.SelectedValue == "authorize.net")
        {
            string filename = FileUpload1.FileName.ToString();
            FileUpload1.SaveAs("C:\\inetpub\\temp\\" + filename);

            if (filename != "")
            {
                FileInfo file = new FileInfo("C:\\inetpub\\temp\\" + filename);

                if (file.Exists)
                {
                    DataTable dt = new DataTable();
                    using (TextReader tr = File.OpenText("C:\\inetpub\\temp\\" + filename))
                    {
                            string line;
                            while ((line = tr.ReadLine()) != null)
                            {
                                string[] items = line.Split('\t');
                                if (dt.Columns.Count == 0)
                                {
                                    // Create the data columns for the data table based on the number of items 
                                    // on the first line of the file
                                    for (int i = 0; i < items.Length; i++)
                                    {
                                        dt.Columns.Add(new DataColumn("Column" + i, typeof(string)));
                                    }
                                }
                                dt.Rows.Add(items);
                            }
                            tr.Close();
                    }
                    ////Print out all the values 
                    string file99 = "C:\\inetpub\\temp\\99-Authorize.Net - " + date1 + " @ " + date2 + ".ach";
                    StreamWriter sw2 = new StreamWriter(file99, true);

                    foreach (DataRow dr in dt.Rows)
                    {
                        string agencyNumber;
                        int index = dr[12].ToString().IndexOf("|");
                        if (index > 0)
                        {
                            agencyNumber = dr[12].ToString().Substring(0, index);
                        }
                        if (dr[0].ToString() != "2")
                        {
                            if (dr[9].ToString() != "Total Amount")
                            {
                                sw2.Write("6                            ");
                                sw2.Write(dr[9].ToString().Replace(".", "").Replace(",", "").PadLeft(10, '0'));
                                sw2.Write((dr[12].ToString().Remove(0, index)).PadRight(15, ' '));
                                sw2.Write((dr[13].ToString() + " " + dr[14].ToString()).PadRight(40));
                                sw2.Write(sw2.NewLine);
                            }
                        }
                    }
                    sw2.Write("9");
                    sw2.Write(sw2.NewLine);
                    sw2.Close();
                    Session.Add("DatedFileName2", file99);
                }
                LinkButton1.Visible = true;
                LinkButton2.Visible = true;
            }
        }
Posted
Updated 25-Jun-12 7:05am
v2

1 solution

Maybe I'm oversimplifying the issue here, but try the following.

In your last loop, try the following
C#
////Print out all the values 
foreach (DataRow dr in dt.Rows)
{
    StringBuilder sb = new StringBuilder();

    string agencyNumber;
    int index = dr[12].ToString().IndexOf("|");
    if (index > 0)
    {
        agencyNumber = dr[12].ToString().Substring(0, index);
    }
    if (dr[0].ToString() != "2")
    {
        if (dr[9].ToString() != "Total Amount")
        {
            sb.Append("6                            ");
            sb.Append(dr[9].ToString().Replace(".", "").Replace(",", "").PadLeft(10, '0'));
            sb.Append((dr[12].ToString().Remove(0, index)).PadRight(15, ' '));
            sb.Append((dr[13].ToString() + " " + dr[14].ToString()).PadRight(40));
            sb.Append(sw2.NewLine);
        }
    }
    }

sb.Append("9");
sb.Append(sw2.NewLine);
                   
string file = "C:\\inetpub\\temp\\" + agencyNumber + "-Authorize.Net - " + date1 + " @ " + date2 + ".ach";
using (StreamWriter sw2 = new StreamWriter(file, true)
{
    sw2.WriteLine(sb.ToString());
}


So I'm using the foreach loop to decide which agency you're dealing with and changing the file name to write to each time. This minor change in logic will give you multiple files with the data specific to each agency. It probably isn't perfect, but a great start towards your final solution.

Hogan
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900