Search code examples
c#c#-4.0epplusepplus-4

EPPlus to write Dictionary<int, int[]> To Cells


I have a Dictionary in my syntax, that I want to write the results of this starting with cell A2 to Excel. At the most it will only be 50 rows. How is this done in epplus?


Solution

  • I use console application and latest EPPlus version. The first, you need to create a file Sample.xlsx in C disk. Hope to help, my friend!

    public static void WriteExcel()
            {
                Dictionary<int, int> dictionary =
                new Dictionary<int, int>();
    
                for (int i = 1; i <= 100; i++)
                {
                    dictionary.Add(i, i);
                }   
    
                var filePath = @"C:/Sample.xlsx";             
                var file = new FileInfo(filePath);
    
                using (var package = new ExcelPackage(file))
                {
                    ExcelWorksheet worksheet = package.Workbook.Worksheets["Sheet1"];
                    int k = 0;
                    foreach (var item in dictionary.Take(50))
                    {
                        int cell = k + 2;
                        worksheet.Cells["A" + cell].Value = item.Value;
                        k++;
                    }
                    package.Save();
                }
            }