Introduction to ASP. NET MVC programming--excel upload

Source: Internet
Author: User
Tags save file syslog

Reference blog: Using file uploads under ASP. NET MVC

Reference blog: Npoi User manual

Reference blog: ASP. NET MVC uses Npoi import to export Excel

Reference blog: C # npoi Import and Export Excel documents compatible xlsx, XLS

File Upload code block

        #regionExcel upload/// <summary>        ///Excel Upload/// </summary>        /// <param name= "FileData" ></param>        /// <returns></returns>[Acceptverbs (httpverbs.post)] Publicjsonresult excelupload (httppostedfilebase fileData) {json. Httpresult result=NewJSON.            Httpresult (); if(FileData = =NULL) {result.state=JSON.                ResultType.error.ToString (); Result.message=""; returnJson (New{result}, Jsonrequestbehavior.allowget); }            Try            {                //save path after file upload                stringFilePath = Request.applicationpath +"upload/excel/"; if(Createfilepath (FilePath)) {stringFileName = Path.getfilename (filedata.filename);//Original file name                    stringFileExtension = Path.getextension (fileName);//file name extension                    stringSavename = Guid.NewGuid (). ToString ("N") + fileextension;//Save file name                    stringExcelpath = FilePath +Savename; Filedata.saveas (FilePath+savename); DataTable DT=Getexceldata (Excelpath); BOOLRe =BLL.                    productmanager.adddatatable (DT); Common. Syslog.insertlistlog (Common. Syslog.product_module, Common. syslog.operate_insert_list, dt.                    Rows.Count); Result.state=JSON.                    ResultType.success.ToString (); Result.message=JSON.                resultmessage.success; }                        }            Catch(Exception ex) {result.state=JSON.                ResultType.error.ToString (); Result.message=Ex.            Message; }            returnJson (New{result}, Jsonrequestbehavior.allowget); }        #endregion

Build a DataTable code block

        #regionGet Excel DataTable/// <summary>        ///get Excel DataTable/// </summary>        /// <param name= "FilePath" ></param>        /// <returns></returns>        PrivateDataTable Getexceldata (stringFilePath) {DataTable dt=NewDataTable ();            Iworkbook workbook; stringFileext =path.getextension (FilePath).            ToLower (); using(FileStream fs =NewFileStream (FilePath, FileMode.Open, FileAccess.Read)) {                //xssfworkbook for xlsx format, hssfworkbook for XLS format                if(Fileext = =". xlsx") {Workbook=NewXssfworkbook (FS); }                Else if(Fileext = =". xls") {Workbook=NewHssfworkbook (FS); }                Else                {                    return NULL; }} isheet Sheet= Workbook. Getsheetat (0); IRow HeaderRow= Sheet. GetRow (sheet. Firstrownum);//first behavior header row/***************************************/returnDT; }        #endregion

Introduction to ASP. NET MVC programming--excel upload

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.