Import excel data in list data type

ankit goel 766 Reputation points
2022-11-26T10:42:18.747+00:00

I am working on a c# project where i have open a excel file using interop assembly. I am doing some operation on this file like deleting rows, columns, copying data on it but i found out that it has some performance issues as the excel file is big. So, i decided to import the whole excel data in collections data types in c# as they work faster. Can someone please guide me how to import the whole excel file data into this. Also share the code for deleting top 9 elements and last element of that list data type. I am attaching the screenshot for the excel file
264411-microsoft.jpg

Developer technologies C#
{count} votes

Accepted answer
  1. Jack J Jun 25,296 Reputation points
    2022-11-28T08:20:32.363+00:00

    @ankit goel , thanks for the feedback, based on your latest requirement, I make a code example to convert your current excel to list.

    Here is a code example you could refer to.

    public class Model  
        {  
            public BlanaceType type { get; set; }  
      
            public string Name { get; set; }  
      
            public double Amount { get; set; }  
        }  
      static void Main(string[] args)  
            {  
                Excel.Application app = new Excel.Application();  
                Excel.Workbook workbook = app.Workbooks.Open("Example.xlsx");  
                Excel.Worksheet ws= workbook.Worksheets[1];  
                int usedrowcount = ws.UsedRange.Rows.Count;  
                List<Model> models = new List<Model>();  
                for (int i = 7; i < usedrowcount; i++)  
                {  
                    string text= ws.Cells[i, 1].Text;  
                    string text1= ws.Cells[i, 2].Text;  
                    string text2 = ws.Cells[i, 3].Text;  
                    if(text1!="")  
                    {  
                        Model model = new Model();  
                        model.Name = text;  
                        model.type = BlanaceType.Debit;  
                        model.Amount = Convert.ToDouble(text1);  
                        models.Add(model);  
                    }  
                    if (text2 != "")  
                    {  
                        Model model = new Model();  
                        model.Name = text;  
                        model.type = BlanaceType.Credit;  
                        model.Amount = Convert.ToDouble(text2);  
                        models.Add(model);  
                    }  
      
                }  
                workbook.Close();  
                 
                var result = models.OrderBy(i => i.Amount);  
                foreach (var item in result)  
                {  
                    Console.WriteLine(item.Name);  
                    Console.WriteLine(item.Amount);  
                    Console.WriteLine(item.type.ToString());  
                }  
      
            }  
    

    Please note that you need to start from the 10 row instead of 7 row.

    My excel file and tested result:

    264589-image.png


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.