Import and Export excel in ASP.

Source: Internet
Author: User

Front button Request

function Out_excel (tc_id) {

$.post ("Ajax/exportexcel.aspx", {tc_id:tc_id}, function (data) {

if (Data! = "") {
window.open (data);
}

})

}

Back-end Interface processing
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Data;
Using Model;
Using BLL;
Using System.IO;

Namespace CRM system. Ajax
{
public partial class ExportExcel:System.Web.UI.Page
{
protected void Page_Load (object sender, EventArgs e)
{
int tc_id = Convert.ToInt32 (request["tc_id"]);
List<out_excel> stulist =studentsbll.getstudentsandtrainclassstudents (tc_id);

Create an object for an Excel file
Npoi. HSSF. Usermodel.hssfworkbook book = new Npoi. HSSF. Usermodel.hssfworkbook ();

Add a sheet
Npoi. Ss. Usermodel.isheet Sheet1 = Book. Createsheet ("Sheet1");

Add title
Npoi. Ss. Usermodel.irow Row1 = Sheet1. CreateRow (0);
Row1. Createcell (0). Setcellvalue ("original Campus");
Row1. Createcell (1). Setcellvalue ("original class");
Row1. Createcell (2). Setcellvalue ("current Class");
Row1. Createcell (3). Setcellvalue ("Campus Code");
Row1. Createcell (4). Setcellvalue ("serial number");
Row1. Createcell (5). Setcellvalue ("School Number");
Row1. Createcell (6). Setcellvalue ("name");
Row1. Createcell (7). Setcellvalue ("name Pinyin");
Row1. Createcell (8). Setcellvalue ("gender");
Row1. Createcell (9). Setcellvalue ("Social Security Number");
Row1. Createcell (10). Setcellvalue ("class number");
Row1. Createcell (11). Setcellvalue ("Student Status");
Row1. Createcell (12). Setcellvalue ("certified");
Row1. Createcell (13). Setcellvalue ("Education");
Row1. Createcell (14). Setcellvalue ("professional");
Row1. Createcell (15). Setcellvalue ("Graduate School");
Row1. Createcell (16). Setcellvalue ("personal phone");
Row1. Createcell (17). Setcellvalue ("Home Landline");
Row1. Createcell (18). Setcellvalue ("QQ number");
Row1. Createcell (19). Setcellvalue ("Correspondence Address");
Row1. Createcell (20). Setcellvalue ("postcode");
Row1. Createcell (21). Setcellvalue ("technical evaluation");
Row1. Createcell (22). Setcellvalue ("Class teacher evaluation");
Row1. Createcell (23). Setcellvalue ("Remarks");

for (int i = 0; i < Stulist.count; i++)
{
Npoi. Ss. Usermodel.irow sturows = Sheet1. CreateRow (i + 1);
Sturows.createcell (0). Setcellvalue (Stulist[i]. School_name);
Sturows.createcell (1). Setcellvalue (Stulist[i]. Sc_name);
Sturows.createcell (2). Setcellvalue (Stulist[i]. Tc_name);
Sturows.createcell (3). Setcellvalue (Stulist[i]. School_code);
Sturows.createcell (4). Setcellvalue (i + 1);
Sturows.createcell (5). Setcellvalue (Stulist[i]. STUDENT_NO);
Sturows.createcell (6). Setcellvalue (Stulist[i]. Student_name);
Sturows.createcell (7). Setcellvalue (Stulist[i]. Student_namespell);
Sturows.createcell (8). Setcellvalue (Stulist[i]. Student_sex);
Sturows.createcell (9). Setcellvalue (Stulist[i]. Student_identitynumber);
Sturows.createcell (10). Setcellvalue (Stulist[i]. sc_id. ToString ());
Sturows.createcell (11). Setcellvalue (Stulist[i]. Student_state);
Sturows.createcell (12). Setcellvalue (Stulist[i]. Student_exam);
Sturows.createcell (13). Setcellvalue (Stulist[i]. Student_education);
Sturows.createcell (14). Setcellvalue (Stulist[i]. Student_specialty);
Sturows.createcell (15). Setcellvalue (Stulist[i]. Student_schoolofgraduation);
Sturows.createcell (16). Setcellvalue (Stulist[i]. Student_personaltel);
Sturows.createcell (17). Setcellvalue (Stulist[i]. Student_familytel);
Sturows.createcell (18). Setcellvalue (Stulist[i]. STUDENT_QQ);
Sturows.createcell (19). Setcellvalue (Stulist[i]. student_address);
Sturows.createcell (20). Setcellvalue (Stulist[i]. Student_postcode);
Sturows.createcell (21). Setcellvalue (Stulist[i]. STUDENT_EVALUATE1);
Sturows.createcell (22). Setcellvalue (Stulist[i]. STUDENT_EVALUATE2);
Sturows.createcell (23). Setcellvalue (Stulist[i]. Student_remark);
}

FileStream fs = new FileStream (Server.MapPath (". \ \ create the folder name \ \ "+ Stulist.count +". xls "), FileMode.Create);
Book. Write (FS);
Fs. Close ();
Response.Write (".. \ \ create the folder name \ \ "+ Stulist.count +". xls ");
}
}
}


Import Npoi File
Locate the Npoi file that you imported into the bin directory and then reference

Import and Export excel in ASP.

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.