zoukankan      html  css  js  c++  java
  • c# 导入excle数据

    //热力图
            [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);
                }
            }
  • 相关阅读:
    redis 批量删除key
    控制台直接执行sql语句
    item2 快捷键
    mac mamp环境 PHP命令行反应缓慢解决
    composer gitlab 搭建私包
    PostgreSql命令
    maven 程序包com.sun.image.codec.jpeg
    lumen配置日志daily模式
    PHPStorm怎么修改选中的背景颜色呢?
    vim 配置文件.vimrc,高亮+自动缩进+行号+折叠+优化
  • 原文地址:https://www.cnblogs.com/lvqianqian/p/15148403.html
Copyright © 2011-2022 走看看