Click here to Skip to main content
15,867,904 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi,

I have created a c# program to generate excel.That working fine.But i am facing a challenge now.I need to add around 100 of data in to the excel row using c#(add to  table.Rows.Add()) .For small data i can manualy add.but my client given data like in txt file.I have created coloum data but how can i programaticaly add rows from .txt file?

I need to add below data to  table.Rows.Add(1, 2, "M", "M", "M", "M", "M", "M", "M"); this format


567611 

15 

appu

Oracle Learning Management 

HPHC Learner Self Service 

N/A 

General Role 

OPEN 

APPROVED: da 




567611 

15 

appu

Oracle Learning Management 

HPHC Learner Self Service 

N/A 

General VDI Role 

OPEN 

APPROVED: ss 


i have 100 data like this in txt format.please help me

What I have tried:

public System.Data.DataTable ExportToExcel()
        {
            System.Data.DataTable table = new System.Data.DataTable();
            table.Columns.Add("RequestID", typeof(int));
            table.Columns.Add("Age", typeof(int));
            table.Columns.Add("User Name ", typeof(string));
            table.Columns.Add(" Target", typeof(string));
            table.Columns.Add(" Resource", typeof(string));
            table.Columns.Add(" Atribute value", typeof(string));
            table.Columns.Add("Role", typeof(string));
            table.Columns.Add("Status", typeof(string));
            table.Columns.Add("Approver Status", typeof(string));

         

            table.Rows.Add(1, 2, "M", "M", "M", "M", "M", "M", "M"); //example i have 100 data like this in .txt format.please help me
        
            return table;
        }
Posted
Updated 6-Aug-21 6:15am
v2
Comments
Richard MacCutchan 6-Aug-21 12:05pm    
Use a loop to repeat the Add method however many times you need.

1 solution

1) Create a loop.
2) Loop while there are still sets of data in the file.
2.1) Read one set of data from the file.
2.2) Use the Add method to add that set of data to the DataTable.
3) After the loop, its done.
 
Share this answer
 
Comments
[no name] 6-Aug-21 15:02pm    
How that loop would be? I have a .txt file and its having line by line data...that part os confusing me
OriginalGriff 6-Aug-21 15:05pm    
You know how to read text from a file, I assume?
[no name] 6-Aug-21 15:19pm    
yes but each row i need to get 5datas with comma.how can i do? 5 set 1 record.how will i divid
OriginalGriff 6-Aug-21 16:17pm    
So, you can read each line. Fine - that's your loop outlined!
Now look at your data and see what goes where.
[no name] 6-Aug-21 16:53pm    
i have a list having 1000 data now.i need to add each 9 values with comma to table.Rows.Add(1, 2, "M", "M", "M", "M", "M", "M", "M") how can i do that? and my list staring index value is " " how can i remove ?(its not a null "   " is not going when i remove).i need to remove index 0 and take each 9 values with comma

I need to remove index 0 and take each 9 record and add  like table.Rows.Add(1, 2, "M", "M", "M", "M", "M", "M", "M")

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