This commit is contained in:
@@ -3,6 +3,7 @@ using OfficeOpenXml;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Configuration;
|
||||
using System.Data.SQLite;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
@@ -22,6 +23,8 @@ namespace ShanghaiEnvironmentalTechnology
|
||||
|
||||
public partial class ReportWindow1 : Window
|
||||
{
|
||||
|
||||
private readonly string _lang = ConfigurationManager.AppSettings["Language"] ?? "zh-CN";
|
||||
private const int PageSize = 10;
|
||||
private int currentPage = 1;
|
||||
private int totalRecords = 0;
|
||||
@@ -125,8 +128,10 @@ namespace ShanghaiEnvironmentalTechnology
|
||||
|
||||
private void UpdatePageInfo()
|
||||
{
|
||||
// 保持原有分页信息逻辑
|
||||
PageInfo.Text = $"第 {currentPage} 页 / 共 {Math.Ceiling((double)totalRecords / PageSize)} 页 {totalRecords}条记录";
|
||||
if (_lang == "en-US")
|
||||
PageInfo.Text = $"Page {currentPage} / {Math.Ceiling((double)totalRecords / PageSize)} | Total: {totalRecords} records";
|
||||
else
|
||||
PageInfo.Text = $"第 {currentPage} 页 / 共 {Math.Ceiling((double)totalRecords / PageSize)} 页 {totalRecords}条记录";
|
||||
}
|
||||
|
||||
private void PreviousPage_Click(object sender, RoutedEventArgs e)
|
||||
@@ -162,33 +167,43 @@ namespace ShanghaiEnvironmentalTechnology
|
||||
{
|
||||
if (sender is FrameworkElement element && element.Tag is int id)
|
||||
{
|
||||
var result = MessageBox.Show($"确定要删除ID为 {id} 的记录吗?", "确认删除",
|
||||
MessageBoxButton.YesNo, MessageBoxImage.Warning);
|
||||
MessageBoxResult result;
|
||||
|
||||
if (_lang == "en-US")
|
||||
result = MessageBox.Show($"Delete record ID: {id} ?", "Confirm Delete", MessageBoxButton.YesNo, MessageBoxImage.Warning);
|
||||
else
|
||||
result = MessageBox.Show($"确定要删除ID为 {id} 的记录吗?", "确认删除", MessageBoxButton.YesNo, MessageBoxImage.Warning);
|
||||
|
||||
if (result == MessageBoxResult.Yes)
|
||||
{
|
||||
DeleteRecordFromDb(id);
|
||||
// 刷新当前页数据
|
||||
LoadData(_currentStartDate, _currentEndDate);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 新增:批量删除功能
|
||||
|
||||
private void BatchDeleteButton_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
var selectedIds = _records.Where(r => r.IsSelected).Select(r => r.Id).ToList();
|
||||
if (!selectedIds.Any())
|
||||
{
|
||||
MessageBox.Show("请先选择要删除的记录", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
|
||||
if (_lang == "en-US")
|
||||
MessageBox.Show("Please select records to delete", "Tip", MessageBoxButton.OK, MessageBoxImage.Information);
|
||||
else
|
||||
MessageBox.Show("请先选择要删除的记录", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
|
||||
return;
|
||||
}
|
||||
|
||||
var result = MessageBox.Show($"确定要删除选中的 {selectedIds.Count} 条记录吗?", "确认批量删除",
|
||||
MessageBoxButton.YesNo, MessageBoxImage.Warning);
|
||||
MessageBoxResult result;
|
||||
if (_lang == "en-US")
|
||||
result = MessageBox.Show($"Delete {selectedIds.Count} selected records?", "Confirm Batch Delete", MessageBoxButton.YesNo, MessageBoxImage.Warning);
|
||||
else
|
||||
result = MessageBox.Show($"确定要删除选中的 {selectedIds.Count} 条记录吗?", "确认批量删除", MessageBoxButton.YesNo, MessageBoxImage.Warning);
|
||||
|
||||
if (result == MessageBoxResult.Yes)
|
||||
{
|
||||
BatchDeleteFromDb(selectedIds);
|
||||
// 刷新当前页数据
|
||||
LoadData(_currentStartDate, _currentEndDate);
|
||||
}
|
||||
}
|
||||
@@ -238,69 +253,79 @@ namespace ShanghaiEnvironmentalTechnology
|
||||
}
|
||||
|
||||
DateTime? endDate = EndDatePicker.SelectedDate;
|
||||
|
||||
if (endDate.HasValue)
|
||||
{
|
||||
endDate = endDate.Value.Date.AddDays(1).AddTicks(-1);
|
||||
}
|
||||
|
||||
// 步骤2:读取CO2表数据
|
||||
List<CO2Record> co2Records = ReadCO2RecordsFromDatabase(startDate, endDate);
|
||||
if (co2Records == null || !co2Records.Any())
|
||||
{
|
||||
MessageBox.Show("排气流表中无数据,无法导出", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
|
||||
if (_lang == "en-US")
|
||||
MessageBox.Show("No data to export", "Tip", MessageBoxButton.OK, MessageBoxImage.Warning);
|
||||
else
|
||||
MessageBox.Show("排气流表中无数据,无法导出", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
|
||||
return;
|
||||
}
|
||||
|
||||
// 步骤3:导出Excel
|
||||
bool exportSuccess = ExportCO2RecordsToExcel(co2Records);
|
||||
if (exportSuccess)
|
||||
{
|
||||
MessageBox.Show("数据已成功导出到Excel", "成功", MessageBoxButton.OK, MessageBoxImage.Information);
|
||||
if (_lang == "en-US")
|
||||
MessageBox.Show("Export successful!", "Success", MessageBoxButton.OK, MessageBoxImage.Information);
|
||||
else
|
||||
MessageBox.Show("数据已成功导出到Excel", "成功", MessageBoxButton.OK, MessageBoxImage.Information);
|
||||
}
|
||||
else
|
||||
{
|
||||
MessageBox.Show("Excel导出失败,请检查文件是否被占用", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
|
||||
if (_lang == "en-US")
|
||||
MessageBox.Show("Export failed! File may be in use.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
|
||||
else
|
||||
MessageBox.Show("Excel导出失败,请检查文件是否被占用", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private bool ExportCO2RecordsToExcel(List<CO2Record> records)
|
||||
{
|
||||
try
|
||||
{
|
||||
// WPF 原生保存对话框(替代 WinForms)
|
||||
SaveFileDialog saveDialog = new SaveFileDialog
|
||||
{
|
||||
Filter = "Excel文件 (*.xlsx)|*.xlsx",
|
||||
FileName = $"排气流记录_{DateTime.Now:yyyyMMddHHmmss}.xlsx",
|
||||
Title = "保存排气流记录"
|
||||
Filter = _lang == "en-US" ? "Excel File (*.xlsx)|*.xlsx" : "Excel文件 (*.xlsx)|*.xlsx",
|
||||
FileName = $"FlowRecords_{DateTime.Now:yyyyMMddHHmmss}.xlsx",
|
||||
Title = _lang == "en-US" ? "Save Flow Records" : "保存排气流记录"
|
||||
};
|
||||
|
||||
bool? result = saveDialog.ShowDialog(); if (!(result ?? false)) return false;
|
||||
bool? result = saveDialog.ShowDialog();
|
||||
if (!(result ?? false)) return false;
|
||||
|
||||
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
|
||||
|
||||
|
||||
// EPPlus 许可设置(.NET 8 必须显式设置)
|
||||
ExcelPackage.LicenseContext = LicenseContext.NonCommercial; // 非商业用途
|
||||
|
||||
// 创建并写入Excel
|
||||
using (ExcelPackage package = new ExcelPackage(new FileInfo(saveDialog.FileName)))
|
||||
{
|
||||
ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("排气流记录");
|
||||
ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("Flow Records");
|
||||
|
||||
// 表头(对应DataGrid列)
|
||||
worksheet.Cells[1, 1].Value = "排气流流量L/min";
|
||||
worksheet.Cells[1, 2].Value = "压力(pa)";
|
||||
worksheet.Cells[1, 3].Value = "时间";
|
||||
// 双语表头
|
||||
if (_lang == "en-US")
|
||||
{
|
||||
worksheet.Cells[1, 1].Value = "Flow (L/min)";
|
||||
worksheet.Cells[1, 2].Value = "Pressure (Pa)";
|
||||
worksheet.Cells[1, 3].Value = "Time";
|
||||
}
|
||||
else
|
||||
{
|
||||
worksheet.Cells[1, 1].Value = "排气流流量L/min";
|
||||
worksheet.Cells[1, 2].Value = "压力(pa)";
|
||||
worksheet.Cells[1, 3].Value = "时间";
|
||||
}
|
||||
|
||||
// 表头样式(加粗、居中)
|
||||
using (var headerRange = worksheet.Cells[1, 1, 1, 6])
|
||||
{
|
||||
headerRange.Style.Font.Bold = true;
|
||||
headerRange.Style.HorizontalAlignment = OfficeOpenXml.Style.ExcelHorizontalAlignment.Center;
|
||||
}
|
||||
|
||||
// 填充数据(从第2行开始)
|
||||
for (int i = 0; i < records.Count; i++)
|
||||
{
|
||||
int row = i + 2;
|
||||
@@ -310,18 +335,17 @@ namespace ShanghaiEnvironmentalTechnology
|
||||
worksheet.Cells[row, 3].Value = record.RecordTime.ToString("yyyy-MM-dd HH:mm:ss");
|
||||
}
|
||||
|
||||
// 自动调整列宽
|
||||
worksheet.Cells.AutoFitColumns();
|
||||
|
||||
// 保存文件
|
||||
package.Save();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show($"导出失败:{ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
|
||||
if (_lang == "en-US")
|
||||
MessageBox.Show($"Export failed: {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
|
||||
else
|
||||
MessageBox.Show($"导出失败:{ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ using OfficeOpenXml;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Configuration;
|
||||
using System.Data.SQLite;
|
||||
using System.IO;
|
||||
using System.Windows;
|
||||
@@ -19,6 +20,8 @@ namespace ShanghaiEnvironmentalTechnology
|
||||
|
||||
public partial class ReportWindow2 : Window
|
||||
{
|
||||
private readonly string _lang = ConfigurationManager.AppSettings["Language"] ?? "zh-CN";
|
||||
|
||||
private const int PageSize = 10;
|
||||
private int currentPage = 1;
|
||||
private int totalRecords = 0;
|
||||
@@ -119,7 +122,10 @@ namespace ShanghaiEnvironmentalTechnology
|
||||
|
||||
private void UpdatePageInfo()
|
||||
{
|
||||
PageInfo.Text = $"第 {currentPage} 页 / 共 {Math.Ceiling((double)totalRecords / PageSize)} 页 {totalRecords}条记录";
|
||||
if (_lang == "en-US")
|
||||
PageInfo.Text = $"Page {currentPage} / {Math.Ceiling((double)totalRecords / PageSize)} | Total: {totalRecords} records";
|
||||
else
|
||||
PageInfo.Text = $"第 {currentPage} 页 / 共 {Math.Ceiling((double)totalRecords / PageSize)} 页 {totalRecords}条记录";
|
||||
}
|
||||
|
||||
private void PreviousPage_Click(object sender, RoutedEventArgs e)
|
||||
@@ -153,12 +159,16 @@ namespace ShanghaiEnvironmentalTechnology
|
||||
{
|
||||
if (sender is FrameworkElement element && element.Tag is int id)
|
||||
{
|
||||
var result = MessageBox.Show($"确定要删除ID为 {id} 的记录吗?", "确认删除",
|
||||
MessageBoxButton.YesNo, MessageBoxImage.Warning);
|
||||
MessageBoxResult result;
|
||||
|
||||
if (_lang == "en-US")
|
||||
result = MessageBox.Show($"Delete record ID: {id} ?", "Confirm Delete", MessageBoxButton.YesNo, MessageBoxImage.Warning);
|
||||
else
|
||||
result = MessageBox.Show($"确定要删除ID为 {id} 的记录吗?", "确认删除", MessageBoxButton.YesNo, MessageBoxImage.Warning);
|
||||
|
||||
if (result == MessageBoxResult.Yes)
|
||||
{
|
||||
DeleteRecordFromDb(id);
|
||||
// 刷新当前页数据
|
||||
LoadData(_currentStartDate, _currentEndDate);
|
||||
}
|
||||
}
|
||||
@@ -170,16 +180,22 @@ namespace ShanghaiEnvironmentalTechnology
|
||||
var selectedIds = _records.Where(r => r.IsSelected).Select(r => r.Id).ToList();
|
||||
if (!selectedIds.Any())
|
||||
{
|
||||
MessageBox.Show("请先选择要删除的记录", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
|
||||
if (_lang == "en-US")
|
||||
MessageBox.Show("Please select records to delete", "Tip", MessageBoxButton.OK, MessageBoxImage.Information);
|
||||
else
|
||||
MessageBox.Show("请先选择要删除的记录", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
|
||||
return;
|
||||
}
|
||||
|
||||
var result = MessageBox.Show($"确定要删除选中的 {selectedIds.Count} 条记录吗?", "确认批量删除",
|
||||
MessageBoxButton.YesNo, MessageBoxImage.Warning);
|
||||
MessageBoxResult result;
|
||||
if (_lang == "en-US")
|
||||
result = MessageBox.Show($"Delete {selectedIds.Count} selected records?", "Confirm Batch Delete", MessageBoxButton.YesNo, MessageBoxImage.Warning);
|
||||
else
|
||||
result = MessageBox.Show($"确定要删除选中的 {selectedIds.Count} 条记录吗?", "确认批量删除", MessageBoxButton.YesNo, MessageBoxImage.Warning);
|
||||
|
||||
if (result == MessageBoxResult.Yes)
|
||||
{
|
||||
BatchDeleteFromDb(selectedIds);
|
||||
// 刷新当前页数据
|
||||
LoadData(_currentStartDate, _currentEndDate);
|
||||
}
|
||||
}
|
||||
@@ -222,7 +238,6 @@ namespace ShanghaiEnvironmentalTechnology
|
||||
|
||||
private void Export_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
|
||||
DateTime? startDate = null;
|
||||
if (DateTime.TryParse(StartDatePicker.SelectedDate?.ToString(), out DateTime tempStartDate))
|
||||
{
|
||||
@@ -230,94 +245,103 @@ namespace ShanghaiEnvironmentalTechnology
|
||||
}
|
||||
|
||||
DateTime? endDate = EndDatePicker.SelectedDate;
|
||||
|
||||
if (endDate.HasValue)
|
||||
{
|
||||
endDate = endDate.Value.Date.AddDays(1).AddTicks(-1);
|
||||
}
|
||||
|
||||
// 步骤2:读取CO2表数据
|
||||
List<CO2Record> co2Records = ReadCO2RecordsFromDatabase(startDate, endDate);
|
||||
|
||||
if (co2Records == null || !co2Records.Any())
|
||||
List<CO2Record> records = ReadCO2RecordsFromDatabase(startDate, endDate);
|
||||
if (records == null || !records.Any())
|
||||
{
|
||||
MessageBox.Show("气阻表中无数据,无法导出", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
|
||||
if (_lang == "en-US")
|
||||
MessageBox.Show("No data to export", "Tip", MessageBoxButton.OK, MessageBoxImage.Warning);
|
||||
else
|
||||
MessageBox.Show("气阻表中无数据,无法导出", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
|
||||
return;
|
||||
}
|
||||
|
||||
// 步骤3:导出Excel
|
||||
bool exportSuccess = ExportCO2RecordsToExcel(co2Records);
|
||||
if (exportSuccess)
|
||||
bool success = ExportCO2RecordsToExcel(records);
|
||||
if (success)
|
||||
{
|
||||
MessageBox.Show("数据已成功导出到Excel", "成功", MessageBoxButton.OK, MessageBoxImage.Information);
|
||||
if (_lang == "en-US")
|
||||
MessageBox.Show("Export successful!", "Success", MessageBoxButton.OK, MessageBoxImage.Information);
|
||||
else
|
||||
MessageBox.Show("数据已成功导出到Excel", "成功", MessageBoxButton.OK, MessageBoxImage.Information);
|
||||
}
|
||||
else
|
||||
{
|
||||
MessageBox.Show("Excel导出失败,请检查文件是否被占用", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
|
||||
if (_lang == "en-US")
|
||||
MessageBox.Show("Export failed! File may be in use.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
|
||||
else
|
||||
MessageBox.Show("Excel导出失败,请检查文件是否被占用", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private bool ExportCO2RecordsToExcel(List<CO2Record> records)
|
||||
{
|
||||
try
|
||||
{
|
||||
SaveFileDialog saveDialog = new SaveFileDialog
|
||||
{
|
||||
Filter = "Excel文件 (*.xlsx)|*.xlsx",
|
||||
FileName = $"气阻记录_{DateTime.Now:yyyyMMddHHmmss}.xlsx",
|
||||
Title = "保存气阻记录"
|
||||
Filter = _lang == "en-US" ? "Excel File (*.xlsx)|*.xlsx" : "Excel文件 (*.xlsx)|*.xlsx",
|
||||
FileName = $"AirResistance_{DateTime.Now:yyyyMMddHHmmss}.xlsx",
|
||||
Title = _lang == "en-US" ? "Save Air Resistance Records" : "保存气阻记录"
|
||||
};
|
||||
|
||||
// 用户取消选择则返回
|
||||
bool? result = saveDialog.ShowDialog(); if (!(result ?? false)) return false;
|
||||
bool? result = saveDialog.ShowDialog();
|
||||
if (!(result ?? false)) return false;
|
||||
|
||||
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
|
||||
|
||||
// EPPlus 许可设置(.NET 8 必须显式设置)
|
||||
ExcelPackage.LicenseContext = LicenseContext.NonCommercial; // 非商业用途
|
||||
|
||||
// 创建并写入Excel
|
||||
using (ExcelPackage package = new ExcelPackage(new FileInfo(saveDialog.FileName)))
|
||||
{
|
||||
ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("气阻记录");
|
||||
ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("Air Resistance");
|
||||
|
||||
// 表头(对应DataGrid列)
|
||||
worksheet.Cells[1, 1].Value = "气阻hpa";
|
||||
worksheet.Cells[1, 2].Value = "流量L/min";
|
||||
worksheet.Cells[1, 3].Value = "时间";
|
||||
|
||||
// 表头样式(加粗、居中)
|
||||
using (var headerRange = worksheet.Cells[1, 1, 1, 6])
|
||||
// 双语表头
|
||||
if (_lang == "en-US")
|
||||
{
|
||||
headerRange.Style.Font.Bold = true;
|
||||
headerRange.Style.HorizontalAlignment = OfficeOpenXml.Style.ExcelHorizontalAlignment.Center;
|
||||
worksheet.Cells[1, 1].Value = "Air Resistance (hPa)";
|
||||
worksheet.Cells[1, 2].Value = "Flow (L/min)";
|
||||
worksheet.Cells[1, 3].Value = "Time";
|
||||
}
|
||||
else
|
||||
{
|
||||
worksheet.Cells[1, 1].Value = "气阻hpa";
|
||||
worksheet.Cells[1, 2].Value = "流量L/min";
|
||||
worksheet.Cells[1, 3].Value = "时间";
|
||||
}
|
||||
|
||||
using (var header = worksheet.Cells[1, 1, 1, 3])
|
||||
{
|
||||
header.Style.Font.Bold = true;
|
||||
header.Style.HorizontalAlignment = OfficeOpenXml.Style.ExcelHorizontalAlignment.Center;
|
||||
}
|
||||
|
||||
// 填充数据(从第2行开始)
|
||||
for (int i = 0; i < records.Count; i++)
|
||||
{
|
||||
int row = i + 2;
|
||||
var record = records[i];
|
||||
worksheet.Cells[row, 1].Value = record.Flow;
|
||||
worksheet.Cells[row, 2].Value = record.Pressure;
|
||||
worksheet.Cells[row, 3].Value = record.RecordTime.ToString("yyyy-MM-dd HH:mm:ss");
|
||||
worksheet.Cells[row, 1].Value = records[i].Flow;
|
||||
worksheet.Cells[row, 2].Value = records[i].Pressure;
|
||||
worksheet.Cells[row, 3].Value = records[i].RecordTime.ToString("yyyy-MM-dd HH:mm:ss");
|
||||
}
|
||||
|
||||
// 自动调整列宽
|
||||
worksheet.Cells.AutoFitColumns();
|
||||
|
||||
// 保存文件
|
||||
package.Save();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show($"导出失败:{ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
|
||||
if (_lang == "en-US")
|
||||
MessageBox.Show($"Export failed: {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
|
||||
else
|
||||
MessageBox.Show($"导出失败:{ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
|
||||
@@ -3,6 +3,7 @@ using OfficeOpenXml;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Configuration;
|
||||
using System.Data.SQLite;
|
||||
using System.IO;
|
||||
using System.Windows;
|
||||
@@ -19,6 +20,8 @@ namespace ShanghaiEnvironmentalTechnology
|
||||
}
|
||||
public partial class ReportWindow3 : Window
|
||||
{
|
||||
|
||||
private readonly string _lang = ConfigurationManager.AppSettings["Language"] ?? "zh-CN";
|
||||
private const int PageSize = 10;
|
||||
private int currentPage = 1;
|
||||
private int totalRecords = 0;
|
||||
@@ -116,9 +119,13 @@ namespace ShanghaiEnvironmentalTechnology
|
||||
LoadData(startDate, endDateString);
|
||||
}
|
||||
|
||||
|
||||
private void UpdatePageInfo()
|
||||
{
|
||||
PageInfo.Text = $"第 {currentPage} 页 / 共 {Math.Ceiling((double)totalRecords / PageSize)} 页 {totalRecords}条记录";
|
||||
if (_lang == "en-US")
|
||||
PageInfo.Text = $"Page {currentPage} / {Math.Ceiling((double)totalRecords / PageSize)} | Total: {totalRecords} records";
|
||||
else
|
||||
PageInfo.Text = $"第 {currentPage} 页 / 共 {Math.Ceiling((double)totalRecords / PageSize)} 页 {totalRecords}条记录";
|
||||
}
|
||||
|
||||
private void PreviousPage_Click(object sender, RoutedEventArgs e)
|
||||
@@ -148,41 +155,53 @@ namespace ShanghaiEnvironmentalTechnology
|
||||
}
|
||||
|
||||
|
||||
|
||||
private void DeleteButton_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
if (sender is FrameworkElement element && element.Tag is int id)
|
||||
{
|
||||
var result = MessageBox.Show($"确定要删除ID为 {id} 的记录吗?", "确认删除",
|
||||
MessageBoxButton.YesNo, MessageBoxImage.Warning);
|
||||
MessageBoxResult result;
|
||||
if (_lang == "en-US")
|
||||
result = MessageBox.Show($"Delete record ID: {id} ?", "Confirm Delete", MessageBoxButton.YesNo, MessageBoxImage.Warning);
|
||||
else
|
||||
result = MessageBox.Show($"确定要删除ID为 {id} 的记录吗?", "确认删除", MessageBoxButton.YesNo, MessageBoxImage.Warning);
|
||||
|
||||
if (result == MessageBoxResult.Yes)
|
||||
{
|
||||
DeleteRecordFromDb(id);
|
||||
// 刷新当前页数据
|
||||
LoadData(_currentStartDate, _currentEndDate);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 新增:批量删除功能
|
||||
|
||||
private void BatchDeleteButton_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
var selectedIds = _records.Where(r => r.IsSelected).Select(r => r.Id).ToList();
|
||||
if (!selectedIds.Any())
|
||||
{
|
||||
MessageBox.Show("请先选择要删除的记录", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
|
||||
if (_lang == "en-US")
|
||||
MessageBox.Show("Please select records to delete", "Tip", MessageBoxButton.OK, MessageBoxImage.Information);
|
||||
else
|
||||
MessageBox.Show("请先选择要删除的记录", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
|
||||
return;
|
||||
}
|
||||
|
||||
var result = MessageBox.Show($"确定要删除选中的 {selectedIds.Count} 条记录吗?", "确认批量删除",
|
||||
MessageBoxButton.YesNo, MessageBoxImage.Warning);
|
||||
MessageBoxResult result;
|
||||
if (_lang == "en-US")
|
||||
result = MessageBox.Show($"Delete {selectedIds.Count} selected records?", "Confirm Batch Delete", MessageBoxButton.YesNo, MessageBoxImage.Warning);
|
||||
else
|
||||
result = MessageBox.Show($"确定要删除选中的 {selectedIds.Count} 条记录吗?", "确认批量删除", MessageBoxButton.YesNo, MessageBoxImage.Warning);
|
||||
|
||||
if (result == MessageBoxResult.Yes)
|
||||
{
|
||||
BatchDeleteFromDb(selectedIds);
|
||||
// 刷新当前页数据
|
||||
LoadData(_currentStartDate, _currentEndDate);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 新增:数据库单条删除操作
|
||||
private void DeleteRecordFromDb(int id)
|
||||
{
|
||||
@@ -221,7 +240,6 @@ namespace ShanghaiEnvironmentalTechnology
|
||||
|
||||
private void Export_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
|
||||
DateTime? startDate = null;
|
||||
if (DateTime.TryParse(StartDatePicker.SelectedDate?.ToString(), out DateTime tempStartDate))
|
||||
{
|
||||
@@ -229,93 +247,102 @@ namespace ShanghaiEnvironmentalTechnology
|
||||
}
|
||||
|
||||
DateTime? endDate = EndDatePicker.SelectedDate;
|
||||
|
||||
if (endDate.HasValue)
|
||||
{
|
||||
endDate = endDate.Value.Date.AddDays(1).AddTicks(-1);
|
||||
}
|
||||
|
||||
// 步骤2:读取CO2表数据
|
||||
List<CO2Record> co2Records = ReadCO2RecordsFromDatabase(startDate, endDate);
|
||||
|
||||
if (co2Records == null || !co2Records.Any())
|
||||
List<CO2Record> records = ReadCO2RecordsFromDatabase(startDate, endDate);
|
||||
if (records == null || !records.Any())
|
||||
{
|
||||
MessageBox.Show("防室息阀压力表中无数据,无法导出", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
|
||||
if (_lang == "en-US")
|
||||
MessageBox.Show("No data to export", "Tip", MessageBoxButton.OK, MessageBoxImage.Warning);
|
||||
else
|
||||
MessageBox.Show("防窒息阀压力表中无数据,无法导出", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
|
||||
return;
|
||||
}
|
||||
|
||||
// 步骤3:导出Excel
|
||||
bool exportSuccess = ExportCO2RecordsToExcel(co2Records);
|
||||
if (exportSuccess)
|
||||
bool success = ExportCO2RecordsToExcel(records);
|
||||
if (success)
|
||||
{
|
||||
MessageBox.Show("数据已成功导出到Excel", "成功", MessageBoxButton.OK, MessageBoxImage.Information);
|
||||
if (_lang == "en-US")
|
||||
MessageBox.Show("Export successful!", "Success", MessageBoxButton.OK, MessageBoxImage.Information);
|
||||
else
|
||||
MessageBox.Show("数据已成功导出到Excel", "成功", MessageBoxButton.OK, MessageBoxImage.Information);
|
||||
}
|
||||
else
|
||||
{
|
||||
MessageBox.Show("Excel导出失败,请检查文件是否被占用", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
|
||||
if (_lang == "en-US")
|
||||
MessageBox.Show("Export failed! File may be in use.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
|
||||
else
|
||||
MessageBox.Show("Excel导出失败,请检查文件是否被占用", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
|
||||
}
|
||||
}
|
||||
|
||||
private bool ExportCO2RecordsToExcel(List<CO2Record> records)
|
||||
{
|
||||
try
|
||||
{
|
||||
SaveFileDialog saveDialog = new SaveFileDialog
|
||||
{
|
||||
Filter = "Excel文件 (*.xlsx)|*.xlsx",
|
||||
FileName = $"防室息阀压力记录_{DateTime.Now:yyyyMMddHHmmss}.xlsx",
|
||||
Title = "保存防室息阀压力记录"
|
||||
Filter = _lang == "en-US" ? "Excel File (*.xlsx)|*.xlsx" : "Excel文件 (*.xlsx)|*.xlsx",
|
||||
FileName = $"AntiSuffocation_{DateTime.Now:yyyyMMddHHmmss}.xlsx",
|
||||
Title = _lang == "en-US" ? "Save Anti-Suffocation Records" : "保存防窒息阀压力记录"
|
||||
};
|
||||
|
||||
// 用户取消选择则返回
|
||||
bool? result = saveDialog.ShowDialog(); if (!(result ?? false)) return false;
|
||||
bool? result = saveDialog.ShowDialog();
|
||||
if (!(result ?? false)) return false;
|
||||
|
||||
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
|
||||
|
||||
// EPPlus 许可设置(.NET 8 必须显式设置)
|
||||
ExcelPackage.LicenseContext = LicenseContext.NonCommercial; // 非商业用途
|
||||
|
||||
// 创建并写入Excel
|
||||
using (ExcelPackage package = new ExcelPackage(new FileInfo(saveDialog.FileName)))
|
||||
{
|
||||
ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("防室息阀压力记录");
|
||||
ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("Anti-Suffocation");
|
||||
|
||||
// 表头(对应DataGrid列)
|
||||
worksheet.Cells[1, 1].Value = "开阀压力pa";
|
||||
worksheet.Cells[1, 2].Value = "关阀压力pa";
|
||||
worksheet.Cells[1, 3].Value = "时间";
|
||||
|
||||
// 表头样式(加粗、居中)
|
||||
using (var headerRange = worksheet.Cells[1, 1, 1, 6])
|
||||
// 双语表头
|
||||
if (_lang == "en-US")
|
||||
{
|
||||
headerRange.Style.Font.Bold = true;
|
||||
headerRange.Style.HorizontalAlignment = OfficeOpenXml.Style.ExcelHorizontalAlignment.Center;
|
||||
worksheet.Cells[1, 1].Value = "Open Pressure (Pa)";
|
||||
worksheet.Cells[1, 2].Value = "Close Pressure (Pa)";
|
||||
worksheet.Cells[1, 3].Value = "Time";
|
||||
}
|
||||
else
|
||||
{
|
||||
worksheet.Cells[1, 1].Value = "开阀压力pa";
|
||||
worksheet.Cells[1, 2].Value = "关阀压力pa";
|
||||
worksheet.Cells[1, 3].Value = "时间";
|
||||
}
|
||||
|
||||
using (var header = worksheet.Cells[1, 1, 1, 3])
|
||||
{
|
||||
header.Style.Font.Bold = true;
|
||||
header.Style.HorizontalAlignment = OfficeOpenXml.Style.ExcelHorizontalAlignment.Center;
|
||||
}
|
||||
|
||||
// 填充数据(从第2行开始)
|
||||
for (int i = 0; i < records.Count; i++)
|
||||
{
|
||||
int row = i + 2;
|
||||
var record = records[i];
|
||||
worksheet.Cells[row, 1].Value = record.Flow;
|
||||
worksheet.Cells[row, 2].Value = record.Pressure;
|
||||
worksheet.Cells[row, 3].Value = record.RecordTime.ToString("yyyy-MM-dd HH:mm:ss");
|
||||
worksheet.Cells[row, 1].Value = records[i].Flow;
|
||||
worksheet.Cells[row, 2].Value = records[i].Pressure;
|
||||
worksheet.Cells[row, 3].Value = records[i].RecordTime.ToString("yyyy-MM-dd HH:mm:ss");
|
||||
}
|
||||
|
||||
// 自动调整列宽
|
||||
worksheet.Cells.AutoFitColumns();
|
||||
|
||||
// 保存文件
|
||||
package.Save();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show($"导出失败:{ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
|
||||
if (_lang == "en-US")
|
||||
MessageBox.Show($"Export failed: {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
|
||||
else
|
||||
MessageBox.Show($"导出失败:{ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
|
||||
@@ -3,6 +3,7 @@ using OfficeOpenXml;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Configuration;
|
||||
using System.Data.SQLite;
|
||||
using System.IO;
|
||||
using System.Windows;
|
||||
@@ -18,6 +19,9 @@ namespace ShanghaiEnvironmentalTechnology
|
||||
}
|
||||
public partial class ReportWindow4 : Window
|
||||
{
|
||||
|
||||
private readonly string _lang = ConfigurationManager.AppSettings["Language"] ?? "zh-CN";
|
||||
|
||||
private const int PageSize = 10;
|
||||
private int currentPage = 1;
|
||||
private int totalRecords = 0;
|
||||
@@ -117,7 +121,10 @@ namespace ShanghaiEnvironmentalTechnology
|
||||
|
||||
private void UpdatePageInfo()
|
||||
{
|
||||
PageInfo.Text = $"第 {currentPage} 页 / 共 {Math.Ceiling((double)totalRecords / PageSize)} 页 {totalRecords}条记录";
|
||||
if (_lang == "en-US")
|
||||
PageInfo.Text = $"Page {currentPage} / {Math.Ceiling((double)totalRecords / PageSize)} | Total: {totalRecords} records";
|
||||
else
|
||||
PageInfo.Text = $"第 {currentPage} 页 / 共 {Math.Ceiling((double)totalRecords / PageSize)} 页 {totalRecords}条记录";
|
||||
}
|
||||
|
||||
private void PreviousPage_Click(object sender, RoutedEventArgs e)
|
||||
@@ -151,12 +158,15 @@ namespace ShanghaiEnvironmentalTechnology
|
||||
{
|
||||
if (sender is FrameworkElement element && element.Tag is int id)
|
||||
{
|
||||
var result = MessageBox.Show($"确定要删除ID为 {id} 的记录吗?", "确认删除",
|
||||
MessageBoxButton.YesNo, MessageBoxImage.Warning);
|
||||
MessageBoxResult result;
|
||||
if (_lang == "en-US")
|
||||
result = MessageBox.Show($"Delete record ID: {id} ?", "Confirm Delete", MessageBoxButton.YesNo, MessageBoxImage.Warning);
|
||||
else
|
||||
result = MessageBox.Show($"确定要删除ID为 {id} 的记录吗?", "确认删除", MessageBoxButton.YesNo, MessageBoxImage.Warning);
|
||||
|
||||
if (result == MessageBoxResult.Yes)
|
||||
{
|
||||
DeleteRecordFromDb(id);
|
||||
// 刷新当前页数据
|
||||
LoadData(_currentStartDate, _currentEndDate);
|
||||
}
|
||||
}
|
||||
@@ -168,16 +178,22 @@ namespace ShanghaiEnvironmentalTechnology
|
||||
var selectedIds = _records.Where(r => r.IsSelected).Select(r => r.Id).ToList();
|
||||
if (!selectedIds.Any())
|
||||
{
|
||||
MessageBox.Show("请先选择要删除的记录", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
|
||||
if (_lang == "en-US")
|
||||
MessageBox.Show("Please select records to delete", "Tip", MessageBoxButton.OK, MessageBoxImage.Information);
|
||||
else
|
||||
MessageBox.Show("请先选择要删除的记录", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
|
||||
return;
|
||||
}
|
||||
|
||||
var result = MessageBox.Show($"确定要删除选中的 {selectedIds.Count} 条记录吗?", "确认批量删除",
|
||||
MessageBoxButton.YesNo, MessageBoxImage.Warning);
|
||||
MessageBoxResult result;
|
||||
if (_lang == "en-US")
|
||||
result = MessageBox.Show($"Delete {selectedIds.Count} selected records?", "Confirm Batch Delete", MessageBoxButton.YesNo, MessageBoxImage.Warning);
|
||||
else
|
||||
result = MessageBox.Show($"确定要删除选中的 {selectedIds.Count} 条记录吗?", "确认批量删除", MessageBoxButton.YesNo, MessageBoxImage.Warning);
|
||||
|
||||
if (result == MessageBoxResult.Yes)
|
||||
{
|
||||
BatchDeleteFromDb(selectedIds);
|
||||
// 刷新当前页数据
|
||||
LoadData(_currentStartDate, _currentEndDate);
|
||||
}
|
||||
}
|
||||
@@ -220,7 +236,6 @@ namespace ShanghaiEnvironmentalTechnology
|
||||
|
||||
private void Export_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
|
||||
DateTime? startDate = null;
|
||||
if (DateTime.TryParse(StartDatePicker.SelectedDate?.ToString(), out DateTime tempStartDate))
|
||||
{
|
||||
@@ -228,30 +243,35 @@ namespace ShanghaiEnvironmentalTechnology
|
||||
}
|
||||
|
||||
DateTime? endDate = EndDatePicker.SelectedDate;
|
||||
|
||||
if (endDate.HasValue)
|
||||
{
|
||||
endDate = endDate.Value.Date.AddDays(1).AddTicks(-1);
|
||||
}
|
||||
|
||||
// 步骤2:读取CO2表数据
|
||||
List<CO2Record> co2Records = ReadCO2RecordsFromDatabase(startDate, endDate);
|
||||
|
||||
if (co2Records == null || !co2Records.Any())
|
||||
List<CO2Record> records = ReadCO2RecordsFromDatabase(startDate, endDate);
|
||||
if (records == null || !records.Any())
|
||||
{
|
||||
MessageBox.Show("单一故障气阻表中无数据,无法导出", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
|
||||
if (_lang == "en-US")
|
||||
MessageBox.Show("No data to export", "Tip", MessageBoxButton.OK, MessageBoxImage.Warning);
|
||||
else
|
||||
MessageBox.Show("单一故障气阻表中无数据,无法导出", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
|
||||
return;
|
||||
}
|
||||
|
||||
// 步骤3:导出Excel
|
||||
bool exportSuccess = ExportCO2RecordsToExcel(co2Records);
|
||||
if (exportSuccess)
|
||||
bool success = ExportCO2RecordsToExcel(records);
|
||||
if (success)
|
||||
{
|
||||
MessageBox.Show("数据已成功导出到Excel", "成功", MessageBoxButton.OK, MessageBoxImage.Information);
|
||||
if (_lang == "en-US")
|
||||
MessageBox.Show("Export successful!", "Success", MessageBoxButton.OK, MessageBoxImage.Information);
|
||||
else
|
||||
MessageBox.Show("数据已成功导出到Excel", "成功", MessageBoxButton.OK, MessageBoxImage.Information);
|
||||
}
|
||||
else
|
||||
{
|
||||
MessageBox.Show("Excel导出失败,请检查文件是否被占用", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
|
||||
if (_lang == "en-US")
|
||||
MessageBox.Show("Export failed! File may be in use.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
|
||||
else
|
||||
MessageBox.Show("Excel导出失败,请检查文件是否被占用", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -260,60 +280,61 @@ namespace ShanghaiEnvironmentalTechnology
|
||||
{
|
||||
try
|
||||
{
|
||||
// WPF 原生保存对话框(替代 WinForms)
|
||||
SaveFileDialog saveDialog = new SaveFileDialog
|
||||
{
|
||||
Filter = "Excel文件 (*.xlsx)|*.xlsx",
|
||||
FileName = $"单一故障气阻记录_{DateTime.Now:yyyyMMddHHmmss}.xlsx",
|
||||
Title = "保存单一故障气阻记录"
|
||||
Filter = _lang == "en-US" ? "Excel File (*.xlsx)|*.xlsx" : "Excel文件 (*.xlsx)|*.xlsx",
|
||||
FileName = $"SingleFaultResistance_{DateTime.Now:yyyyMMddHHmmss}.xlsx",
|
||||
Title = _lang == "en-US" ? "Save Single Fault Resistance Records" : "保存单一故障气阻记录"
|
||||
};
|
||||
|
||||
// 用户取消选择则返回
|
||||
bool? result = saveDialog.ShowDialog(); if (!(result ?? false)) return false;
|
||||
bool? result = saveDialog.ShowDialog();
|
||||
if (!(result ?? false)) return false;
|
||||
|
||||
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
|
||||
|
||||
// EPPlus 许可设置(.NET 8 必须显式设置)
|
||||
ExcelPackage.LicenseContext = LicenseContext.NonCommercial; // 非商业用途
|
||||
|
||||
// 创建并写入Excel
|
||||
using (ExcelPackage package = new ExcelPackage(new FileInfo(saveDialog.FileName)))
|
||||
{
|
||||
ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("单一故障气阻记录");
|
||||
ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("Single Fault Resistance");
|
||||
|
||||
// 表头(对应DataGrid列)
|
||||
worksheet.Cells[1, 1].Value = "呼气阻力pa";
|
||||
worksheet.Cells[1, 2].Value = "吸气阻力pa";
|
||||
worksheet.Cells[1, 3].Value = "时间";
|
||||
|
||||
// 表头样式(加粗、居中)
|
||||
using (var headerRange = worksheet.Cells[1, 1, 1, 6])
|
||||
// 双语表头
|
||||
if (_lang == "en-US")
|
||||
{
|
||||
headerRange.Style.Font.Bold = true;
|
||||
headerRange.Style.HorizontalAlignment = OfficeOpenXml.Style.ExcelHorizontalAlignment.Center;
|
||||
worksheet.Cells[1, 1].Value = "Expiratory Resistance (Pa)";
|
||||
worksheet.Cells[1, 2].Value = "Inspiratory Resistance (Pa)";
|
||||
worksheet.Cells[1, 3].Value = "Time";
|
||||
}
|
||||
else
|
||||
{
|
||||
worksheet.Cells[1, 1].Value = "呼气阻力pa";
|
||||
worksheet.Cells[1, 2].Value = "吸气阻力pa";
|
||||
worksheet.Cells[1, 3].Value = "时间";
|
||||
}
|
||||
|
||||
using (var header = worksheet.Cells[1, 1, 1, 3])
|
||||
{
|
||||
header.Style.Font.Bold = true;
|
||||
header.Style.HorizontalAlignment = OfficeOpenXml.Style.ExcelHorizontalAlignment.Center;
|
||||
}
|
||||
|
||||
// 填充数据(从第2行开始)
|
||||
for (int i = 0; i < records.Count; i++)
|
||||
{
|
||||
int row = i + 2;
|
||||
var record = records[i];
|
||||
worksheet.Cells[row, 1].Value = record.Flow;
|
||||
worksheet.Cells[row, 2].Value = record.Pressure;
|
||||
worksheet.Cells[row, 3].Value = record.RecordTime.ToString("yyyy-MM-dd HH:mm:ss");
|
||||
worksheet.Cells[row, 1].Value = records[i].Flow;
|
||||
worksheet.Cells[row, 2].Value = records[i].Pressure;
|
||||
worksheet.Cells[row, 3].Value = records[i].RecordTime.ToString("yyyy-MM-dd HH:mm:ss");
|
||||
}
|
||||
|
||||
// 自动调整列宽
|
||||
worksheet.Cells.AutoFitColumns();
|
||||
|
||||
// 保存文件
|
||||
package.Save();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show($"导出失败:{ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
|
||||
if (_lang == "en-US")
|
||||
MessageBox.Show($"Export failed: {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
|
||||
else
|
||||
MessageBox.Show($"导出失败:{ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -374,7 +395,7 @@ namespace ShanghaiEnvironmentalTechnology
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show($"读取单一故障气阻表失败:{ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
|
||||
//MessageBox.Show($"读取单一故障气阻表失败:{ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ using OfficeOpenXml;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Configuration;
|
||||
using System.Data.SQLite;
|
||||
using System.IO;
|
||||
using System.Windows;
|
||||
@@ -18,6 +19,8 @@ namespace ShanghaiEnvironmentalTechnology
|
||||
}
|
||||
public partial class ReportWindow5 : Window
|
||||
{
|
||||
|
||||
private readonly string _lang = ConfigurationManager.AppSettings["Language"] ?? "zh-CN";
|
||||
private const int PageSize = 10;
|
||||
private int currentPage = 1;
|
||||
private int totalRecords = 0;
|
||||
@@ -119,7 +122,10 @@ namespace ShanghaiEnvironmentalTechnology
|
||||
|
||||
private void UpdatePageInfo()
|
||||
{
|
||||
PageInfo.Text = $"第 {currentPage} 页 / 共 {Math.Ceiling((double)totalRecords / PageSize)} 页 {totalRecords}条记录";
|
||||
if (_lang == "en-US")
|
||||
PageInfo.Text = $"Page {currentPage} / {Math.Ceiling((double)totalRecords / PageSize)} | Total: {totalRecords} records";
|
||||
else
|
||||
PageInfo.Text = $"第 {currentPage} 页 / 共 {Math.Ceiling((double)totalRecords / PageSize)} 页 {totalRecords}条记录";
|
||||
}
|
||||
|
||||
private void PreviousPage_Click(object sender, RoutedEventArgs e)
|
||||
@@ -148,37 +154,48 @@ namespace ShanghaiEnvironmentalTechnology
|
||||
this.Background = brush;
|
||||
}
|
||||
|
||||
|
||||
private void DeleteButton_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
if (sender is FrameworkElement element && element.Tag is int id)
|
||||
{
|
||||
var result = MessageBox.Show($"确定要删除ID为 {id} 的记录吗?", "确认删除",
|
||||
MessageBoxButton.YesNo, MessageBoxImage.Warning);
|
||||
MessageBoxResult result;
|
||||
if (_lang == "en-US")
|
||||
result = MessageBox.Show($"Delete record ID: {id} ?", "Confirm Delete", MessageBoxButton.YesNo, MessageBoxImage.Warning);
|
||||
else
|
||||
result = MessageBox.Show($"确定要删除ID为 {id} 的记录吗?", "确认删除", MessageBoxButton.YesNo, MessageBoxImage.Warning);
|
||||
|
||||
if (result == MessageBoxResult.Yes)
|
||||
{
|
||||
DeleteRecordFromDb(id);
|
||||
// 刷新当前页数据
|
||||
LoadData(_currentStartDate, _currentEndDate);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 新增:批量删除功能
|
||||
|
||||
private void BatchDeleteButton_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
var selectedIds = _records.Where(r => r.IsSelected).Select(r => r.Id).ToList();
|
||||
if (!selectedIds.Any())
|
||||
{
|
||||
MessageBox.Show("请先选择要删除的记录", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
|
||||
if (_lang == "en-US")
|
||||
MessageBox.Show("Please select records to delete", "Tip", MessageBoxButton.OK, MessageBoxImage.Information);
|
||||
else
|
||||
MessageBox.Show("请先选择要删除的记录", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
|
||||
return;
|
||||
}
|
||||
|
||||
var result = MessageBox.Show($"确定要删除选中的 {selectedIds.Count} 条记录吗?", "确认批量删除",
|
||||
MessageBoxButton.YesNo, MessageBoxImage.Warning);
|
||||
MessageBoxResult result;
|
||||
if (_lang == "en-US")
|
||||
result = MessageBox.Show($"Delete {selectedIds.Count} selected records?", "Confirm Batch Delete", MessageBoxButton.YesNo, MessageBoxImage.Warning);
|
||||
else
|
||||
result = MessageBox.Show($"确定要删除选中的 {selectedIds.Count} 条记录吗?", "确认批量删除", MessageBoxButton.YesNo, MessageBoxImage.Warning);
|
||||
|
||||
if (result == MessageBoxResult.Yes)
|
||||
{
|
||||
BatchDeleteFromDb(selectedIds);
|
||||
// 刷新当前页数据
|
||||
LoadData(_currentStartDate, _currentEndDate);
|
||||
}
|
||||
}
|
||||
@@ -221,7 +238,6 @@ namespace ShanghaiEnvironmentalTechnology
|
||||
|
||||
private void Export_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
|
||||
DateTime? startDate = null;
|
||||
if (DateTime.TryParse(StartDatePicker.SelectedDate?.ToString(), out DateTime tempStartDate))
|
||||
{
|
||||
@@ -229,30 +245,35 @@ namespace ShanghaiEnvironmentalTechnology
|
||||
}
|
||||
|
||||
DateTime? endDate = EndDatePicker.SelectedDate;
|
||||
|
||||
if (endDate.HasValue)
|
||||
{
|
||||
endDate = endDate.Value.Date.AddDays(1).AddTicks(-1);
|
||||
}
|
||||
|
||||
// 步骤2:读取CO2表数据
|
||||
List<CO2Record> co2Records = ReadCO2RecordsFromDatabase(startDate, endDate);
|
||||
|
||||
if (co2Records == null || !co2Records.Any())
|
||||
List<CO2Record> records = ReadCO2RecordsFromDatabase(startDate, endDate);
|
||||
if (records == null || !records.Any())
|
||||
{
|
||||
MessageBox.Show("CO2表中无数据,无法导出", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
|
||||
if (_lang == "en-US")
|
||||
MessageBox.Show("No CO2 data to export", "Tip", MessageBoxButton.OK, MessageBoxImage.Warning);
|
||||
else
|
||||
MessageBox.Show("CO2表中无数据,无法导出", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
|
||||
return;
|
||||
}
|
||||
|
||||
// 步骤3:导出Excel
|
||||
bool exportSuccess = ExportCO2RecordsToExcel(co2Records);
|
||||
if (exportSuccess)
|
||||
bool success = ExportCO2RecordsToExcel(records);
|
||||
if (success)
|
||||
{
|
||||
MessageBox.Show("数据已成功导出到Excel", "成功", MessageBoxButton.OK, MessageBoxImage.Information);
|
||||
if (_lang == "en-US")
|
||||
MessageBox.Show("Export successful!", "Success", MessageBoxButton.OK, MessageBoxImage.Information);
|
||||
else
|
||||
MessageBox.Show("数据已成功导出到Excel", "成功", MessageBoxButton.OK, MessageBoxImage.Information);
|
||||
}
|
||||
else
|
||||
{
|
||||
MessageBox.Show("Excel导出失败,请检查文件是否被占用", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
|
||||
if (_lang == "en-US")
|
||||
MessageBox.Show("Export failed! File may be in use.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
|
||||
else
|
||||
MessageBox.Show("Excel导出失败,请检查文件是否被占用", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -267,32 +288,16 @@ namespace ShanghaiEnvironmentalTechnology
|
||||
using (SQLiteConnection conn = new SQLiteConnection(CSConstant.DbConnectionString))
|
||||
{
|
||||
conn.Open();
|
||||
// 构建查询语句,根据开始时间和结束时间进行筛选
|
||||
string query = "SELECT BeginCO2, EndCO2, CO2Added, RecordTime FROM CO2 WHERE 1=1";
|
||||
|
||||
if (startDate != null)
|
||||
{
|
||||
query += " AND RecordTime >= @StartDate";
|
||||
}
|
||||
|
||||
if (endDate != null)
|
||||
{
|
||||
query += " AND RecordTime <= @EndDate";
|
||||
}
|
||||
|
||||
if (startDate != null) query += " AND RecordTime >= @StartDate";
|
||||
if (endDate != null) query += " AND RecordTime <= @EndDate";
|
||||
query += " ORDER BY RecordTime";
|
||||
|
||||
using (SQLiteCommand cmd = new SQLiteCommand(query, conn))
|
||||
{
|
||||
if (startDate != null)
|
||||
{
|
||||
cmd.Parameters.AddWithValue("@StartDate", startDate);
|
||||
}
|
||||
|
||||
if (endDate != null)
|
||||
{
|
||||
cmd.Parameters.AddWithValue("@EndDate", endDate);
|
||||
}
|
||||
if (startDate != null) cmd.Parameters.AddWithValue("@StartDate", startDate);
|
||||
if (endDate != null) cmd.Parameters.AddWithValue("@EndDate", endDate);
|
||||
|
||||
using (SQLiteDataReader reader = cmd.ExecuteReader())
|
||||
{
|
||||
@@ -300,10 +305,10 @@ namespace ShanghaiEnvironmentalTechnology
|
||||
{
|
||||
records.Add(new CO2Record
|
||||
{
|
||||
BeginCO2 = reader.GetDouble(0), // 开始CO2浓度(%)
|
||||
EndCO2 = reader.GetDouble(1), // 终CO2浓度(%)
|
||||
CO2Added = reader.GetDouble(2), // CO2浓度相对增加(%)
|
||||
RecordTime = reader.GetDateTime(3) // 时间
|
||||
BeginCO2 = reader.GetDouble(0),
|
||||
EndCO2 = reader.GetDouble(1),
|
||||
CO2Added = reader.GetDouble(2),
|
||||
RecordTime = reader.GetDateTime(3)
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -313,7 +318,10 @@ namespace ShanghaiEnvironmentalTechnology
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show($"读取CO2表失败:{ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
|
||||
if (_lang == "en-US")
|
||||
MessageBox.Show($"Failed to read CO2 data: {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
|
||||
else
|
||||
MessageBox.Show($"读取CO2表失败:{ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -336,7 +344,7 @@ namespace ShanghaiEnvironmentalTechnology
|
||||
|
||||
// 用户取消选择则返回
|
||||
bool? result = saveDialog.ShowDialog(); if (!(result ?? false)) return false;
|
||||
|
||||
|
||||
|
||||
// EPPlus 许可设置(.NET 8 必须显式设置)
|
||||
ExcelPackage.LicenseContext = LicenseContext.NonCommercial; // 非商业用途
|
||||
|
||||
@@ -3,6 +3,7 @@ using OfficeOpenXml;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Configuration;
|
||||
using System.Data.SQLite;
|
||||
using System.IO;
|
||||
using System.Windows;
|
||||
@@ -19,6 +20,8 @@ namespace ShanghaiEnvironmentalTechnology
|
||||
}
|
||||
public partial class ReportWindow6 : Window
|
||||
{
|
||||
private readonly string _lang = ConfigurationManager.AppSettings["Language"] ?? "zh-CN";
|
||||
|
||||
private const int PageSize = 10;
|
||||
private int currentPage = 1;
|
||||
private int totalRecords = 0;
|
||||
@@ -118,7 +121,10 @@ namespace ShanghaiEnvironmentalTechnology
|
||||
|
||||
private void UpdatePageInfo()
|
||||
{
|
||||
PageInfo.Text = $"第 {currentPage} 页 / 共 {Math.Ceiling((double)totalRecords / PageSize)} 页 {totalRecords}条记录";
|
||||
if (_lang == "en-US")
|
||||
PageInfo.Text = $"Page {currentPage} / {Math.Ceiling((double)totalRecords / PageSize)} | Total: {totalRecords} records";
|
||||
else
|
||||
PageInfo.Text = $"第 {currentPage} 页 / 共 {Math.Ceiling((double)totalRecords / PageSize)} 页 {totalRecords}条记录";
|
||||
}
|
||||
|
||||
private void PreviousPage_Click(object sender, RoutedEventArgs e)
|
||||
@@ -151,33 +157,43 @@ namespace ShanghaiEnvironmentalTechnology
|
||||
{
|
||||
if (sender is FrameworkElement element && element.Tag is int id)
|
||||
{
|
||||
var result = MessageBox.Show($"确定要删除ID为 {id} 的记录吗?", "确认删除",
|
||||
MessageBoxButton.YesNo, MessageBoxImage.Warning);
|
||||
MessageBoxResult result;
|
||||
if (_lang == "en-US")
|
||||
result = MessageBox.Show($"Delete record ID: {id} ?", "Confirm Delete", MessageBoxButton.YesNo, MessageBoxImage.Warning);
|
||||
else
|
||||
result = MessageBox.Show($"确定要删除ID为 {id} 的记录吗?", "确认删除", MessageBoxButton.YesNo, MessageBoxImage.Warning);
|
||||
|
||||
if (result == MessageBoxResult.Yes)
|
||||
{
|
||||
DeleteRecordFromDb(id);
|
||||
// 刷新当前页数据
|
||||
LoadData(_currentStartDate, _currentEndDate);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 新增:批量删除功能
|
||||
private void BatchDeleteButton_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
var selectedIds = _records.Where(r => r.IsSelected).Select(r => r.Id).ToList();
|
||||
if (!selectedIds.Any())
|
||||
{
|
||||
MessageBox.Show("请先选择要删除的记录", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
|
||||
if (_lang == "en-US")
|
||||
MessageBox.Show("Please select records to delete", "Tip", MessageBoxButton.OK, MessageBoxImage.Information);
|
||||
else
|
||||
MessageBox.Show("请先选择要删除的记录", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
|
||||
return;
|
||||
}
|
||||
|
||||
var result = MessageBox.Show($"确定要删除选中的 {selectedIds.Count} 条记录吗?", "确认批量删除",
|
||||
MessageBoxButton.YesNo, MessageBoxImage.Warning);
|
||||
MessageBoxResult result;
|
||||
if (_lang == "en-US")
|
||||
result = MessageBox.Show($"Delete {selectedIds.Count} selected records?", "Confirm Batch Delete", MessageBoxButton.YesNo, MessageBoxImage.Warning);
|
||||
else
|
||||
result = MessageBox.Show($"确定要删除选中的 {selectedIds.Count} 条记录吗?", "确认批量删除", MessageBoxButton.YesNo, MessageBoxImage.Warning);
|
||||
|
||||
if (result == MessageBoxResult.Yes)
|
||||
{
|
||||
BatchDeleteFromDb(selectedIds);
|
||||
// 刷新当前页数据
|
||||
LoadData(_currentStartDate, _currentEndDate);
|
||||
}
|
||||
}
|
||||
@@ -218,9 +234,9 @@ namespace ShanghaiEnvironmentalTechnology
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void Export_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
|
||||
DateTime? startDate = null;
|
||||
if (DateTime.TryParse(StartDatePicker.SelectedDate?.ToString(), out DateTime tempStartDate))
|
||||
{
|
||||
@@ -228,29 +244,35 @@ namespace ShanghaiEnvironmentalTechnology
|
||||
}
|
||||
|
||||
DateTime? endDate = EndDatePicker.SelectedDate;
|
||||
|
||||
if (endDate.HasValue)
|
||||
{
|
||||
endDate = endDate.Value.Date.AddDays(1).AddTicks(-1);
|
||||
}
|
||||
|
||||
// 步骤2:读取CO2表数据
|
||||
List<CO2Record> co2Records = ReadCO2RecordsFromDatabase(startDate, endDate);
|
||||
if (co2Records == null || !co2Records.Any())
|
||||
List<CO2Record> records = ReadCO2RecordsFromDatabase(startDate, endDate);
|
||||
if (records == null || !records.Any())
|
||||
{
|
||||
MessageBox.Show("CO2表中无数据,无法导出", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
|
||||
if (_lang == "en-US")
|
||||
MessageBox.Show("No CO2 data to export", "Tip", MessageBoxButton.OK, MessageBoxImage.Warning);
|
||||
else
|
||||
MessageBox.Show("CO2表中无数据,无法导出", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
|
||||
return;
|
||||
}
|
||||
|
||||
// 步骤3:导出Excel
|
||||
bool exportSuccess = ExportCO2RecordsToExcel(co2Records);
|
||||
if (exportSuccess)
|
||||
bool success = ExportCO2RecordsToExcel(records);
|
||||
if (success)
|
||||
{
|
||||
MessageBox.Show("数据已成功导出到Excel", "成功", MessageBoxButton.OK, MessageBoxImage.Information);
|
||||
if (_lang == "en-US")
|
||||
MessageBox.Show("Export successful!", "Success", MessageBoxButton.OK, MessageBoxImage.Information);
|
||||
else
|
||||
MessageBox.Show("数据已成功导出到Excel", "成功", MessageBoxButton.OK, MessageBoxImage.Information);
|
||||
}
|
||||
else
|
||||
{
|
||||
MessageBox.Show("Excel导出失败,请检查文件是否被占用", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
|
||||
if (_lang == "en-US")
|
||||
MessageBox.Show("Export failed! File may be in use.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
|
||||
else
|
||||
MessageBox.Show("Excel导出失败,请检查文件是否被占用", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -323,69 +345,66 @@ namespace ShanghaiEnvironmentalTechnology
|
||||
{
|
||||
try
|
||||
{
|
||||
// WPF 原生保存对话框(替代 WinForms)
|
||||
|
||||
SaveFileDialog saveDialog = new SaveFileDialog
|
||||
{
|
||||
Filter = "Excel文件 (*.xlsx)|*.xlsx",
|
||||
FileName = $"CO2过程记录导出_{DateTime.Now:yyyyMMddHHmmss}.xlsx",
|
||||
Title = "保存CO2记录"
|
||||
Filter = _lang == "en-US" ? "Excel File (*.xlsx)|*.xlsx" : "Excel文件 (*.xlsx)|*.xlsx",
|
||||
FileName = $"CO2_Process_Records_{DateTime.Now:yyyyMMddHHmmss}.xlsx",
|
||||
Title = _lang == "en-US" ? "Save CO2 Process Records" : "保存CO2过程记录"
|
||||
};
|
||||
|
||||
// 用户取消选择则返回
|
||||
bool? result = saveDialog.ShowDialog(); if (!(result ?? false)) return false;
|
||||
|
||||
bool? result = saveDialog.ShowDialog();
|
||||
if (!(result ?? false)) return false;
|
||||
|
||||
// EPPlus 许可设置(.NET 8 必须显式设置)
|
||||
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
|
||||
|
||||
// 创建并写入Excel
|
||||
using (ExcelPackage package = new ExcelPackage(new FileInfo(saveDialog.FileName)))
|
||||
{
|
||||
ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("CO2记录");
|
||||
ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("CO2 Process");
|
||||
|
||||
// 表头(对应DataGrid列)
|
||||
worksheet.Cells[1, 1].Value = "二氧化碳浓度(%)";
|
||||
worksheet.Cells[1, 2].Value = "压力(pa)";
|
||||
|
||||
worksheet.Cells[1, 3].Value = "时间";
|
||||
|
||||
// 表头样式(加粗、居中)
|
||||
using (var headerRange = worksheet.Cells[1, 1, 1, 6])
|
||||
// 双语表头
|
||||
if (_lang == "en-US")
|
||||
{
|
||||
headerRange.Style.Font.Bold = true;
|
||||
headerRange.Style.HorizontalAlignment = OfficeOpenXml.Style.ExcelHorizontalAlignment.Center;
|
||||
worksheet.Cells[1, 1].Value = "CO2 Concentration (%)";
|
||||
worksheet.Cells[1, 2].Value = "Pressure (Pa)";
|
||||
worksheet.Cells[1, 3].Value = "Time";
|
||||
}
|
||||
else
|
||||
{
|
||||
worksheet.Cells[1, 1].Value = "二氧化碳浓度(%)";
|
||||
worksheet.Cells[1, 2].Value = "压力(pa)";
|
||||
worksheet.Cells[1, 3].Value = "时间";
|
||||
}
|
||||
|
||||
using (var header = worksheet.Cells[1, 1, 1, 3])
|
||||
{
|
||||
header.Style.Font.Bold = true;
|
||||
header.Style.HorizontalAlignment = OfficeOpenXml.Style.ExcelHorizontalAlignment.Center;
|
||||
}
|
||||
|
||||
// 填充数据(从第2行开始)
|
||||
for (int i = 0; i < records.Count; i++)
|
||||
{
|
||||
int row = i + 2;
|
||||
var record = records[i];
|
||||
worksheet.Cells[row, 1].Value = record.Flow;
|
||||
worksheet.Cells[row, 2].Value = record.Pressure;
|
||||
//worksheet.Cells[row, 1].Value = record.BeginCO2;
|
||||
//worksheet.Cells[row, 2].Value = record.EndCO2;
|
||||
//worksheet.Cells[row, 3].Value = record.CO2Added;
|
||||
worksheet.Cells[row, 3].Value = record.RecordTime.ToString("yyyy-MM-dd HH:mm:ss");
|
||||
worksheet.Cells[row, 1].Value = records[i].Flow;
|
||||
worksheet.Cells[row, 2].Value = records[i].Pressure;
|
||||
worksheet.Cells[row, 3].Value = records[i].RecordTime.ToString("yyyy-MM-dd HH:mm:ss");
|
||||
}
|
||||
|
||||
// 自动调整列宽
|
||||
worksheet.Cells.AutoFitColumns();
|
||||
|
||||
// 保存文件
|
||||
package.Save();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show($"导出失败:{ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
|
||||
if (_lang == "en-US")
|
||||
MessageBox.Show($"Export failed: {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
|
||||
else
|
||||
MessageBox.Show($"导出失败:{ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void HomePage_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
this.Close();
|
||||
|
||||
Reference in New Issue
Block a user