码迷,mamicode.com
首页 > 数据库 > 详细

EXCEL数据导入数据库实例(NPOI)

时间:2014-06-20 18:34:19      阅读:390      评论:0      收藏:0      [点我收藏+]

标签:class   code   java   http   ext   com   

Default.aspx 页面代码:

引用了:    <script src="../../js/jquery.easyui.min.js" type="text/javascript"></script>

 

 <form id="fileForm" runat="server" enctype="multipart/form-data" method="post">

 导入EXCEL:<asp:fileupload runat="server" id="upFile"></asp:fileupload><a href="javascript:;"
                class="btn btn-primary" id="btnExelre">导入</a>

</form>

 

js代码:

  <script type="text/javascript">
        $(function() {
            //提交导入
            $("#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 页面:

    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 = ""; //返回的提示信息
        string rType = ""; //返回的状态
        int obj = 0;
        HttpPostedFile file = context.Request.Files["upFile"]; //上传文件的控件
        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();//身份证号
                        model.sno = dtImport.Rows[i][1].ToString();//学号
                        model.sname = dtImport.Rows[i][2].ToString();//姓名      
                        model.ssex = dtImport.Rows[i][3].ToString();//性别
                        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 = "恭喜你,数据导入成功!";
                        rType = "success";
                    }
                    else
                    {
                        strMsg = "很抱歉,数据导入失败!";
                        rType = "error";
                    }
                }
                else
                {
                    strMsg = "很抱歉,操作失败!";
                    rType = "error";
                }
            }
            else
            {
                strMsg = "很抱歉,上传文件为空,操作失败!";
                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);
    }

 //最后提示:

第一次使用的时候遇到的问题

使用的npoi不支持excel2003以上的版本,所以导入的时候。要选择03的excel数据,

解决办法:使用新的npoi 或者 把excel文件另存为03的

EXCEL数据导入数据库实例(NPOI),布布扣,bubuko.com

EXCEL数据导入数据库实例(NPOI)

标签:class   code   java   http   ext   com   

原文地址:http://www.cnblogs.com/vanteking/p/3796366.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!