1: Install the Epplus nuget package
Implementing the Source code:
1 usingOfficeopenxml;2 usingSystem;3 usingSystem.Collections.Generic;4 usingSystem.IO;5 usingSystem.Linq;6 7 namespaceBigBeer.Core.Excel8 {9 Public classExcelTen { One /// <summary> A ///for more than 2003 versions only - /// </summary> - /// <param name= "Data" >data that needs to be stored</param> the /// <param name= "path" >Storage Address</param> - /// <param name= "filename" >file name, suffix. xlsx</param> - /// <param name= "WorksheetName" >under the Excel document named</param> - Public Static voidSaveexcel (ilist<Object> Data,stringPath =NULL,stringfilename =NULL,stringWorksheetName =NULL) + { - intCount = data[0]. ToString (). Split (','). Count (); + stringSwebrootfolder ="d://excel/"; A if(!string. IsNullOrEmpty (path)) Swebrootfolder =path; at if(!directory.exists (swebrootfolder)) - { - directory.createdirectory (swebrootfolder); - } - stringsFileName = $"{guid.newguid ()}.xlsx"; - if(!string. IsNullOrEmpty (filename)) sFileName = $"{filename}.xlsx"; inFileInfo file =NewFileInfo (Path.Combine (Swebrootfolder, sFileName)); - stringWorksheetName ="sheet"; to if(!string. IsNullOrEmpty (worksheetname)) WorksheetName =WorksheetName; + - using(Excelpackage package =Newexcelpackage (file)) the { * //Add Worksheet $excelworksheet worksheet =Package . WORKBOOK.WORKSHEETS.ADD (worksheetname);Panax Notoginseng //Add Header - for(inti =0; I < count; i++) the { + varKey = data. ToList () [i]. ToString (). Split (',') [i]. Split ('=')[0]. Replace ("{",""). Replace ("}",""); AWorksheet. cells[1, i +1]. Value =key; the } + //Add Value - for(inti =0; I < data. Count; i++) $ { $ varList =Data[i]; - varShu = i +2; - for(intt =0; T < count; t++) the { - varc = Convert.tochar ('A'+t);Wuyi varValue = list. ToString (). Split (',') [T]. Split ('=')[1]. Replace ("{",""). Replace ("}",""); theWorksheet. cells[$"{C}{shu}"]. Value =value; - } Wu } - Package . Save (); About GC. Collect (); $ } - } - } -}
3: How to use:
1ilist<Object> Good =Newlist<Object>();2 3 vartemp =result. ToList (); Result for the queried data4 5 for(inti =0; I < temp. Count (); i++)6 {7 Good.add (Temp[i]);8 }9Excel.saveexcel (good);
C#core using Epplus to access Excel files