Skip to content

How can i do this mapping between 2 txt files

Hiast Rose

Asked Sep 11, 2013, 4:22 AM EDT2 answers
Share
LinkedInX
I have text file contains data like as:

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

and I have another  txt 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 txt files based on ID in c#?

2 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    Hello,

    try below code this will solve your problem

    Note: 1st file contains data like 

    ID 1 Name Mary Mark 89



    2nd file contains data like 

    ID Mobile
    1   0993209320







     DataTable dtMapping = new DataTable();
            ArrayList lstMapping = new ArrayList();
            dtMapping.Columns.Add("Mobile"); dtMapping.Columns.Add("Message");
            string[] readFile1 = File.ReadAllLines(Location of 1st file);
            string[] readFile2 = File.ReadAllLines(Location of 2nd file);


            for (int i = 0; i < readFile1.Length; i++)
            {
                string fileData = readFile1[i].ToString();
                int find_Id_Index = fileData.IndexOf("ID ");
                if (find_Id_Index != -1)
                {
                    int find_Name_Index = fileData.IndexOf("Name ", find_Id_Index);
                   string id = fileData.Substring(find_Id_Index+3,find_Name_Index-(find_Id_Index+3) ).Trim();
                    lstMapping.Add("ID " + id);
                    lstMapping.Add(fileData.Substring(find_Name_Index, fileData.Length - (find_Name_Index)).Trim());
                }
            }


            for (int i = 1; i < readFile2.Length; i++)
            {
                int find_Space_Index = readFile2[i].IndexOf(" ");
                if (find_Space_Index != -1)
                {
                    string file2id = readFile2[i].Substring(0, find_Space_Index).Trim();
                    string mobileno = readFile2[i].Substring(find_Space_Index, readFile2[2].Length - (find_Space_Index - file2id.Length+1)).Trim();
                    int find_Id = lstMapping.IndexOf("ID " + file2id);
                    if (find_Id != -1)
                    {
                        dtMapping.Rows.Add(mobileno, "ID " + file2id + " " + lstMapping[find_Id + 1]);
                    }
                }
            }


            Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
            Microsoft.Office.Interop.Excel.Workbook workbook = excelApp.Workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
            Microsoft.Office.Interop.Excel.Worksheet workSheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets[1];
            for (int i = 0; i < dtMapping.Columns.Count; i++)
            {
                workSheet.Cells[1, (i + 1)] = dtMapping.Columns[i].ColumnName;
            }
            for (int i = 0; i < dtMapping.Rows.Count; i++)
            {
                for (int j = 0; j < dtMapping.Columns.Count; j++)
                {
                    workSheet.Cells[(i + 2), (j + 1)] = dtMapping.Rows[i][j];
                }
            }
            excelApp.Visible = true;
    0
  • Suthish Nair
    0
    If your thread got resolved, please "Accept the thread as Answer", that helped you.
    0