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