Click here to Skip to main content
15,914,419 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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#?
Posted
Comments
ZurdoDev 11-Sep-13 8:38am    
Just write the code and process it.

Suppose my text file info.txt contains below data :
VB
ID 1 Name Mary Mark 89
ID 2 Name Mai Mark 67


and my text file Mobile.txt contains below data :
VB
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 :


C#
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 :
C#
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
 
Share this answer
 
v2
You can use String.Split[^] or a combination of String.IndexOf[^] and String.Substring[^] to isolate the ID.
 
Share this answer
 
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 ;(
 
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