ImportOhter.cs
7.59 KB
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
using Hh.Mes.POJO.Response;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text.RegularExpressions;
using static Hh.Mes.Common.Exel.NpoiExcelImportHelper;
namespace Hh.Mes.Service.WebService.Planned
{
/// <summary>
/// 第一步 Excel页签的名称 对应数据库表
/// </summary>
public enum ExcelName
{
equipment,
sys_user,
base_material,
base_process_route_head,
base_process_route_detail,
bus_workOrder_head,
bus_workOrder_detail,
}
/// <summary>
/// 第二步 定义表名 tableNamexxxxx :对应数据库的表名
/// </summary>
public class TableName
{
/// <summary>
/// 数据源 后缀(_source) 禁止修改
/// </summary>
public static readonly string tableSuffix = "_source";
/// <summary>
/// 设备管理 数据库表名
/// </summary>
public static readonly string tableNameEquipment = ExcelName.equipment.ToString();
/// <summary>
/// 设备管理 excel文件名 用于错误信息提示
/// </summary>
public static readonly string excelFileNameEquipment = "设备管理";
/// <summary>
/// 用户表 数据库表名
/// </summary>
public static readonly string tableNameSysUser = ExcelName.sys_user.ToString();
/// <summary>
/// 人员信息 excel文件名 用于错误信息提示
/// </summary>
public static readonly string excelFileNameSysUser = "人员信息";
/// <summary>
/// 物料表 数据库表名
/// </summary>
public static readonly string tableNameBaseMaterial = ExcelName.base_material.ToString();
/// <summary>
/// 物料信息 excel文件名 用于错误信息提示
/// </summary>
public static readonly string excelFileNameBaseMaterial = "物料信息";
public static readonly string tableNameBaseProcessRouteHead=ExcelName.base_process_route_head.ToString();
public static readonly string excelFileNameBaseProcessRouteHead = "工艺路线";
public static readonly string tableNameBaseProcessRouteDetail = ExcelName.base_process_route_detail.ToString();
public static readonly string excelFileNameBaseProcessRouteDetail = "工艺路线明细";
public static readonly string tableNameBusWorkOrderHead=ExcelName.bus_workOrder_head.ToString();
public static readonly string excelFileNameBusWorkOrderHead = "生产订单";
public static readonly string tableNameBusWorkOrderDetail = ExcelName.bus_workOrder_detail.ToString();
public static readonly string excelFileNameBusWorkOrderDetail = "生产订单明细";
}
#region 公共类 不需要理会
/// <summary>
/// ImportService 帮助类
/// </summary>
public static class ImportOhter
{
public static Response ImportMethodBefore(Dictionary<string, SysDtAllCls> pairs, Response response, string keys)
{
DataTable dt;
if (pairs.ContainsKey(keys))
{
dt = pairs[keys].dtData;
}
else
{
response.Code = 500;
response.Status = false;
response.Message = $"导入Excel不存在[{keys}]文件]";
return response;
}
if (dt == null || dt.Rows.Count == 0)
{
response.Code = 500;
response.Message = $"[{keys}]导入Excel文件没有数据!";
return response;
}
response.Result = new
{
data = dt,
title = pairs[keys].dtTitle
};
return response;
}
/// <summary>
/// 过滤列名
/// </summary>
public static DataTable DtModifyColumnName(this DataTable dt, ref Response response, string ExcelName)
{
//查询更改列名
for (int i = 0; i < dt.Columns.Count; i++)
{
var columnName = Regex.Replace(dt.Columns[i].ColumnName, @"[^a-zA-Z1-9]", "").Trim();
if (string.IsNullOrEmpty(columnName))
{
response.Code = 500;
response.Message = $"{ExcelName} 列【{dt.Columns[i].ColumnName}】不存在英文 ";
return dt;
}
dt.Columns[i].ColumnName = columnName;
}
return dt;
}
/// <summary>
/// 去重方法
/// </summary>
public static DataTable Distinct(this DataTable ds, string columns)
{
DataView dv = new DataView(ds);
DataTable where = dv.ToTable(true, columns);
DataTable data = ds.Clone();
foreach (DataRow item in where.Rows)
{
DataRow[] dataRow = ds.Select($"{columns}='{item[columns]}'");
data.ImportRow(dataRow[0]);
}
return data;
}
/// <summary>
/// 获取dt所有列
/// </summary>
public static List<string> GetColumnsByDataTable(this DataTable dt)
{
var temp = new List<string>();
foreach (DataColumn c in dt.Columns)
{
temp.Add(c.ColumnName);
}
return temp;
}
/// <summary>
/// 设置DataTable 值 下拉框格式【name_code】
/// </summary>
public static dynamic SetDataTableData(dynamic colNameVal, string dataTypeName)
{
var selectKey = "_";
if (!string.IsNullOrEmpty(colNameVal) && colNameVal.IndexOf(selectKey) > -1)
{
var tempArr = colNameVal.Split(selectKey);
switch (dataTypeName)
{
case "Int32":
colNameVal = int.Parse(tempArr[1]);
break;
default:
colNameVal = tempArr[1];
break;
}
}
else if (string.IsNullOrEmpty(colNameVal))
{
switch (dataTypeName)
{
case "String":
colNameVal = "";
break;
case "Int32":
case "DateTime":
colNameVal = DBNull.Value;
break;
default:
colNameVal = DBNull.Value;
break;
}
}
return colNameVal;
}
/// <summary>
/// 设置DataTable Before 判断是否必填
/// </summary>
public static Response SetDataTableDataBefore(Response response, int index, string colName, string colNameVal, DataTable titleVerifySourceDt, string excelName)
{
var keys = "*";
if (index == 0)
{
string tempVerifyVal = titleVerifySourceDt.Rows[index][colName].ToString();
if (tempVerifyVal.IndexOf(keys) > -1 && string.IsNullOrEmpty(colNameVal))
{
response.Code = 500;
response.Status = false;
response.Message = $"【{excelName}】导入 Excel 列名【{colName}、{tempVerifyVal}】不能为空";
return response;
}
}
response.Message = "";
response.Code = 200;
return response;
}
}
#endregion
}