예제 #1
0
        public ActionResult OutFile(string sql)
        {
            try
            {
                if (string.IsNullOrEmpty(sql))
                {
                    return(Json(new RequestResultDto()
                    {
                        Msg = "sql语句不能为空", Ret = -1
                    }));
                }

                var    dt   = QueryHelper.ExecSql("MyP", sql).Tables[0];
                string path = @"E:\";
                var    msg  = "导出成功,导出路径:" + path;
                NpoiSdk.OutFile(dt, path, ref msg);
                return(Json(new RequestResultDto()
                {
                    Msg = msg, Ret = 0
                }));
            }
            catch (Exception ex)
            {
                return(Json(new RequestResultDto()
                {
                    Msg = ex.Message.Replace("\r", "\\r").Replace("\n", "\\n").Replace("\'", "\\'").Replace("\"", "\\\""),
                    Ret = -1
                }));
            }
        }
예제 #2
0
        public ActionResult Index(HttpPostedFileBase filebase)
        {
            try
            {
                HttpPostedFileBase file = Request.Files["files"];
                var error    = "";
                var savePath = NpoiSdk.GetSavePath(file, out error);
                if (string.IsNullOrWhiteSpace(savePath))
                {
                    ViewBag.error = error;
                }
                DataTable table = NpoiSdk.ExcelToDataTable(savePath);


                //引用事务机制,出错时,事物回滚
                using (TransactionScope transaction = new TransactionScope())
                {
                    for (int i = 0; i < table.Rows.Count; i++)
                    {
                        string msg   = table.Rows[i][0].ToString();
                        var    ret   = table.Rows[i][1];
                        var    model = new Log()
                        {
                            Ret        = Convert.ToInt32(ret),
                            Msg        = msg,
                            CreateTime = DateTime.Now
                        };
                        _log.AddLog(model);
                        //此处写录入数据库代码;
                    }
                    transaction.Complete();
                }
                ViewBag.error = "导入成功";
            }
            catch (Exception e)
            {
                ViewBag.error = "导入失败:" + e.Message;
            }
            System.Threading.Thread.Sleep(2000);
            return(View());
        }