Excel data Import Database instance (NPOI)

Source: Internet
Author: User
Tags import database

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

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.