Adsence750x90

Saturday, July 4, 2020

Export to Excel using OpenXml


Some situations we need to export data in Excel format. I had such situation couple of years back and I created a repository in my bitbucket account.

I thought it may be help for some one who is looking for same kind of solution while programming.

Here is the repository url
https://bitbucket.org/makhaai/exporttoexcel/src/master/ 


for Git Clone
git clone https://makhaai@bitbucket.org/makhaai/exporttoexcel.git


This includes there project. One model Library, One Service Library and a MVC project. 












Basically we can plug  List data in to it and download it as Excel document 


   public FileContentResult Download() {

            List> listOfSheets = new List>() {
                    new ExcelSheet() {
                     Records =Data.GetProducts(),
                     SheetName = "ProductList 1"
                },
                 new ExcelSheet() {
                     Records =Data.GetProducts(),
                     SheetName = "ProductList 2"
                },
            };

            var excelBytes = _iExcelWriterService.GetExcelInBytes(listOfSheets);
            var fileName = string.Format("{0}_{1}.xlsx",
                                      "Products_", DateTime.Now.ToString("MMddyyyyHHmmssfff"));
            return excelBytes != null ? File(excelBytes,
                                             "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
                                              fileName) : null;
        }