MVC4.0系统开发新手历程(三)---数据导入计算

                                                                                    数据导入计算

          数据的重要意义就不需要我多说了吧,本项目中的数据来源是来自荆艺系统(熟悉物流报关的朋友应该知道)导出的一张Excel,包含了一段时间内整个公司所有销售的全部记录,保密级别那是杠杠的,下面开搞!!

          首先在Controllers文件夹下面添加一个控制器 命名DataInfoAndCalController.cs 在他默认action上面右击添加视图,就会在Views文件夹下面创建对应的视图,这样可以避免拼写错误。

        这个View个页面很简单,引用了”_Layout.cshtml“页面后只有两个选择年月的下拉框,一个文件域,两个按钮,一个导入一个计算,剩下的就是处理了

数据提交部分给大家参考下 

<form onsubmit="return funs();" enctype="multipart/form-data" method="post" action="/DataInfoAndCal/index/">
                                        <div class="table_box">
                                            <table cellpadding="0" cellspacing="0" border="0" style="padding: 10px;" width="100%">
                                                <tr>
                                                    <td width="40%" height="40" align="right" style="font-size: 14px;">选择导入月份:</td>
                                                    <td height="40" width="60%">

                                                        <select id="selYear" name="selYear" class="input_xz">
                                                            <option value="2014">2014</option>
                                                            <option value="2013" selected="selected">2013</option>
                                                            <option value="2012">2012</option>
                                                        </select>
                                                        <select id="selMon" name="selMon" class="input_xz">
                                                            <option value="01">1</option>
                                                            <option value="02">2</option>
                                                            <option value="03">3</option>
                                                            <option value="04">4</option>
                                                            <option value="05">5</option>
                                                            <option value="06">6</option>
                                                            <option value="07">7</option>
                                                            <option value="08">8</option>
                                                            <option value="09">9</option>
                                                            <option value="10">10</option>
                                                            <option value="11">11</option>
                                                            <option value="12">12</option>
                                                        </select>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td height="40" align="right" style="font-size: 14px;">选择导入文件:</td>
                                                    <td>
                                                        <input id="fileName" type="file" name="fileName" />
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td></td>
                                                    <td height="70">

                                                        <input id="info" type="submit" value="导&nbsp;&nbsp;入" class="bt_bg" />&nbsp;&nbsp;
                                                    <input id="cal" type="button" value="计&nbsp;&nbsp;算" class="bt_bg" />
                                                    </td>
                                                </tr>
                                            </table>
                                        </div>
                                    </form>
数据导入 --View         
  function funs() {
            var msg = $("#selYear option:selected").val() + '' + $("#selMon option:selected").val() + '';
            if (confirm("确定要导入'" + msg + "'的数据吗?")) {
                return true;
            }
            return false;
        }
JS funs

   下面来看Index视图,注意一点 <from>表单里面的html元素的值可以通过相同的id接受到这个标签的Value值,所以我的导入的action

 [HttpPost]
        public ActionResult index(string selYear, string selMon)
        {
            date = selYear + selMon;
            GlobalValue.CurrentDate = date;
            HttpPostedFileBase fb = Request.Files[0];
            string fileName = fb.FileName;  //获取到文件名  xx.xls
            if (!string.IsNullOrEmpty(fileName))
            {
                string extenSion = Path.GetExtension(fileName);  //文件后缀 .xls 
                if (extenSion.Equals(".xls") || extenSion.Equals(".xlsx"))
                {
                    string suffix = ConfigurationManager.AppSettings["FileNameSuffix"].ToString();
                    string ExcelName = date + suffix + extenSion;
                    //一:上传服务器
                    string pathEndName = string.Empty;
                    string savePath = string.Empty;
                    try
                    {
                        savePath = ConfigurationManager.AppSettings["FilePath"].ToString();
                        if (!Directory.Exists(savePath))
                        {
                            Directory.CreateDirectory(savePath);
                        }
                        //确定要写入的xls文件是否存在 不存在则复制并重新命名
                        string pathStart = Server.MapPath(@"~/ResourceData/Model.xls");
                        savePath += "\" + ExcelName;
                        try
                        {
                            System.IO.File.Copy(pathStart, savePath, true);

                        }
                        catch (Exception ex)
                        {
                            throw ex;
                        }

                        //将文件流写入指定的文件
                        fb.SaveAs(savePath);
                        pathEndName = savePath;
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }

                    //二:将Excel表格转换为DadaSet数据集
                    dsCal = GetDataSet(pathEndName);
                    //将五张基本表存入数据库中                   
                    if (FillDataToDB(date, savePath))
                    {
                        ViewBag.returnMsg = "数据导入成功!请计算";
                    }
                    else
                    {
                        ViewBag.returnMsg = "请确认导入Excel表格格式是否规范!";
                    }
                }
                else
                {
                    ViewBag.returnMsg = "请选择Excel文件!";
                }
            }
            else
            {
                ViewBag.returnMsg = "您还没有没有选择要导入的Excel文件!";
            }

            return View();
        }
数据导入 Action

   具体的 导入方法和逻辑在这里就不再写了,因为以后还要提供下载的功能,所以这里把这个Excel文件上传到了服务器上面了,因为数据两比较大,开始我是考虑的是用Linq To Sql 入库的,但是处理大批量的数据貌似不是很给力,所以最后选择了SqlBulkCopy来批量导入,效率还是不错的,我测过了,过万的数据只要两秒多点,要知道这可是导入金的不同的数据表的,把这一段贴出来鉴赏下

  

 #region 统一将DataSet中的数据存入数据库
        private bool DataSetToDB()
        {
            bool isSuc = false;
            using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["GM360_RewardContext"].ToString()))
            {
                if (conn.State == ConnectionState.Closed)
                {
                    conn.Open();
                }
                SqlTransaction tran = conn.BeginTransaction();
                //同月份是否有数据 有数据则删除
                DeleteSameMonthData(date);
                SqlBulkCopy sbKc = null;
                try
                {
                    for (int i = 0; i < dsDate.Tables.Count; i++)
                    {
                        sbKc = new SqlBulkCopy(conn, SqlBulkCopyOptions.Default, tran);

                        sbKc.BatchSize = 800;
                        sbKc.DestinationTableName = dsDate.Tables[i].TableName;
                        if (dsDate.Tables[i].TableName == "AdvanceExtendFees")
                        {
                            sbKc.DestinationTableName = "AdvanceExtendFee";
                        }
                        int count = dsDate.Tables[i].Columns.Count;
                        for (int k = 0; k < count; k++)
                        {
                            sbKc.ColumnMappings.Add(k, k);
                        }
                        sbKc.WriteToServer(dsDate.Tables[i]);
                    }
                    isSuc = true;
                    tran.Commit();
                }
                catch (Exception ex)
                {
                    isSuc = false;
                    tran.Rollback();
                }
                finally
                {
                    conn.Dispose();
                    conn.Close();
                }
            }
            return isSuc;
        }
        #endregion
批量入库

       这里要操作的是一个DataSet,放进了内存里面了 ,所以不用想这个方法里面传参了,删除同月数据是写了个存储过程,运用了EF的Linq To SQL 做删除效率还是蛮可以的。好了数据成功入库

     由于这里是Post提交过来的文件,所以有几点需要注意 <form>标签要添加 ” enctype="multipart/form-data" “属性数据才不会乱码,返回页面的时候刷新了页面会造成页面年月重新加载丢失导入条件,所以在这里添加两句

  

  ViewBag.selYear = (selYear == "") ? "0" : selYear;
            ViewBag.selMon = (selMon == "") ? "0" : selMon;

View页面的最底部添加

  <script type="text/javascript">
   var yearReturn = '@ViewBag.selYear';
        if (yearReturn != null && yearReturn != '') {
            $("#selYear option[value='" + yearReturn + "']").attr("selected", true);
        }
        else {
            var year = '@ViewBag.year';
            $("#selYear option[value='" + year + "']").attr("selected", true);
        }

        var monthReturn = '@ViewBag.selMon';
        if (monthReturn != null && monthReturn != '') {
            $("#selMon option[value='" + monthReturn + "']").attr("selected", true);
        }
        else {
            var month = '@ViewBag.month';
            $("#selMon option[value='" + month + "']").attr("selected", true);
        }
 </script>

 这里ViewBag.year 和 ViewBag.month是开始加载页面初始化的,而另外两个则是记录查询条件的

 数据导入到这里也算告一段落了 计算要说的比这个还要多就留下下一篇里面说吧,篇幅实在是有限,所以就写了部分我觉得比较核心的,还是不错的哈哈

    

原文地址:https://www.cnblogs.com/a164266729/p/3277472.html