Default.aspx page code:
Referenced by: <script src= ". /.. /js/jquery.easyui.min.js "type=" Text/javascript "></script>
<form id= "Fileform" runat= "Server" enctype= "Multipart/form-data" method= "POST" >
Import excel:<asp:fileupload runat= "server" id= "upfile" ></asp:fileupload><a href= "javascript:;"
class= "btn btn-primary" id= "Btnexelre" > Import </a>
</form>
JS Code:
<script type= "Text/javascript" >
$ (function () {
Submit Import
$ ("#btnExelre"). Click (function () {
$ ("#fileForm"). Form ("Submit", {
URL: ".. /handler/diryexcel.ashx ",
Success:function (rdata) {
Console.log (Rdata);
Rdata = $.parsejson (rdata);
if (Rdata.result = = "Success") {
alert (rdata.message);
Window.location.reload ();
} else if (Rdata.result = = "Error") {
alert (rdata.message);
}
}
});
});
});
</script>
Diryexcel.ashx page:
public void ProcessRequest (HttpContext context) {
Context. Response.ContentType = "Text/plain";
Context. Response.ContentType = "Text/plain";
Systembll._zaizhi FILEBLL = new Systembll._zaizhi ();
Context. Response.ContentType = "Text/plain";
String strMsg = ""; Returned prompt information
String rtype = ""; The returned state
int obj = 0;
Httppostedfile file = context. request.files["Upfile"]; Controls for uploading files
Try
{
if (file! = null && file. InputStream! = null && file. ContentLength > 0)
{
System.Data.DataTable dtimport = CommonLib.ExcelHelper1.ImportExceltoDt (file. InputStream);
if (dtimport! = null)
{
for (int i = 0; i < DtImport.Rows.Count; i++)
{
Systemmodel._zaizhi model = new Systemmodel._zaizhi ();
Model.idcard = dtimport.rows[i][0]. ToString ();//ID number
Model.sno = dtimport.rows[i][1]. ToString ();//Study No.
Model.sname = dtimport.rows[i][2]. ToString ();//Name
Model.ssex = dtimport.rows[i][3]. ToString ();//gender
Model.other = Dtimport.rows[i][4]. ToString ();//
Model.szcode = dtimport.rows[i][5]. ToString ();//
Model.schoole = Dtimport.rows[i][6]. ToString ();//
Model.szname = Dtimport.rows[i][7]. ToString ();//
Model.other1 = DateTime.Now.ToString ();
obj + = Filebll.add (model);
}
if (obj = = dtImport.Rows.Count)
{
STRMSG = "Congratulations, data import success!";
Rtype = "Success";
}
Else
{
STRMSG = "Sorry, data import failed!";
Rtype = "Error";
}
}
Else
{
STRMSG = "Sorry, the operation failed!";
Rtype = "Error";
}
}
Else
{
STRMSG = "Sorry, the upload file is empty, the operation failed!";
Rtype = "Error";
}
}
catch (Exception ex)
{
STRMSG = "Error:" + ex. Message + "<br>" + ex. Source + ex. StackTrace;
Rtype = "Error";
}
String resultstring = "{\" result\ ": \" "+ Rtype +" \ ", \" message\ ": \" "+ STRMSG +" \ "}";
Context. Response.Write (resultstring);
}
Last hint:
Problems encountered during the first use
The use of Npoi does not support excel2003 more than the version, so when importing. To select 03 of Excel data,
WORKAROUND: Use the new Npoi or save the Excel file as 03