Submit JSON data as a form when you export Excel

Source: Internet
Author: User
Tags save file

Today when writing a project to write an Excel export, began to want to use AJAX request background export, but found that Ajax will have a return value, and Ajax can not directly output files, and the background of the Excel export method has been packaged, not easy to modify.

Instead of submitting the form, but form submission, table paging with jquerytable, I need some jquerytable some parameters to the background, but this data is already JSON data, if I put in input directly into the background in parsing parameters will be very troublesome , so you want to convert the JSON data into a form submission.

Js

//Exportfunction Exportexcel () {varURL ="List.aspx?method=exportinfo"; $("#exportForm"). attr ("Action", URL); //Clear Form$("#exportForm"). HTML (""); //Convert JSON data to form form$.each (tabledata, function (i, obj) {$ ("#exportForm"). Append ("<input type= ' hidden ' name= '"+ Obj.name +"' value= '"+ Obj.value +"'/>");    }); Exportform.submit ();}

Foreground effect

Backstage (here I can directly use the starting paging data without having to do too much processing (just the total number of fixed page displays and the current page))

         Public voidExportinfo () {//need to assign values to foreground parametersClassparameter param =NewClassparameter (); Param.idisplaylength=int. Parse (request.form["Idisplaylength"]); Param.idisplaystart=int. Parse (request.form["Idisplaystart"]); Param.icolumns=int. Parse (request.form["Icolumns"]); Param.isortingcols=int. Parse (request.form["Isortingcols"]); Param.scolumns= request.form["Scolumns"]; //Query CriteriaParam. Selectstr = request.form["Selectstr"]; Param. Specialty=string. IsNullOrEmpty (request.form["Specialty"]) ? Guid.Empty:NewGuid (request.form["Specialty"]); Param. StartTime= request.form["StartTime"]; Param. State=string. IsNullOrEmpty (request.form[" State"]) ? Commonconts.intzero:int. Parse (request.form[" State"]); Param.            Getorderexpression (Request); Datatableresult<List<P_ClassInfoVO>> result =p_classmanage.page (param); Export (result.aadata);//Export Method}

Export method

  /// <summary>        ///Export Excel/// </summary>        /// <param name= "classlist" ></param>        Private voidExport (list<p_classinfovo>classlist) {            //whether the interpretation is empty            if(Classlist.count = =0)            {                return; }            Try            {                //Instantiate a workbookWorkbook Workbook =NewWorkbook (); //Open the template (on the server)                stringPath = System.Web.HttpContext.Current.Server.MapPath ("~"); Path= path. Substring (0, Path. LastIndexOf ("\\")); Path+=@"\file\exceltemplate\ class Information sheet. xlsx"; Workbook.                Open (path); introws =Classlist.count; intindex =4; inti =1; //Get a worksheetWorksheet master = workbook. worksheets[0]; Workbook. Worksheets.add ("class Information"); Worksheet sheet= Workbook. worksheets["class Information"]; Sheet.                Copy (master); foreach(P_classinfovo Iteminchclasslist) {Cells Cells=sheet.                    Cells; //fill in the table contentscells["A"+ Index. ToString ()]. Putvalue (i++); cells["B"+index. ToString ()]. Putvalue (item.                    C_name); cells["C"+index. ToString ()]. Putvalue (item.                    C_code); cells["D"+index. ToString ()]. Putvalue (item.                    Sp_name); cells["E"+index. ToString ()]. Putvalue (item.                    C_startclasstime); cells["F"+index. ToString ()]. Putvalue (item.                    C_closeclasstime); cells["G"+index. ToString ()]. Putvalue (item.                    C_studylength); cells["H"+index. ToString ()]. Putvalue (item.                    T_name); cells["I"+index. ToString ()]. Putvalue (item.                    C_STATESTR); cells["J"+index. ToString ()]. Putvalue (item.                    C_other); Index++; } workbook. Worksheets.removeat (0); Random Rd=NewRandom (); stringFileName ="class Information"+ DateTime.Now.ToString ("YYYYMMDDHHMMSS") + Rd. Next (Ten, -) +". xls"; //IE requires code (browser kernel)                if(Request.UserAgent.ToLower (). Contains ("Trident"))                {                    //Save FileFileName =Httputility.urlencode (FileName, System.Text.Encoding.UTF8); } workbook. Save (FileName, fileformattype.excel2003, Savetype.openinexcel, System.Web.HttpContext.Current.Response,                SYSTEM.TEXT.ENCODING.UTF8);            HttpContext.Current.ApplicationInstance.CompleteRequest (); }            Catch            {            }            finally{GC.            Collect (); }        }

This allows you to export the

Submit JSON data as a form when you export Excel

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.