usingMongodb.bson;usingNewtonsoft.Json.Linq;usingNpoi. HSSF. Usermodel;usingNpoi. Ss. Usermodel;usingSystem;usingSystem.Collections;usingSystem.Collections.Generic;usingSystem.Data;usingSystem.IO;usingSystem.Linq;usingsystem.web;usingSystem.Web.Http;usingSYSTEM.WEB.MVC;usingZluo.CMember.Entity;usingZluo.CMember.Interface;usingZluo.CMember.Service;usingZluo.CMember.Web.RequestParam;usingZluo.CMember.Web.RequestParam.Account;usingZluo.CMember.Web.RequestParam.Order;usingZluo.common;usingZluo.Common.CMember;usingzluo.sessioncached; Public voidCreateexport () {Try { stringFileName ="Errorsongids.xls";//file name stringFilePath = Path.Combine (Httpruntime.appdomainapppath,"Upload") +"/"+FileName; //1. Detect if a folder exists and create a folder if it does not exist stringDirectoryName = Path.getdirectoryname (FilePath);if(!directory.exists (directoryname)) {directory.createdirectory (directoryname);} //2. Resolve the cell header, set the Chinese name of the unit headHssfworkbook Workbook =NewHssfworkbook (); // WorkbooksIsheet sheet = workbook. Createsheet ("failed Songs"); //Work TableIRow row = sheet. CreateRow (0); Row. Createcell (0). Setcellvalue ("Original ID"); Row. Createcell (1). Setcellvalue ("Song name"); Row. Createcell (2). Setcellvalue ("Name of singer"); Row. Createcell (3). Setcellvalue ("Reason for failure"); //_songlistservice.geterrorexcel (UID);Bsonarray array =NULL; intRowIndex =1; bsondocument BD =NULL; if(Array! =NULL&& Array. Count >0) { for(inti =0; I < array. Count; i++) {IRow rowtmp=sheet. CreateRow (RowIndex); BD=(bsondocument) array[i]; if(BD! =NULL) {Rowtmp.createcell (0). Setcellvalue (Bd. GetValue ("SourceID"). ToString ()); Rowtmp.createcell (1). Setcellvalue (Bd. GetValue ("Songname"). ToString ()); Rowtmp.createcell (2). Setcellvalue (Bd. GetValue ("Singername"). ToString ()); Rowtmp.createcell (3). Setcellvalue (string. IsNullOrEmpty (Bd. GetValue ("Errorreason"). ToString ())?"": Bd. GetValue ("Errorreason"). ToString ()); RowIndex++; } } } //4. Generating FilesFileStream file =NewFileStream (FilePath, FileMode.Create); Workbook. Write (file); File. Close (); Response.appendheader ("content-disposition","Attachment;filename=errorsongids.xls"); Response.ContentType="Application/ms-excel"; Response.WriteFile (FilePath); Response.Flush (); Response.End (); } Catch(Exception ex) {Throwex;} }
C # Build Excel save to server side and download