oee相关修改
This commit is contained in:
1 parent
9c5ef7bb31
commit
4419a4de3c
9 files changed
+422
-121
No files matched your search
@@ -1,4 +1,4 @@
|
||||
using System;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Data;
|
||||
@@ -24,28 +24,6 @@ namespace WindowsFormsApp1.form
|
||||
GetInfo();
|
||||
}
|
||||
|
||||
private void 保存_Click(object sender, EventArgs e)
|
||||
{
|
||||
try
|
||||
{
|
||||
SQLiteHelper<t_oeeaddress> sQLiteHelper = new db.SQLiteHelper<t_oeeaddress>();
|
||||
var result = sQLiteHelper.UpdateAll((List<t_oeeaddress>)dataGridView1.DataSource);
|
||||
if (result.Success)
|
||||
{
|
||||
MessageBox.Show("更新成功");
|
||||
GetInfo();
|
||||
}
|
||||
else
|
||||
{
|
||||
MessageBox.Show("更新失败");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show($"更新失败,错误原因{ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
private void 关闭_Click(object sender, EventArgs e)
|
||||
{
|
||||
this.Close();
|
||||
@@ -85,7 +63,7 @@ namespace WindowsFormsApp1.form
|
||||
comboBoxColumn.DisplayMember = "name";
|
||||
comboBoxColumn.ValueMember = "value";
|
||||
|
||||
comboBoxColumn.DataPropertyName = "type";
|
||||
comboBoxColumn.DataPropertyName = "type";
|
||||
|
||||
var comboBoxColumn1 = (DataGridViewComboBoxColumn)dataGridView1.Columns[5];
|
||||
// 绑定数据源到 ComboBox
|
||||
@@ -114,10 +92,10 @@ namespace WindowsFormsApp1.form
|
||||
dataGridView1.CellContentClick += DataGridView1_CellContentClick;
|
||||
|
||||
dataGridView1.Columns.Add(deleteColumn);
|
||||
|
||||
|
||||
GetInfo();
|
||||
}
|
||||
|
||||
|
||||
private void DataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
|
||||
{
|
||||
// 检查点击的是否为按钮列
|
||||
@@ -127,7 +105,7 @@ namespace WindowsFormsApp1.form
|
||||
DataGridViewRow row = dataGridView.Rows[e.RowIndex];
|
||||
|
||||
int selectid = int.Parse(row.Cells[0].Value.ToString());
|
||||
if (e.ColumnIndex == 9)
|
||||
if (e.ColumnIndex == 10)
|
||||
{
|
||||
OEEaddress_Insert oeeInsert = new OEEaddress_Insert();
|
||||
oeeInsert.dataid = selectid;
|
||||
@@ -135,7 +113,7 @@ namespace WindowsFormsApp1.form
|
||||
oeeInsert.ShowDialog();
|
||||
GetInfo();
|
||||
}
|
||||
else if (e.ColumnIndex == 10)
|
||||
else if (e.ColumnIndex == 11)
|
||||
{
|
||||
DialogResult result = MessageBox.Show("你确定要删除数据吗?", "确认提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
|
||||
|
||||
@@ -156,7 +134,7 @@ namespace WindowsFormsApp1.form
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void GetInfo()
|
||||
{
|
||||
try
|
||||
@@ -200,5 +178,219 @@ namespace WindowsFormsApp1.form
|
||||
oeeInsert.ShowDialog();
|
||||
GetInfo();
|
||||
}
|
||||
|
||||
// 添加导入按钮点击事件处理
|
||||
private void 导入_Click(object sender, EventArgs e)
|
||||
{
|
||||
try
|
||||
{
|
||||
OpenFileDialog openFileDialog = new OpenFileDialog();
|
||||
openFileDialog.Filter = "CSV文件|*.csv";
|
||||
openFileDialog.Title = "选择要导入的CSV文件";
|
||||
|
||||
if (openFileDialog.ShowDialog() == DialogResult.OK)
|
||||
{
|
||||
string filePath = openFileDialog.FileName;
|
||||
ImportFromCSV(filePath);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show($"导入失败,错误原因:{ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
private void ImportFromCSV(string filePath)
|
||||
{
|
||||
try
|
||||
{
|
||||
List<t_oeeaddress> addressList = new List<t_oeeaddress>();
|
||||
|
||||
// 读取CSV文件 - 修改编码方式
|
||||
// 尝试使用UTF-8编码读取
|
||||
string[] lines = System.IO.File.ReadAllLines(filePath, Encoding.UTF8);
|
||||
|
||||
// 如果检测到乱码,可以尝试其他编码
|
||||
if (ContainsGarbledText(lines))
|
||||
{
|
||||
// 尝试使用GB2312编码
|
||||
lines = System.IO.File.ReadAllLines(filePath, Encoding.GetEncoding("gb2312"));
|
||||
|
||||
// 如果仍然乱码,再尝试其他编码
|
||||
if (ContainsGarbledText(lines))
|
||||
{
|
||||
// 尝试使用GBK编码
|
||||
lines = System.IO.File.ReadAllLines(filePath, Encoding.GetEncoding("gbk"));
|
||||
}
|
||||
}
|
||||
|
||||
// 从第2行开始读取数据(假设第1行是标题)
|
||||
for (int i = 1; i < lines.Length; i++)
|
||||
{
|
||||
string line = lines[i];
|
||||
if (string.IsNullOrWhiteSpace(line))
|
||||
continue;
|
||||
|
||||
// 分割CSV行并去除双引号
|
||||
string[] values = SplitCsvLineAndRemoveQuotes(line);
|
||||
|
||||
if (values.Length < 10)
|
||||
{
|
||||
MessageBox.Show($"第{i + 1}行数据格式不正确,请检查CSV文件格式");
|
||||
break;
|
||||
}
|
||||
|
||||
var test = values[0];
|
||||
try
|
||||
{
|
||||
t_oeeaddress address = new t_oeeaddress
|
||||
{
|
||||
plcid = Convert.ToInt32(values[0]),
|
||||
code = values[1],
|
||||
type = values[2],
|
||||
dbaddress = values[3],
|
||||
addresstype = values[4],
|
||||
addresstypelegth = Convert.ToInt32(values[5]),
|
||||
addressoffset = Convert.ToInt32(values[6]),
|
||||
addressdecimal = Convert.ToInt32(values[7]),
|
||||
remark = values[8] ?? "",
|
||||
};
|
||||
|
||||
addressList.Add(address);
|
||||
}
|
||||
catch (FormatException)
|
||||
{
|
||||
MessageBox.Show($"第{i + 1}行数据格式转换错误,请检查数据类型是否正确");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
SaveImportedData(addressList);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show($"CSV导入失败,错误原因:{ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
// 添加一个方法来分割CSV行并去除双引号
|
||||
private string[] SplitCsvLineAndRemoveQuotes(string line)
|
||||
{
|
||||
// 使用正则表达式处理CSV,考虑引号内的逗号
|
||||
List<string> result = new List<string>();
|
||||
bool inQuotes = false;
|
||||
StringBuilder field = new StringBuilder();
|
||||
|
||||
for (int i = 0; i < line.Length; i++)
|
||||
{
|
||||
char c = line[i];
|
||||
|
||||
if (c == '"')
|
||||
{
|
||||
// 如果是引号,切换引号状态
|
||||
inQuotes = !inQuotes;
|
||||
// 不将引号添加到结果中
|
||||
}
|
||||
else if (c == ',' && !inQuotes)
|
||||
{
|
||||
// 如果是逗号且不在引号内,则添加当前字段并重置
|
||||
result.Add(field.ToString().Trim());
|
||||
field.Clear();
|
||||
}
|
||||
else
|
||||
{
|
||||
// 其他字符直接添加
|
||||
field.Append(c);
|
||||
}
|
||||
}
|
||||
|
||||
// 添加最后一个字段
|
||||
result.Add(field.ToString().Trim());
|
||||
|
||||
return result.ToArray();
|
||||
}
|
||||
|
||||
// 添加一个辅助方法检测文本是否包含乱码
|
||||
private bool ContainsGarbledText(string[] lines)
|
||||
{
|
||||
// 简单检测:如果包含特定的乱码字符,则认为是乱码
|
||||
// 这里只是一个简单的示例,可能需要根据实际情况调整
|
||||
string pattern = @"[\u0000-\u001F\u007F-\u009F]"; // 控制字符
|
||||
|
||||
foreach (string line in lines)
|
||||
{
|
||||
if (System.Text.RegularExpressions.Regex.IsMatch(line, pattern))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private void SaveImportedData(List<t_oeeaddress> addressList)
|
||||
{
|
||||
if (addressList.Count == 0)
|
||||
{
|
||||
MessageBox.Show("没有有效数据可导入");
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
SQLiteHelper<t_oeeaddress> sQLiteHelper = new db.SQLiteHelper<t_oeeaddress>();
|
||||
var result = sQLiteHelper.InsertAll(addressList);
|
||||
|
||||
if (result.Success)
|
||||
{
|
||||
// 更新缓存
|
||||
var allAddresses = sQLiteHelper.QueryAll();
|
||||
if (allAddresses.Success)
|
||||
{
|
||||
Init.t_Oeeaddresses = allAddresses.Data;
|
||||
}
|
||||
|
||||
MessageBox.Show($"成功导入{addressList.Count}条数据");
|
||||
GetInfo(); // 刷新数据显示
|
||||
}
|
||||
else
|
||||
{
|
||||
MessageBox.Show($"导入失败:{result.Msg}");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show($"保存导入数据失败,错误原因:{ex.Message}");
|
||||
}
|
||||
}
|
||||
// 添加清空按钮点击事件处理
|
||||
private void 清空_Click(object sender, EventArgs e)
|
||||
{
|
||||
try
|
||||
{
|
||||
DialogResult result = MessageBox.Show("确定要删除所有数据吗?此操作不可恢复!", "警告",
|
||||
MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
|
||||
|
||||
if (result == DialogResult.OK)
|
||||
{
|
||||
SQLiteHelper<t_oeeaddress> sQLiteHelper = new db.SQLiteHelper<t_oeeaddress>();
|
||||
var deleteResult = sQLiteHelper.DeleteAll();
|
||||
|
||||
if (deleteResult.Success)
|
||||
{
|
||||
MessageBox.Show("所有数据已成功删除!");
|
||||
GetInfo(); // 刷新数据显示
|
||||
}
|
||||
else
|
||||
{
|
||||
MessageBox.Show($"删除失败:{deleteResult.Msg}");
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show($"清空数据失败,错误原因:{ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user