1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
| using Ascentn.Workflow.Base; using Medalsoft.Workflow.Resource.CommonUtil; using System; using System.Data; using System.Data.SqlClient; using System.IO; using System.Web; using System.Web.Http;
namespace Medalsoft.Workflow.Resource.Controllers { [RoutePrefix("api/upload")] public class UploadController : ApiController { string connstr = AgilePointUtil.CreateSQLConnectInstance(); [Route("ImportFile"), HttpPost] public ResultData ImportFile() { using (var w = new LimitedConcurrency()) { string fileName = ""; Stream fileStream = null; var request = System.Web.HttpContext.Current.Request; var formData = request.Form; string userName = formData["userId"]; string clearOriginalTableSql = $@"TRUNCATE TABLE UserInfo"; string nowTime = DateTime.Now.ToString("yyyyMMddHHmmss"); string createBackupTableSql = $@"SELECT * INTO UserInfobackup{nowTime} FROM UserInfo"; HttpFileCollection files = HttpContext.Current.Request.Files;
SqlHelper.ExecteNonQuery(connstr, CommandType.Text, createBackupTableSql, null); SqlHelper.ExecteNonQuery(connstr, CommandType.Text, clearOriginalTableSql, null);
SqlConnection myConnection = new SqlConnection(connstr); myConnection.Open(); SqlTransaction myTrans = myConnection.BeginTransaction(); SqlCommand myCommand = new SqlCommand(); myCommand.Connection = myConnection; myCommand.Transaction = myTrans;
foreach (string key in files.AllKeys) { HttpPostedFile file = files[key]; if (string.IsNullOrEmpty(file.FileName) == false) { fileName = file.FileName; fileStream = file.InputStream; } else { return new ResultData { IsError = true, Message = "未获取到Excel数据" }; } } if (Path.GetExtension(fileName).ToLower() != ".xls" && Path.GetExtension(fileName).ToLower() != ".xlsx") { return new ResultData { IsError = true, Message = "请上传excel文件" }; }
try { DataTable data = ExcelHelper.ImportStream(fileStream, fileName);
foreach (DataRow itemRows in data.Rows) { string description = HandleString(itemRows["UserDescription"].ToString()); string userSex = itemRows["UserSex"].ToString(); string userAge = itemRows["UserAge"].ToString(); if (StringIsEmpty(userName)) { return new ResultData { IsError = true, Message = "UserName不能为空" }; } if (StringIsEmpty(description)) { return new ResultData { IsError = true, Message = "UserDescription不能为空" }; } if (StringIsEmpty(userSex)) { return new ResultData { IsError = true, Message = "UserSex不能为空" }; } if (!IsNumber(userAge)) { return new ResultData { IsError = true, Message = "UserAge不是数字类型" }; } string sql = $@"INSERT INTO UserInfo( [ID], [CREATED_DATE], [UserName], [UserSex], [UserAge] [UserDescription] VALUES ( NEWID(), GETDATE(), '{userName}', '{userSex}', '{userAge}', '{description}')";
myCommand.CommandText = sql; myCommand.ExecuteNonQuery();
}
myTrans.Commit(); return new ResultData { IsError = false, Message = "数据导入成功" }; } catch (Exception ex) { myTrans.Rollback(); throw ex; }
}
}
public bool StringIsEmpty(string str) { if (string.IsNullOrEmpty(str)) { return true; } return false; }
public bool IsNumber(string number) { decimal num = 0; bool n = decimal.TryParse(number, out num); if (!string.IsNullOrEmpty(number) && n) { return true; } return false; }
public string HandleString(string content) { if (content.Contains("'")) { content = content.Replace("'", "''"); } return content; }
public class ResultData { public bool IsError { get; set; } public string Message { get; set; } } } }
|