UsrDataSource.cs
4.4 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using Asa.FaceControl;
using BLL;
using ClosedXML.Excel;
using Model;
namespace SmartScan
{
public partial class UsrDataSource : UserControl, ISetMenu
{
public UsrDataSource()
{
InitializeComponent();
CboDataType.Items.Add("None");
CboDataType.Items.Add("Excel/CSV");
CboDataType.SelectedIndex = 0;
TxtDataSource.TextChanged += TxtDataSource_TextChanged;
TxtDataSource.Text = Config.DataSource_FilePath;
ChkRecursive.Checked= Config.DataSource_Recursive;
Asa.FaceControl.Language.SetLanguage(this);
}
private void TxtDataSource_TextChanged(object sender, EventArgs e)
{
var filestring = TxtDataSource.Text.Trim();
if (string.IsNullOrEmpty(filestring)) {
LblFilestatus.Text = Language.Dialog("selectdatasource","请选择数据文件");
}
if (!File.Exists(filestring))
{
LblFilestatus.Text = Language.Dialog("filenotexists", "文件状态:改文件不存在");
}
List<string> titles;
if (Path.GetExtension(TxtDataSource.Text).ToLower() == ".csv")
{
titles = ExtraFileData.ParseCSVFileTitle(TxtDataSource.Text);
}
else if (Path.GetExtension(TxtDataSource.Text).ToLower() == ".xlsx")
{
try
{
XLWorkbook wb = new XLWorkbook(filestring);
}
catch
{
LblFilestatus.Text = Language.Dialog("filepatseerror", "文件内容解析失败");
return;
}
titles = ExtraFileData.ParseXLSFileTitle(TxtDataSource.Text);
}
else
{
LblFilestatus.Text = Language.Dialog("fileformaterror", "文件格式不正确");
return;
}
txtkey.Text = "";
CboDataTitle.Items.Clear();
titles.ForEach(t => {
CboDataTitle.Items.Add(t);
txtkey.Text += $"{{{t}}}\r\n";
});
if (CboDataTitle.Items.Count > 0)
CboDataTitle.SelectedIndex = 0;
if (!string.IsNullOrEmpty(Config.DataSource_DataTitle))
{
CboDataTitle.SelectedText = Config.DataSource_DataTitle;
}
}
public Asa.FaceControl.FacePanel GetPanel()
{
if (Config.DataSource_Type == "xls")
{
CboDataType.SelectedIndex = 1;
}
else {
}
CboDataKey.Items.Clear();
CboDataKey.Items.AddRange(BLLCommon.macroKey.ToArray());
CboDataKey.SelectedIndex = 0;
if (!string.IsNullOrEmpty(Config.DataSource_DataKey))
{
CboDataKey.SelectedText = Config.DataSource_DataKey;
}
TxtDataSource.Text = Config.DataSource_FilePath;
return facePanel1;
}
public void Save()
{
if (CboDataType.SelectedIndex == 1)
Config.DataSource_Type = "xls";
else
Config.DataSource_Type = "none";
Config.DataSource_FilePath = TxtDataSource.Text;
Config.DataSource_DataKey = CboDataKey.Text;
Config.DataSource_DataTitle = CboDataTitle.Text;
Config.DataSource_Recursive = ChkRecursive.Checked;
ExtraFileData.Init();
BLLCommon.extraKey = ExtraFileData.Titles;
}
private void BtnSelectFile_Click(object sender, EventArgs e)
{
OpenFileDialog fileDialog = new OpenFileDialog();
fileDialog.Filter = "Excel files (*.csv;*.xlsx)|*.csv;*.xlsx";
fileDialog.RestoreDirectory = true;
if (fileDialog.ShowDialog() != DialogResult.OK)
return;
if (Path.GetExtension(fileDialog.FileName).ToLower() == ".csv")
{
if (!FrmDataFilePreview.ShowPreview(fileDialog.FileName, out string encodingtxt))
return;
Config.DataSource_Encoding = encodingtxt;
}
TxtDataSource.Text = fileDialog.FileName;
}
}
}