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.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>
|
||||
|
||||
Reference in New Issue
Block a user