我如何在2个txt文件之间进行此映射 [英] how can i do this mapping between 2 txt files

查看:103
本文介绍了我如何在2个txt文件之间进行此映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的文本文件包含如下数据:



ID 1姓名Mary Mark 89

ID 2姓名Mai Mark 67



我还有另一个包含数据的文本文件:

ID Mobile

1 0993209320

2 0943043094



我想创建只包含两列的excel文件:

手机短信

0993209320 ID 1姓名Mary Mark 89

0943043094 ID 2姓名Mai Mark 67





我该怎么做基于ID在2个文本文件之间进行映射以获取c#中的上一个excel文件?

解决方案

假设我的文本文件 info.txt 包含在下面数据:

 ID  1 名称Mary Mark  89  
ID 2 名称Mai Mark 67





和我的文本文件 Mobile.txt 包含以下数据:

 ID移动
1 0993209320
2 0943043094





在这里,我建议创建一个学生课程,其中包含两个属性移动消息。并将所有映射添加到该类的列表对象中,如下所述:





  var  infos = System.IO.File.ReadLines(  C:\\ \\\CodeBlack\\Info.txt); 
var mobiles = System.IO.File.ReadLines( C:\\CodeBlack\\Mobile.txt);

var studentInfo = new List< Student>();
studentInfo.Add( new 学生
{
Mobile = 移动
消息= 消息
});

foreach var info in infos)
{
var id = info.Split(' ')[ 1 ]。ToString();

var mobile = string .Empty;
mobile = mobiles.Where(m = > m.Split(' ')[ 0 ] == id)。选择(m = > ; m.Split(' ')[ 1 ])FirstOrDefault();

studentInfo.Add( new 学生
{
手机=手机,
消息= info
});
}







学生班级:

  public   class 学生
{
public string 移动{ get ; set ; }

public string 消息{获得; set ; }
}







现在你可以使用studentInfo将它导出到excel。 />
可以在网上轻松获得导出到Excel的代码。



问候,

CodeBlack


你可以使用String.Split [ ^ ]或String.IndexOf [ ^ ]和 String.Substring [ ^ ]隔离ID。


有几种方法:



1)使用ADO.NET

关于文本文件的大量ADO [ ^ ]

如何使用ADO.NET使用Visual Basic .NET在Excel工作簿中检索和修改记录 [ ^ ]



2)使用StringReader [ ^ ]类读取文本文件和Split [ ^ ]方法。

如何:从文件中读取文本 [ ^ ]

如何:拆分字符串(C#编程指南) [ ^ ]

您没有指定用于写入Excel文件的方法,所以我无法帮助您;(

I have text file contains data like as:

ID 1 Name Mary Mark 89
ID 2 Name Mai Mark 67

and I have another text file contains data :
ID Mobile
1 0993209320
2 0943043094

I want to create excel file which contains only two columns:
Mobile Message
0993209320 ID 1 Name Mary Mark 89
0943043094 ID 2 Name Mai Mark 67


how can i do this mapping between 2 text files based on ID to obtain the previous excel file in c#?

解决方案

Suppose my text file info.txt contains below data :

ID 1 Name Mary Mark 89
ID 2 Name Mai Mark 67



and my text file Mobile.txt contains below data :

ID Mobile
1 0993209320
2 0943043094



Here I would suggest to create one student class with two properties Mobile and Message. And add all the mappings into list object of that class as mentioned below :


var infos = System.IO.File.ReadLines("C:\\CodeBlack\\Info.txt");
            var mobiles = System.IO.File.ReadLines("C:\\CodeBlack\\Mobile.txt");

            var studentInfo = new List<Student>();
            studentInfo.Add(new Student
                {
                    Mobile =  "Mobile",
                    Message = "Message"
                });

            foreach (var info in infos)
            {
                var id = info.Split(' ')[1].ToString();

                var mobile = string.Empty;
                mobile =  mobiles.Where(m => m.Split(' ')[0] == id).Select(m => m.Split(' ')[1]).FirstOrDefault();

                studentInfo.Add(new Student
                    {
                        Mobile = mobile,
                        Message = info
                    });
            }




Student Class :

public class Student
    {
        public string Mobile { get; set; }

        public string Message { get; set; }
    }




Now you can use studentInfo to export it to excel.
Code for export to excel is easily available on net.

Regards,
CodeBlack


You can use String.Split[^] or a combination of String.IndexOf[^] and String.Substring[^] to isolate the ID.


There are few ways:

1) using ADO.NET
Much ADO About Text Files[^]
How To Use ADO.NET to Retrieve and Modify Records in an Excel Workbook With Visual Basic .NET[^]

2) using StringReader[^] class to read text files and Split[^] method.
How to: Read Text from a File[^]
How to: Split Strings (C# Programming Guide)[^]
You did not specify the method you're using to write to Excel file, so i can't help you more ;(


这篇关于我如何在2个txt文件之间进行此映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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