• 周日. 7月 3rd, 2022

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

c# 导入excle数据

admin

11月 28, 2021
//热力图
        [AcceptVerbs("Post")]
        [LogInfo("导入监测点位", true)]
        public HttpResponseMessage ImportPoint([FromUri] int structId)
        {
            try
            {
                HttpPostedFile file = HttpContext.Current.Request.Files[0];

                string timenow = DateTime.Now.ToString("yyyyMMddHHmmssfff");
                string path = HttpContext.Current.Server.MapPath("~/");
                //检查和创建存储目录
                if (Directory.Exists(path + "\rpt") == false)
                {
                    Directory.CreateDirectory(path + "\rpt");
                }
                if (Directory.Exists(path + "\rpt\" + timenow) == false)
                {
                    Directory.CreateDirectory(path + "\rpt\" + timenow);
                }
                string realPath = "rpt\" + timenow + "\" + file.FileName;
                path += realPath;
                file.SaveAs(path);

                Aspose.Cells.Workbook wk = new Aspose.Cells.Workbook(path);
                Aspose.Cells.Cells cells = wk.Worksheets[0].Cells;
                System.Data.DataTable dt = cells.ExportDataTable(1, 0, cells.MaxDataRow, cells.MaxColumn);//noneTitle

                string cmdText = "";
                string points = "";

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    points += "'" + dt.Rows[i][0].ToString() + "',";
                }


                if (points != "")
                {
                    points = points.Substring(0, points.Length - 1);
                    cmdText = string.Format("Select Count(*) From T_HOTSPOT_MAIN_INFO a Where a.structId = '{0}' and a.Point in ({1})", structId, points);

                    string count = SqlHelper.ExecuteScalarText(cmdText).ToString();
                    if (count != "0")
                    {
                        return Request.CreateResponse(HttpStatusCode.Accepted, "数据中包含已上传的监测点位,请检查后再提交!");
                    }
                }


                string time = DateTime.Now.ToString();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    string point = dt.Rows[i][0].ToString();
                    string x = dt.Rows[i][1].ToString();
                    string y = dt.Rows[i][2].ToString();
                    cmdText = string.Format("Insert Into T_HOTSPOT_MAIN_INFO(point,x,y,structId,createtime) Values('{0}','{1}','{2}','{3}','{4}')", point, x, y, structId, time);
                    SqlHelper.ExecteNonQueryText(cmdText);
                }

                return Request.CreateResponse(HttpStatusCode.OK, "导入成功!");
            }
            catch (Exception ex)
            {
                return Request.CreateResponse(HttpStatusCode.InternalServerError, "导入失败!" + ex.Message + ex.StackTrace);
            }
        }

发表评论

您的电子邮箱地址不会被公开。