0

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?

IcyPopTarts
  • 494
  • 1
  • 12
  • 25

1 Answers1

0

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();
            }
        }
Tomato32
  • 2,145
  • 1
  • 10
  • 10