schangxiang@126.com
2025-11-04 f5ed29dc26c7cd952d56ec5721a2efc43cd25992
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
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using XCommon.Log;
using XImagingXhandler.XDAL;
using Microsoft.Win32;
using XCore;
using XHandler.Class;
using HandyControl.Data;
using XCommon.Tip;
using System.CodeDom.Compiler;
using System.IO;
 
namespace XHandler.View.MethodProperty
{
    /// <summary>
    /// VariableImportProperty.xaml 的交互逻辑
    /// </summary>
    public partial class VariableImportProperty : UserControl
    {
        public MethodVariableImport methodVariableImport { get; set; }
        public MethodVariableImport currmethodVariableImport = null;
 
        #region 全局属性变量
        string isrun = "";
        string status = "";
        string name = "";
        string label = "";
 
        string filePath = "";
        DataTable transferDataTable = new DataTable();
 
        int mark = 0;
        #endregion
 
        public VariableImportProperty()
        {
            InitializeComponent();
        }
 
        public VariableImportProperty(MethodEx method)
        {
            InitializeComponent();
            mark = 0;
            methodVariableImport = new MethodVariableImport();
            methodVariableImport.name = method.method_name;
            methodVariableImport.label = method.method_name;
 
            this.DataContext = methodVariableImport;
            if (method.tag != null)
            {
                methodVariableImport = (MethodVariableImport)method.tag;
 
                currmethodVariableImport = methodVariableImport;
 
                isrun = methodVariableImport.isrun;
                status = methodVariableImport.status;
                name = methodVariableImport.name;
                label = methodVariableImport.label;
 
                filePath = methodVariableImport.filePath;
                transferDataTable = methodVariableImport.transferDataTable;
            }
        }
 
        private void tbxCommandName_TextChanged(object sender, TextChangedEventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(tbxCommandName.Text))
                {
                    xcError.Text = "命令名称不能为空";
                    return;
                }
                else
                {
                    xcError.Text = "";
                }
                if (methodVariableImport != null)
                {
                    methodVariableImport.name = tbxCommandName.Text;
                }
            }
            catch (Exception ex)
            {
                LoggerHelper.ErrorLog("ERROR:", ex);
            }
        }
 
        private void btnSelectFile_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Title = "请选择Excel文件";
            dlg.FileName = string.Empty;
            dlg.FilterIndex = 1;
            dlg.RestoreDirectory = true;
            dlg.Filter = "excel files(*.xls)|*.xls|csv files(*.csv)|*.csv|All files(*.*)|*.*";
            dlg.Multiselect = false;
            if (!string.IsNullOrEmpty(tbExcelFile.Text.Trim()))
            {
                try
                {
                    if (System.IO.Path.HasExtension(tbExcelFile.Text.Trim()))
                        dlg.InitialDirectory = System.IO.Path.GetDirectoryName(tbExcelFile.Text.Trim());
                    else
                        dlg.InitialDirectory = tbExcelFile.Text.Trim();
                }
                catch (Exception ex)
                {
 
                }
            }
            if (dlg.ShowDialog() == true)
            {
                string fullPath = dlg.FileName;
                tbExcelFile.Text = fullPath;
                if (methodVariableImport != null)
                {
                    methodVariableImport.filePath = fullPath;
                }
 
                SelectFile(fullPath);
            }
        }
 
        private void SelectFile(string file)
        {
            string ext = System.IO.Path.GetExtension(file);
            DataTable transferData = null;
            if (string.Compare(ext, ".csv", true) == 0)
                transferData = ExcelAndCsvHelper.GetDataTableFromCsvFile(file);
            else
                transferData = ExcelAndCsvHelper.GetDataTableFromExcelFile(file, true);
 
            if (transferData != null)
            {
                //判断表头是否符合要求
                if(transferData.Columns.Contains("paraName")&& transferData.Columns.Contains("paraValue"))
                {
                    CodeDomProvider provider = CodeDomProvider.CreateProvider("C#");
                    foreach (DataRow dataRow in transferData.Rows)
                    {
                        if (provider.IsValidIdentifier(dataRow["paraName"].ToString()))
                        {
 
                        }
                        else
                        {
                            ShowTip.ShowNotice(string.Format("{0}{1}{2}", "提醒", Environment.NewLine, "表格内容列'" + dataRow["paraName"].ToString() + "'不符合规范,未能加载"), InfoType.Warning);
                            dgTransferFileData.ItemsSource = null;
                            return;
                        }
                    }
 
                    if (methodVariableImport != null)
                    {
                        methodVariableImport.transferDataTable = transferData;
                    }
                    dgTransferFileData.ItemsSource = transferData.DefaultView;
 
                    List<TransferFileHeader> transferFileHeaders = new TransferFileBll().GetTransferFileHeaders(transferData);
                }
                else
                {
                    ShowTip.ShowNotice(string.Format("{0}{1}{2}", "提醒", Environment.NewLine, "表格头不符合规范,未能加载"), InfoType.Warning);
                    dgTransferFileData.ItemsSource = null;
                }
 
                
            }
        }
 
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            mark += 1;
            try
            {
                if (mark > 1)
                {
                    return;
                }
 
                if (!string.IsNullOrEmpty(filePath))
                {
                    tbxCommandName.Text = name;
 
                    dgTransferFileData.ItemsSource = transferDataTable.DefaultView;
                    tbExcelFile.Text = filePath;
 
                    if (currmethodVariableImport != null)
                    {
                        methodVariableImport = currmethodVariableImport;
                    }
                }
                else
                {
                    if (currmethodVariableImport != null)
                    {
                        methodVariableImport = currmethodVariableImport;
                    }
                }
            }
            catch (Exception ex)
            {
                LoggerHelper.ErrorLog("ERROR:", ex);
            }
        }
 
        private void tbExcelFile_TextChanged(object sender, TextChangedEventArgs e)
        {
            try
            {
                if (methodVariableImport != null)
                {
                    methodVariableImport.filePath = tbExcelFile.Text;
                }
            }
            catch (Exception ex)
            {
                LoggerHelper.ErrorLog("ERROR:", ex);
            }
        }
 
        private void tbExcelFile_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                if (!string.IsNullOrEmpty(tbExcelFile.Text.Trim()))
                {
                    if (System.IO.Path.HasExtension(tbExcelFile.Text.Trim()))
                    {
                        if (System.IO.File.Exists(tbExcelFile.Text.Trim()))
                        {
                            SelectFile(tbExcelFile.Text.Trim());
                        }
                    }
                }
            }
        }
 
        private void btnDownloadTemplate_Click(object sender, RoutedEventArgs e)
        {
            //弹出保存对话框
            try
            {
                string fileDir = string.Empty;
                SaveFileDialog dlg = new SaveFileDialog();
                dlg.Title = "另存为";
                dlg.FileName = $"{DateTime.Now.ToString("yyyyMMddHHmmss")}.csv";       // Default file name
                dlg.DefaultExt = ".xls";                                               // Default file extension
                dlg.Filter = "excel files(*.xls)|*.xls|csv files(*.csv)|*.csv";// Filter files by extension
                dlg.InitialDirectory = System.AppDomain.CurrentDomain.BaseDirectory;
 
                if (dlg.ShowDialog() == true)
                {
                    string directoryBase = System.AppDomain.CurrentDomain.BaseDirectory;
                    string sourceFilePath = directoryBase + "TempData\\variableImport.csv";
                    File.Copy(sourceFilePath, dlg.FileName, true);
                    ShowTip.ShowNotice(string.Format("{0}成功{1}{2}", "下载保存", Environment.NewLine, dlg.FileName), InfoType.Warning);
                }
            }
            catch (Exception ex)
            {
                ShowTip.ShowNotice(string.Format("{0}失败{1}{2}", "下载保存", Environment.NewLine, ""), InfoType.Warning);
                LoggerHelper.ErrorLog("btnDownloadTemplate_Click ERROR:", ex);
            }
        }
    }
}