根据DataRow内容创建多个文件 [英] Creating multiple files based on DataRow content

查看:87
本文介绍了根据DataRow内容创建多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个将.xlsx文件转换为.txt文件的应用程序.在我的一个文件中,我需要多个代理机构来分开并将类似的代理机构写入自己的文件中.例如,有一个机构55和99.每个都有多个条目,不一定按顺序输入.我需要每个代理商的文件,每个文件中只有条目具有相关代理商编号.

现在最重要的是,我不知道代理商编号是多少,或者在任何给定的行中它将持续多长时间.我已经弄清楚了如何获取每行的代理商编号,但是我需要有关如何存储这些值并使用它们来编写仅包含那些代理商编号的文件的帮助.

这是到目前为止我可以获得所需的代理机构编号以及刚写到一个文件中的初始代码.

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.

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;
            }
        }

推荐答案

也许我在这里简化了这个问题,但是请尝试以下方法.

在您的上一个循环中,尝试以下
Maybe I''m oversimplifying the issue here, but try the following.

In your last loop, try the following
////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());
}



因此,我正在使用foreach循环来确定您要处理的代理,并更改每次写入的文件名.逻辑上的微小更改将为您提供多个文件,其中包含每个代理商的特定数据.这可能不是完美的,但是是最终解决方案的一个很好的开始.

霍根



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


这篇关于根据DataRow内容创建多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆