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;
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user