加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

C#处理EDI文件(二)

(2011-08-16 16:53:41)
标签:

edi

c

杂谈

分类: IT技术

10.打开optFile.cs的代码页:

   a.导入命名空间:using System.IO;

   b.添加5个处理5种不同文件的方法

      //处理EDI文件
        public bool optEDI(string strFullName)
        {
            //strFullName 传递的完整文件名(包括路径)
            try
            {
                string title = "", path, sse, input, path2, insert = "";
                char s = 'a';
                int length, t, i, n = 0;
                string[] array = new string[300];

                StringBuilder line = new StringBuilder();
                StringBuilder rline = new StringBuilder();

                System.IO.StreamReader sr = new StreamReader(strFullName);//filename存放的是文件的路径

                path = strFullName;
                length = path.Length;
                path2 = path.Insert(length - 11, "results\\");

                string path3 = path2.Substring(0, path2.Length - 12);
                if (!Directory.Exists(path3)) { Directory.CreateDirectory(path3); }

                File.Delete(path2);
                if (path[length - 12] == '\\')
                {
                    title = path.Substring(length - 11, 7);
                    s = title[0];
                    if (s == '8') insert = "1810j ";//取数字的第一位
                    if (s == '9') insert = "MRLn  ";
                    if (s == '2') insert = "MSN   ";
                    if (s == '3') insert = "CX-GCn";
                    if (s == '1') insert = "MRLSn ";
                }

                using (System.IO.StreamWriter sw = new StreamWriter("title.txt"))
                {
                    input = sr.ReadLine();
                    line.Insert(0, input);
                    if (s == '9' || s == '3' || s == '2' || s == '1') { line.Replace("1810j ", insert); }
                    array[0] = line.ToString();

                    line.Remove(0, line.Length);

                    rline.Insert(0, "asdfasdf");
                    t = 1;
                    while ((input = sr.ReadLine()) != null)
                    {
                        line = line.Insert(0, input);
                        array[t] = line.ToString();
                        t++;
                        line.Remove(0, line.Length);
                    }//依次读出每一行
                    t--;


                    for (int k = 0; k <= t; k++)
                    {
                        if (line.Length > 0) { line.Remove(0, line.Length); }
                        line = line.Insert(0, array[k]);
                        if (array[k].Length > 39)
                        {
                            sse = array[k].Substring(29, 8);
                            if (sse == "57914110" || sse == "57914109" || sse == "57914108")
                            {
                                for (int j = t; j > k; j--)
                                {
                                    if (line.Length > 0) { line.Remove(0, line.Length); }
                                    line = line.Insert(0, array[j]);
                                    if (rline.Length > 0) { rline.Remove(0, rline.Length); }
                                    rline.Insert(0, array[j - 1]);
                                    if (rline.Length > 39)
                                    {
                                        for (i = 4; i < line.Length - 1; i++)
                                            if (line[i] == ' ') { n = i; break; }
                                        for (i = 4; i < n; i++)
                                        {
                                            if (line.Length < n || rline.Length < n)
                                                break;
                                            line[i] = rline[i];
                                        }
                                        array[j] = line.ToString();

                                    }

                                }
                                for (int w = k; w <= t; w++)
                                { array[w] = array[w + 1]; }
                                t--;
                                k--;
                            }

                            if (sse == "970262  ")
                            {
                                if (line.Length > 0) { line.Remove(0, line.Length); }
                                line = line.Insert(0, array[k]);
                                line.Replace("303.13", "103.03");
                                line.Replace("970262", "148168");
                                array[k] = line.ToString();
                                line.Remove(0, line.Length);

                                string other = "P10 90        200.10      189459        200.10";

                                int id = 0;
                                for (int j = t; j > k; j--)
                                {
                                    if (line.Length > 0) { line.Remove(0, line.Length); }
                                    line = line.Insert(0, array[j]);
                                    id = (j - 4 + 1) * 10;
                                    string y = id.ToString();
                                    for (int w = 0; w < y.Length; w++)
                                    {
                                        if (line.Length < y.Length) break;
                                        line[4 + w] = y[w];
                                    }
                                    array[j] = line.ToString();

                                }

                                for (int g = t; g > k; g--)
                                {
                                    array[g + 1] = array[g];
                                }
                                t++;
                                array[k + 1] = other.ToString();
                                if (line.Length > 0) { line.Remove(0, line.Length); }
                                line = line.Insert(0, array[k + 1]);
                                id = (k + 1 - 4) * 10;
                                string v = id.ToString();

                                for (int w = 0; w < v.Length; w++)
                                {
                                    if (line.Length < v.Length) break;
                                    line[4 + w] = v[w];
                                }
                                array[k + 1] = line.ToString();

                            }
                        }
                    }
                    for (int g = 0; g <= t; g++)
                        sw.WriteLine(array[g]);

                }
                File.Copy("title.txt", path2);
                File.Delete("title.txt");

                //返回结果
                return true;
            }
            catch
            {
                //返回结果
                return false;
            }
        }

文摘摘自:http://lovecoding.blog.163.com/blog/static/2564403920078183451874/

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有