One,. From NuGet, reference the Microsoft.Office.Interop.Excel class library.
Method One, open a execl table and save it first
Application xApp =NULL; Workbook Xbook=NULL; Try { //1. Create a Applicaton objectXAPP =Newapplication (); Xapp.visible=true; Workbooks WBS=Xapp.workbooks; Workbook WB=WBS. ADD (Missing.Value); Worksheet Xsheet= (Worksheet) WB. worksheets["Sheet1"]; Range R= Xsheet.get_range ("A1","N1"); Object[] Objheader = { "Serial Number","User name","Phone number","whether to spend","Recharge Amount","Recharge Times","Purchase Service amount","number of services purchased","Purchase Copyright Amount","number of copyrights purchased","Voucher Amount", "Gift Amount","Total Amount","account Balance" }; introw =2;//the first line is the title for(inti =0; I < array. Length; i++) { varstr =Array[i]; varList = Tjsonresult.deserializeobject<list<userinfoanalysisentity>>(str); foreach(varEntityinchlist) { if(Entity. Cost = ="is a") {XSheet.Cells.Font.ColorIndex=3; } Else{XSheet.Cells.Font.ColorIndex=1; } Xsheet.cells[row,1] =entity. Id; Xsheet.cells[row,2] =entity. UserName; Xsheet.cells[row,3] =entity. Phone; Xsheet.cells[row,4] =entity. cost; Xsheet.cells[row,5] =entity. Recharge; Xsheet.cells[row,6] =entity. Rechargenum; Xsheet.cells[row,7] =entity. Buyservices; Xsheet.cells[row,8] =entity. Buyservicesnum; Xsheet.cells[row,9] =entity. Buycopyright; Xsheet.cells[row,Ten] =entity. Buycopyrightnum; Xsheet.cells[row, One] =entity. Coupon; Xsheet.cells[row, A] =entity. Givemoney; Xsheet.cells[row, -] =entity. Totalmoney; Xsheet.cells[row, -] =entity. Balance; Row++;//represents a line number} }
var filepath = AppDomain.CurrentDomain.BaseDirectory + "test123.xlsx";
Xsheet.saveas (filepath, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value, Missing.Value);
More operations: https://www.cnblogs.com/junshijie/p/5292087.html
. Net Operations Excel Table