This commit is contained in:
xyy
2026-03-23 10:18:09 +08:00
parent 052af5f895
commit 4d15257e9b
2 changed files with 199 additions and 202 deletions

View File

@@ -268,6 +268,18 @@ standarderror)
}
}
public void DeleteTestAllItems(int id)
{
using (var connection = new MySqlConnection(_connectionString))
{
connection.Open();
var sql = @"delete from normaltemperature where id=@id
";
connection.Execute(sql, new { id });
}
}
public List<ScanData> GetScanDataBylldh_jh(string jh)
{
using (var connection = new MySqlConnection(_connectionString))

View File

@@ -121,7 +121,16 @@ namespace 全自动水压检测仪
dataGridView.Columns.Clear();
dataGridView.ScrollBars = ScrollBars.Both;
// 添加复选框列(放在最前面)
DataGridViewCheckBoxColumn selectColumn = new DataGridViewCheckBoxColumn
{
Name = "SelectColumn",
HeaderText = "选择",
Width = 50,
SortMode = DataGridViewColumnSortMode.NotSortable,
DefaultCellStyle = { Alignment = DataGridViewContentAlignment.MiddleCenter }
};
dataGridView.Columns.Add(selectColumn);
var config = ConfigurationManager.AppSettings;
if (config["AllowModified"]?.ToString() == "1")
{
@@ -373,7 +382,7 @@ namespace 全自动水压检测仪
// 导出报表按钮
btnExport = new Button
{
Text = "导出报表",
Text = "导出选中报表",
Width = 100,
Height = 35,
Font = new Font(this.Font, FontStyle.Regular),
@@ -408,7 +417,7 @@ namespace 全自动水压检测仪
// 导出报表按钮
btnDelete = new Button
{
Text = "一键清除",
Text = "清除选中",
Width = 100,
Height = 35,
Font = new Font(this.Font, FontStyle.Regular),
@@ -574,6 +583,13 @@ namespace 全自动水压检测仪
});
dataGridView.DataSource = CurrentReport;
foreach (DataGridViewRow row in dataGridView.Rows)
{
if (row.Cells["SelectColumn"].Value == null)
row.Cells["SelectColumn"].Value = false;
}
// 刷新显示
dataGridView.Refresh();
@@ -684,14 +700,47 @@ namespace 全自动水压检测仪
}
}
}
/// <summary>
/// 导出报表按钮点击事件
/// </summary>
///// <summary>
///// 导出报表按钮点击事件
///// </summary>
//private void BtnExport_Click(object sender, EventArgs e)
//{
// ExportReport();
//}
private void BtnExport_Click(object sender, EventArgs e)
{
ExportReport();
// 获取选中的行
var selectedItems = dataGridView.Rows.Cast<DataGridViewRow>()
.Where(row => row.Cells["SelectColumn"].Value != null && (bool)row.Cells["SelectColumn"].Value)
.Select(row => row.DataBoundItem as ConductivityTestData)
.Where(item => item != null)
.ToList();
if (selectedItems.Count == 0)
{
MessageBox.Show("请至少选择一行数据!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
try
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "Excel文件|*.xlsx";
saveFileDialog.Title = "导出报表";
saveFileDialog.FileName = $"测试报表_{DateTime.Now:yyyyMMdd_HHmmss}";
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
ExportToExcel(saveFileDialog.FileName, selectedItems);
MessageBox.Show($"报表已成功导出到:\n{saveFileDialog.FileName}", "导出成功",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (Exception ex)
{
MessageBox.Show($"导出报表失败:{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
/// <summary>
/// 返回按钮点击事件
/// </summary>
@@ -700,205 +749,141 @@ namespace 全自动水压检测仪
ReturnToMain();
}
/// <summary>
/// 返回按钮点击事件
/// </summary>
///// <summary>
///// 返回按钮点击事件
///// </summary>
//private void BtnDelete_Click(object sender, EventArgs e)
//{
// DialogResult ask = MessageBox.Show("此操作会清空所有历史数据,请先导出?",
// "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
// if (ask == DialogResult.Yes)
// {
// CurrentReport.Clear();
// dataGridView.DataSource = new List<ConductivityTestData>();
// _repository.DeleteTestAllItems();
// // 刷新显示
// dataGridView.Refresh();
// }
//}
private void BtnDelete_Click(object sender, EventArgs e)
{
DialogResult ask = MessageBox.Show("此操作会清空所有历史数据,请先导出?",
// 获取选中的行
var selectedRows = dataGridView.Rows.Cast<DataGridViewRow>()
.Where(row => row.Cells["SelectColumn"].Value != null && (bool)row.Cells["SelectColumn"].Value)
.ToList();
if (selectedRows.Count == 0)
{
MessageBox.Show("请至少选择一行数据!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
DialogResult ask = MessageBox.Show($"确定要删除选中的 {selectedRows.Count} 条数据吗?",
"确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (ask == DialogResult.Yes)
{
CurrentReport.Clear();
dataGridView.DataSource = new List<ConductivityTestData>();
_repository.DeleteTestAllItems();
// 刷新显示
dataGridView.Refresh();
}
}
/// <summary>
/// 导出报表功能
/// </summary>
private void ExportReport()
{
//if (CurrentReport == null || CurrentReport.Count == 0)
//{
// MessageBox.Show("没有数据可以导出", "提示",
// MessageBoxButtons.OK, MessageBoxIcon.Information);
// return;
//}
try
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "CSV文件|*.csv|Excel文件|*.xlsx|文本文件|*.txt";
saveFileDialog.Title = "导出报表";
saveFileDialog.FileName = $"测试报表_{DateTime.Now:yyyyMMdd_HHmmss}";
if (saveFileDialog.ShowDialog() == DialogResult.OK)
// 收集要删除的ID
List<int> idsToDelete = new List<int>();
foreach (var row in selectedRows)
{
string filePath = saveFileDialog.FileName;
//switch (Path.GetExtension(filePath).ToLower())
//{
//case ".csv":
// ExportToCsv(filePath);
// break;
//case ".txt":
// ExportToTxt(filePath);
// break;
//case ".xlsx":
ExportToExcel(filePath);
// break;
// default:
// ExportToCsv(filePath);
// break;
//}
MessageBox.Show($"报表已成功导出到:\n{filePath}", "导出成功",
MessageBoxButtons.OK, MessageBoxIcon.Information);
var data = row.DataBoundItem as ConductivityTestData;
if (data != null && data.Id > 0)
{
idsToDelete.Add(data.Id);
}
}
// 调用仓储删除方法(假设有一个批量删除的方法)
// 如果只有单个删除,则循环调用
foreach (int id in idsToDelete)
{
_repository.DeleteTestAllItems(id); // 需要在 Repository 中实现此方法
}
// 从内存数据源中移除被删除的对象
CurrentReport.RemoveAll(d => idsToDelete.Contains(d.Id));
// 重新绑定数据源(或直接刷新显示)
dataGridView.DataSource = null;
dataGridView.DataSource = CurrentReport;
// 刷新序号Id 列显示行号)
for (int i = 0; i < CurrentReport.Count; i++)
{
CurrentReport[i].row = i + 1;
}
dataGridView.Refresh();
MessageBox.Show($"已删除 {selectedRows.Count} 条数据。", "操作完成",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show($"导出报表失败:{ex.Message}", "错误",
MessageBoxButtons.OK, MessageBoxIcon.Error);
MessageBox.Show($"删除失败:{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
/// <summary>
/// 导出为Excel文件简单实现
/// </summary>
//private void ExportToExcel(string filePath)
}
///// <summary>
///// 导出报表功能
///// </summary>
//private void ExportReport()
//{
// using (var package = new ExcelPackage())
// //if (CurrentReport == null || CurrentReport.Count == 0)
// //{
// // MessageBox.Show("没有数据可以导出", "提示",
// // MessageBoxButtons.OK, MessageBoxIcon.Information);
// // return;
// //}
// try
// {
// var worksheet = package.Workbook.Worksheets.Add("测试数据报表");
// SaveFileDialog saveFileDialog = new SaveFileDialog();
// saveFileDialog.Filter = "CSV文件|*.csv|Excel文件|*.xlsx|文本文件|*.txt";
// saveFileDialog.Title = "导出报表";
// saveFileDialog.FileName = $"测试报表_{DateTime.Now:yyyyMMdd_HHmmss}";
// // 设置默认样式
// worksheet.Cells.Style.Font.Name = "微软雅黑";
// worksheet.Cells.Style.Font.Size = 11;
// int currentRow = 1;
// // 主标题
// worksheet.Cells[currentRow, 1, currentRow, 9].Merge = true;
// var titleCell = worksheet.Cells[currentRow, 1];
// titleCell.Value = "测试数据报表";
// titleCell.Style.Font.Size = 16;
// titleCell.Style.Font.Bold = true;
// titleCell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
// titleCell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
// currentRow++;
// // 副标题
// worksheet.Cells[currentRow, 1, currentRow, 9].Merge = true;
// var subTitleCell = worksheet.Cells[currentRow, 1];
// subTitleCell.Value = $"生成时间:{DateTime.Now:yyyy-MM-dd HH:mm:ss} | 记录数量:{CurrentReport.Count}条";
// subTitleCell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
// subTitleCell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
// currentRow += 2;
// // 表头
// string[] headers = { "编号", "联络单号", "件号", "刻字号", "数量", "初始压力(PSI)", "开始时间", "结束时间", "结束压力(PSI)", "保压时间(h)", "压差(PSI)", "温度模式", "温度", "标准差值(PSI)", "测试结果", "时间日期" };
// for (int i = 0; i < headers.Length; i++)
// if (saveFileDialog.ShowDialog() == DialogResult.OK)
// {
// var cell = worksheet.Cells[currentRow, i + 1];
// cell.Value = headers[i];
// cell.Style.Font.Bold = true;
// cell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
// cell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
// cell.Style.Fill.PatternType = ExcelFillStyle.Solid;
// cell.Style.Fill.BackgroundColor.SetColor(Color.LightSkyBlue);
// cell.Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.Black);
// // 设置列宽
// worksheet.Column(i + 1).Width = 15;
// }
// // 设置表头行高
// worksheet.Row(currentRow).Height = 35;
// currentRow++;
// // 数据行
// foreach (var data in CurrentReport)
// {
// string tempMode = GetTemperatureModeDisplay(data.Type);
// worksheet.Cells[currentRow, 1].Value = data.Id;
// worksheet.Cells[currentRow, 2].Value = data.lldh ?? "";
// worksheet.Cells[currentRow, 3].Value = data.jh ?? "";
// worksheet.Cells[currentRow, 4].Value = data.kzh ?? "";
// worksheet.Cells[currentRow, 5].Value = data.quantity;
// worksheet.Cells[currentRow, 6].Value = data.startpressure;
// string filePath = saveFileDialog.FileName;
// //switch (Path.GetExtension(filePath).ToLower())
// //{
// //case ".csv":
// // ExportToCsv(filePath);
// // break;
// //case ".txt":
// // ExportToTxt(filePath);
// // break;
// //case ".xlsx":
// ExportToExcel(filePath);
// // break;
// // default:
// // ExportToCsv(filePath);
// // break;
// //}
// worksheet.Cells[currentRow, 7].Value = data.starttime;
// worksheet.Cells[currentRow, 8].Value = data.endtime;
// worksheet.Cells[currentRow, 9].Value = data.endpressure;
// worksheet.Cells[currentRow, 10].Value = data.dwelltime;
// worksheet.Cells[currentRow, 11].Value = data.diffpressure;
// worksheet.Cells[currentRow, 12].Value = tempMode;
// worksheet.Cells[currentRow, 13].Value = data.temperature;
// worksheet.Cells[currentRow, 14].Value = data.standarderror;
// worksheet.Cells[currentRow, 15].Value = data.testresult;
// worksheet.Cells[currentRow, 16].Value = data.CreateTime;
// // 设置数据行样式
// for (int col = 1; col <= 9; col++)
// {
// var cell = worksheet.Cells[currentRow, col];
// cell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
// cell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
// cell.Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.LightGray);
// // 隔行变色
// if (currentRow % 2 == 0)
// {
// cell.Style.Fill.PatternType = ExcelFillStyle.Solid;
// cell.Style.Fill.BackgroundColor.SetColor(Color.AliceBlue);
// MessageBox.Show($"报表已成功导出到:\n{filePath}", "导出成功",
// MessageBoxButtons.OK, MessageBoxIcon.Information);
// }
// }
// // 设置格式
// worksheet.Cells[currentRow, 4].Style.Numberformat.Format = "yyyy-mm-dd hh:mm:ss";
// worksheet.Cells[currentRow, 5].Style.Numberformat.Format = "0.00";
// worksheet.Cells[currentRow, 6].Style.Numberformat.Format = "0.00";
// worksheet.Cells[currentRow, 7].Style.Numberformat.Format = "0.00";
// worksheet.Cells[currentRow, 8].Style.Numberformat.Format = "0.0";
// // 设置行高
// worksheet.Row(currentRow).Height = 25;
// currentRow++;
// }
// // 自动调整列宽
// worksheet.Cells[worksheet.Dimension.Address].AutoFitColumns();
// // 保存
// package.SaveAs(new FileInfo(filePath));
// catch (Exception ex)
// {
// MessageBox.Show($"导出报表失败:{ex.Message}", "错误",
// MessageBoxButtons.OK, MessageBoxIcon.Error);
// }
//}
private void ExportToExcel(string filePath)
private void ExportToExcel(string filePath, List<ConductivityTestData> dataList)
{
using (var package = new ExcelPackage())
{
@@ -927,7 +912,7 @@ namespace 全自动水压检测仪
// 副标题
worksheet.Cells[currentRow, 1, currentRow, 16].Merge = true;
var subTitleCell = worksheet.Cells[currentRow, 1];
subTitleCell.Value = $"生成时间:{DateTime.Now:yyyy-MM-dd HH:mm:ss} | 记录数量:{CurrentReport.Count}条";
subTitleCell.Value = $"生成时间:{DateTime.Now:yyyy-MM-dd HH:mm:ss} | 记录数量:{dataList.Count}条";
subTitleCell.Style.Font.Size = 11;
subTitleCell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
subTitleCell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
@@ -981,7 +966,7 @@ namespace 全自动水压检测仪
currentRow++;
// 数据行
foreach (var data in CurrentReport)
foreach (var data in dataList)
{
string tempMode = GetTemperatureModeDisplay(data.Type);