First, refer to the Npoi. dll
. dll: Link: Https://pan.baidu.com/s/1K4cCb4qpkIfDoH1fZnNvVQ Password: I9SL
There is a Excelhelper helper class, copied to the project, to the method in the Help class to pass the parameters to complete the export
Second, in view: The button jumps directly to which Controller's method (note that the method's return value type is Fileresult)
Third, the code in the Controller method is as follows:
PublicFileresult Dropfile () {stringSheetName ="Workbook 1";//Set Workbook name stringDroppath = Server.MapPath (@"\dropexcal\");//Set Save path if(! Directory.Exists (Droppath))//determine if the path exists{directory.createdirectory (droppath);//does not exist then creates } stringFileName ="Excel Data Export"+ SYSTEM.DATETIME.NOW.TOFILETIMEUTC (). ToString () +". xlsx";//set the file name after export //////////Invoke Helper class//////////DataTable CUSDT = todatatable (list);//calling the Help class to convert the List<t> type to a DataTable typeExportexcel (CUSDT, SheetName,"Customer Data", Droppath + FileName);//call the Help class to save the file in a local folder (the Help class automatically converts the table table to Excel when there are several styles to choose from) //////////Invoke Helper class////////// strings = mimemapping.getmimemapping (FileName);//Gets the download content type (MIME type). returnFile (Droppath + filename, s, path.getfilename (Droppath + filename));//Returns a browser download dialog box (download the saved local file)}
Data format conversions: 6617683 references
Clicking on the download browser will bring up the download prompt box:
MVC Browser Export Excel file (NPOI)