yl
2022-06-28 fcca000072a8cfae98c1cb3dadce0512b19083cc
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using VueWebApi.DLL.BLL;
using VueWebApi.Models;
using VueWebApi.Tools;
 
namespace VueWebApi.Controllers
{
    [RoutePrefix(prefix: "api/BasicSetting")]
    [ControllerGroup("Excel导入模板验证", "在线接口")]
    public class ImportExcelController : ApiController
    {
        //定义全局信息返回变量
        ToMessage mes = new ToMessage();
 
 
        #region [Excel导入模板下载]
        /// <summary>
        /// Excel导入模板下载
        /// </summary>
        /// <param name="FileCode">文件编码</param>
        /// <returns></returns>
        [Route(template: "DownLoadExcel")]
        [HttpGet]
        public HttpResponseMessage DownLoadExcel(string FileCode = null)
        {
            List<ScoreReport> list = ExcelList.ExcelData();
            list = list.Where(s => s.FileCode == FileCode).ToList();
            var filename = list[0].FileName + ".xls";
            string fileip = System.Configuration.ConfigurationManager.AppSettings["FileIP"];
            var msg = fileip + "/apis/Excel/" + filename;
            mes.code = "200";
            mes.data = msg;
            return TJson.toJson(mes);
        }
        #endregion
 
        #region [Excel导入文件上传、模板验证、数据量、验证]
        /// <summary>
        /// Excel导入文件上传、模板验证、数据量、验证
        /// </summary>
        /// <param name="files">上传文件</param>
        /// <param name="FileCode">文件编码</param>
        /// <returns></returns>
        [Route(template: "ExcelModelCheck")]
        [HttpGet]
        public HttpResponseMessage ExcelModelCheck(HttpPostedFileBase files, string FileCode = null)
        {
            ExcelModelCheck list = new ExcelModelCheck();
            Dictionary<object, object> dList = new Dictionary<object, object>();
            list.json1 = ExcelCheckUpload(files, FileCode);  //文件上传
            if (list.json1.code == "300")
            {
                list.json1 = list.json1;
                return TJson.toJson(list);
            }
            list.json2 = ExcelCheck(files, FileCode);  //模板验证
            if (list.json2.code == "300")
            {
                list.json2 = list.json2;
                return TJson.toJson(list);
            }
            list.json3 = ExcelCheckCount(files, FileCode);  //数据量验证
            if (list.json3.code == "300")
            {
                list.json3 = list.json3;
                return TJson.toJson(list);
            }
            return TJson.toJson(list);
        }
        #endregion
 
        #region [Excel导入模板上传]
        /// <summary>
        /// Excel导入模板上传
        /// </summary>
        /// <param name="files">上传文件</param>
        /// <param name="FileCode">文件编码</param>
        /// <returns></returns>
        [Route(template: "ExcelCheckUpload")]
        [HttpGet]
        [HiddenApi]
        public ToMessage ExcelCheckUpload(HttpPostedFileBase files, string FileCode = null)
        {
            try
            {
                string savePath;
                string filename = Path.GetFileName(files.FileName);
 
                string path = System.Web.HttpContext.Current.Server.MapPath("/InExcel/" + FileCode + "/");
                string fileEx = System.IO.Path.GetExtension(filename);//获取上传文件的扩展名
                string FileType = ".xls,.xlsx";//定义上传文件的类型字符串
                savePath = Path.Combine(path, filename);
                if (!FileType.Contains(fileEx))
                {
                    mes.code = "300";
                    mes.Message = "文件类型不对,上传失败!";
                    System.IO.File.Delete(savePath);//执行IO文件删除,需引入命名空间System.IO;
                    return mes;
                }
                else if (Directory.Exists(path) == false)//如果不存在就创建file文件夹
                {
                    Directory.CreateDirectory(path); //添加文件夹
                }
                else if (Directory.Exists(savePath) == true)  //如果存在重名文件就提示
                {
                    System.IO.File.Delete(savePath);//执行IO文件删除,需引入命名空间System.IO;
                    files.SaveAs(savePath);    //将选择的文件保存到指定文件夹下
                    mes.code = "200";
                    mes.Message = "文件上传成功!";
                }
                else
                {
                    files.SaveAs(savePath);    //将选择的文件保存到指定文件夹下
                    mes.code = "200";
                    mes.Message = "文件上传成功!";
                }
            }
            catch (Exception e)
            {
                mes.code = "300";
                mes.Message = e.Message;
            }
            return mes;
        }
        #endregion
 
        #region [Excel导入模板验证]
        /// <summary>
        /// Excel导入模板验证
        /// </summary>
        /// <param name="files">上传文件</param>
        /// <param name="FileCode">文件编码</param>
        /// <returns></returns>
        [Route(template: "ExcelCheckUpload")]
        [HttpGet]
        [HiddenApi]
        public ToMessage ExcelCheck(HttpPostedFileBase files, string FileCode = null)
        {
            try
            {
                string savePath;
 
                string filename = Path.GetFileName(files.FileName);
                string path = System.Web.HttpContext.Current.Server.MapPath("/InExcel/" + FileCode + "/");
                savePath = Path.Combine(path, filename);
                if (Directory.Exists(path) == false)//如果不存在就创建file文件夹
                {
                    Directory.CreateDirectory(path); //添加文件夹
                }
                else
                {
                    mes = ExcelCheckBLL.ExcelCheck(FileCode, savePath);
                    if (mes.code == "300") //上传模板不是指定模板
                    {
                        System.IO.File.Delete(savePath);//执行IO文件删除,需引入命名空间System.IO;
                        return mes;
                    }
                }
                return mes;
            }
            catch (Exception e)
            {
                mes.code = "300";
                mes.Message = e.Message;
            }
            return mes;
        }
        #endregion
 
        #region[Excel导入模板数据量验证]
        /// <summary>
        /// Excel导入模板数据量验证
        /// </summary>
        /// <param name="files">上传文件</param>
        /// <param name="FileCode">文件编码</param>
        /// <returns></returns>
        [HiddenApi]
        public ToMessage ExcelCheckCount(HttpPostedFileBase files, string FileCode = null)
        {
            string savePath;
 
            string filename = Path.GetFileName(files.FileName);
            int filesize = files.ContentLength;//获取上传文件的大小单位为字节byte
            string fileEx = System.IO.Path.GetExtension(filename);//获取上传文件的扩展名
            int Maxsize = 10000 * 1024;//定义上传文件的最大空间大小为10M
            string path = System.Web.HttpContext.Current.Server.MapPath("/InExcel/" + FileCode + "/");
            savePath = Path.Combine(path, filename);
            IWorkbook Workbook;
            DataTable table = new DataTable();
            using (FileStream fileStream = new FileStream(savePath, FileMode.Open, FileAccess.Read))
            {
                //XSSFWorkbook 适用XLSX格式,HSSFWorkbook 适用XLS格式
                string fileExt = Path.GetExtension(savePath).ToLower();
                if (fileExt == ".xls")
                {
                    Workbook = new HSSFWorkbook(fileStream);
                }
                else if (fileExt == ".xlsx")
                {
                    Workbook = new XSSFWorkbook(fileStream);
                }
                else
                {
                    Workbook = null;
                }
            }
            //定位在第一个sheet
            ISheet sheet = Workbook.GetSheetAt(0);
            //sheet.ShiftRows(sheet.FirstRowNum-1, sheet.LastRowNum,1);  
            //第一行为标题行
            IRow headerRow = sheet.GetRow(0);
            int cellCount = headerRow.LastCellNum;
            //int rowCount0 = sheet.FirstRowNum;
            int rowCount = sheet.LastRowNum;
            if (files == null || files.ContentLength <= 0)
            {
                mes.code = "300";
                mes.Message = "文件不能为空!";
                System.IO.File.Delete(savePath);//执行IO文件删除,需引入命名空间System.IO;
                return mes;
            }
            mes = ImportExcel.ExcelCheckCountSum(savePath); //空数据文件验证
            if (mes.code == "300")
            {
 
                mes.code = "300";
                mes.Message = "文件不能为空!";
                System.IO.File.Delete(savePath);//执行IO文件删除,需引入命名空间System.IO;
                return mes;
            }
            if (filesize >= Maxsize)
            {
 
                mes.code = "300";
                mes.Message = "上传文件超过10M,不能上传";
                System.IO.File.Delete(savePath);//执行IO文件删除,需引入命名空间System.IO;
                return mes;
            }
            mes.code = "200";
            mes.Message = "数据量验证通过";
 
            return mes;
        }
        #endregion
 
        #region [Excel导入数据验证]
        /// <summary>
        /// Excel导入数据验证
        /// </summary>
        /// <param name="FileCode">文件编码</param>
        /// <param name="FileName">文件名称</param>
        /// <returns></returns>
        [HiddenApi]
        public HttpResponseMessage ExcelCheckData(string FileCode = null, string FileName = null)
        {
            string message = "";
            string StuCode = "";
            int count = 0;
            List<ExcelErro> list = new List<ExcelErro>();
            string savePath;
            string filename = FileName;
            string path = System.Web.HttpContext.Current.Server.MapPath("/InExcel/" + FileCode + "/");
            savePath = Path.Combine(path, filename);
            list = ExcelCheckBLL.ExcelCheckData(FileCode, savePath, out StuCode, out message, out count);
            if (list.Count > 0)
            {
                System.IO.File.Delete(savePath);//执行IO文件删除,需引入命名空间System.IO;
            }
            Dictionary<object, object> dList = new Dictionary<object, object>();
            dList.Add("code", StuCode);
            dList.Add("Message", message);
            dList.Add("Count", count);
            dList.Add("list", list);
            return TJson.toJson(dList);
        }
        #endregion
 
        #region【Excel导入数据】
        /// <summary>
        /// Excel导入数据
        /// </summary>
        /// <param name="FileCode">文件编码</param>
        /// <param name="FileName">文件名称</param>
        /// <returns></returns>
        [HiddenApi]
        public HttpResponseMessage ExcelImportSubmit(string FileCode = null, string FileName = null)
        {
            var username = HttpContext.Current.Request.Cookies["admin"].Value.ToString(); //登录用户名 
            string savePath;
            string filename = FileName;
            string path = System.Web.HttpContext.Current.Server.MapPath("/InExcel/" + FileCode + "/");
            savePath = Path.Combine(path, filename);
            mes = ExcelCheckBLL.ExcelImportSubmit(FileCode, savePath, username);
            return TJson.toJson(mes);
        }
        #endregion
    }
}