C#core using Epplus to access Excel files

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.