301 lines
10 KiB
C#
301 lines
10 KiB
C#
using jiancaiburanxing.data;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Collections.ObjectModel;
|
||
using System.ComponentModel;
|
||
using System.Diagnostics;
|
||
using System.IO;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
using System.Windows;
|
||
using System.Windows.Controls;
|
||
using System.Windows.Data;
|
||
using System.Windows.Documents;
|
||
using System.Windows.Input;
|
||
using System.Windows.Media;
|
||
using System.Windows.Media.Imaging;
|
||
using System.Windows.Shapes;
|
||
using 建材不燃性试验炉;
|
||
using Path = System.IO.Path;
|
||
|
||
namespace jiancaiburanxing
|
||
{
|
||
/// <summary>
|
||
/// TestReportWindow.xaml 的交互逻辑
|
||
/// </summary>
|
||
public partial class TestReportWindow : Window
|
||
{
|
||
|
||
private ObservableCollection<TestReportData> _testData;
|
||
private ICollectionView _testDataView;
|
||
#region 构造函数和初始化
|
||
public TestReportWindow()
|
||
{
|
||
InitializeComponent();
|
||
InitializeDataGrid();
|
||
LoadTestData();
|
||
}
|
||
#endregion
|
||
|
||
|
||
private void InitializeDataGrid()
|
||
{
|
||
_testData = new ObservableCollection<TestReportData>();
|
||
_testDataView = CollectionViewSource.GetDefaultView(_testData);
|
||
dgTestReport.ItemsSource = _testDataView;
|
||
|
||
// 设置按钮事件
|
||
btnClearData.Click += BtnClearData_Click;
|
||
btnExportReport.Click += BtnExportReport_Click;
|
||
btnReturn.Click += btnReturn_Click;
|
||
}
|
||
|
||
// 加载试验数据
|
||
private void LoadTestData()
|
||
{
|
||
try
|
||
{
|
||
LoadTempDataFromFile();
|
||
//AddSampleData();
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
MessageBox.Show($"加载数据失败: {ex.Message}", "错误",
|
||
MessageBoxButton.OK, MessageBoxImage.Error);
|
||
}
|
||
}
|
||
// 可选:从临时文件加载数据
|
||
private void LoadTempDataFromFile()
|
||
{
|
||
try
|
||
{
|
||
string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TempReportData.txt");
|
||
if (!File.Exists(filePath)) return;
|
||
|
||
var lines = File.ReadAllLines(filePath, Encoding.UTF8);
|
||
foreach (var line in lines)
|
||
{
|
||
var parts = line.Split('|');
|
||
if (parts.Length >= 14)
|
||
{
|
||
var reportData = new TestReportData
|
||
{
|
||
SampleCode = parts[0],
|
||
SampleName = parts[1],
|
||
SampleSpec = parts[2],
|
||
InitialTemp = double.Parse(parts[3]),
|
||
MaxTemp = double.Parse(parts[4]),
|
||
FinalTemp = double.Parse(parts[5]),
|
||
TempRise = double.Parse(parts[6]),
|
||
InitialWeight = double.Parse(parts[7]),
|
||
FinalWeight = double.Parse(parts[8]),
|
||
LossPercent = double.Parse(parts[9]),
|
||
FlameDuration = int.Parse(parts[10]),
|
||
TestDate = parts[11],
|
||
TestDuration = parts[12],
|
||
BalanceStatus = parts[13],
|
||
Remarks = parts.Length > 14 ? parts[14] : ""
|
||
};
|
||
|
||
_testData.Add(reportData);
|
||
}
|
||
}
|
||
|
||
Debug.WriteLine($"从临时文件加载了 {_testData.Count} 条数据");
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Debug.WriteLine($"加载临时数据失败: {ex.Message}");
|
||
}
|
||
}
|
||
//// 添加示例数据
|
||
//private void AddSampleData()
|
||
//{
|
||
// _testData.Add(new TestReportData
|
||
// {
|
||
// SampleCode = "TEST001",
|
||
// SampleName = "岩棉板",
|
||
// SampleSpec = "50mm",
|
||
// InitialTemp = 750.0,
|
||
// MaxTemp = 877.8,
|
||
// FinalTemp = 802.3,
|
||
// TempRise = 75.5,
|
||
// CenterMaxTemp = 850.0,
|
||
// CenterFinalTemp = 800.0,
|
||
// SurfaceMaxTemp = 900.0,
|
||
// SurfaceFinalTemp = 820.0,
|
||
// InitialWeight = 45.32,
|
||
// FinalWeight = 44.78,
|
||
// LossPercent = 1.19,
|
||
// FlameDuration = 0,
|
||
// TestDate = DateTime.Now.ToString("yyyy-MM-dd"),
|
||
// TestDuration = "30:00",
|
||
// BalanceStatus = "达到平衡",
|
||
// Remarks = "符合标准GB/T 5464-2010"
|
||
// });
|
||
//}
|
||
|
||
// 导出报表
|
||
private void BtnExportReport_Click(object sender, RoutedEventArgs e)
|
||
{
|
||
try
|
||
{
|
||
var saveDialog = new Microsoft.Win32.SaveFileDialog
|
||
{
|
||
Filter = "Excel文件 (*.xlsx)|*.xlsx|CSV文件 (*.csv)|*.csv|所有文件 (*.*)|*.*",
|
||
FileName = $"不燃性试验报告_{DateTime.Now:yyyyMMdd_HHmmss}",
|
||
DefaultExt = ".xlsx"
|
||
};
|
||
|
||
if (saveDialog.ShowDialog() == true)
|
||
{
|
||
ExportToExcel(saveDialog.FileName);
|
||
MessageBox.Show($"报表已导出到:\n{saveDialog.FileName}", "导出成功",
|
||
MessageBoxButton.OK, MessageBoxImage.Information);
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
MessageBox.Show($"导出失败: {ex.Message}", "错误",
|
||
MessageBoxButton.OK, MessageBoxImage.Error);
|
||
}
|
||
}
|
||
|
||
|
||
|
||
// 导出到Excel(简化版本)
|
||
private void ExportToExcel(string filePath)
|
||
{
|
||
using (var writer = new StreamWriter(filePath, false, Encoding.UTF8))
|
||
{
|
||
// 写入标题
|
||
writer.WriteLine("建材不燃性试验数据报表");
|
||
writer.WriteLine($"生成时间: {DateTime.Now:yyyy-MM-dd HH:mm:ss}");
|
||
writer.WriteLine("符合标准: GB/T 5464-2010");
|
||
writer.WriteLine();
|
||
|
||
// 写入表头
|
||
writer.WriteLine("试样编号,试样名称,试样规格,炉内初始温度(℃),炉内最高温度(℃),炉内最终温度(℃),温升(℃)," +
|
||
"试样中心最高温度(℃),试样中心最终温度(℃),试样表面最高温度(℃),试样表面最终温度(℃)," +
|
||
"试样初始质量(g),试样结束质量(g),质量损失(%),火焰持续时间(s),试验日期,试验持续时间,平衡状态,备注");
|
||
|
||
// 写入数据
|
||
foreach (var item in _testData)
|
||
{
|
||
writer.WriteLine($"{item.SampleCode},{item.SampleName},{item.SampleSpec}," +
|
||
$"{item.InitialTemp:F1},{item.MaxTemp:F1},{item.FinalTemp:F1},{item.TempRise:F1}," +
|
||
$"{item.CenterMaxTemp:F1},{item.CenterFinalTemp:F1},{item.SurfaceMaxTemp:F1},{item.SurfaceFinalTemp:F1}," +
|
||
$"{item.InitialWeight:F2},{item.FinalWeight:F2},{item.LossPercent:F2}," +
|
||
$"{item.FlameDuration},{item.TestDate},{item.TestDuration},{item.BalanceStatus},{item.Remarks}");
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
|
||
|
||
// 添加新的试验数据(由主窗口调用)
|
||
public void AddTestReportData(TestReportData data)
|
||
{
|
||
Dispatcher.Invoke(() =>
|
||
{
|
||
_testData.Insert(0, data); // 插入到开头
|
||
});
|
||
}
|
||
|
||
// 清空数据
|
||
private void BtnClearData_Click(object sender, RoutedEventArgs e)
|
||
{
|
||
var result = MessageBox.Show("确定要清空所有报表数据吗?此操作不可恢复。",
|
||
"确认清空", MessageBoxButton.YesNo, MessageBoxImage.Warning);
|
||
|
||
if (result == MessageBoxResult.Yes)
|
||
{
|
||
_testData.Clear();
|
||
DeleteJsonDataFile();
|
||
MessageBox.Show("数据已清空", "提示",
|
||
MessageBoxButton.OK, MessageBoxImage.Information);
|
||
}
|
||
}
|
||
// 删除JSON数据文件
|
||
private void DeleteJsonDataFile()
|
||
{
|
||
try
|
||
{
|
||
// 假设你的JSON文件路径(根据你的实际情况调整)
|
||
string jsonFilePath = Path.Combine(
|
||
AppDomain.CurrentDomain.BaseDirectory,
|
||
"TestReportData.json");
|
||
|
||
// 如果文件存在,就删除
|
||
if (File.Exists(jsonFilePath))
|
||
{
|
||
File.Delete(jsonFilePath);
|
||
Debug.WriteLine($"JSON数据文件已删除: {jsonFilePath}");
|
||
}
|
||
else
|
||
{
|
||
Debug.WriteLine($"JSON数据文件不存在: {jsonFilePath}");
|
||
}
|
||
|
||
|
||
string tempFilePath = Path.Combine(
|
||
AppDomain.CurrentDomain.BaseDirectory,
|
||
"TempReportData.txt");
|
||
|
||
if (File.Exists(tempFilePath))
|
||
{
|
||
File.Delete(tempFilePath);
|
||
Debug.WriteLine($"临时数据文件已删除: {tempFilePath}");
|
||
}
|
||
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
// 记录错误但不影响主流程
|
||
Debug.WriteLine($"删除JSON数据文件失败: {ex.Message}");
|
||
|
||
|
||
}
|
||
}
|
||
#region 切换主页相关
|
||
|
||
private MainWindow _mainWindow;
|
||
|
||
//返回
|
||
private void btnReturn_Click(object sender, RoutedEventArgs e)
|
||
{
|
||
SwitchWindow(ref _mainWindow, () => new MainWindow());
|
||
}
|
||
|
||
private void SwitchWindow<T>(ref T windowInstance, Func<T> createFunc) where T : Window, new()
|
||
{
|
||
|
||
|
||
if (windowInstance == null)
|
||
{
|
||
windowInstance = createFunc();
|
||
// 添加窗口关闭事件处理
|
||
windowInstance.Closed += (s, args) =>
|
||
{
|
||
this.Show();
|
||
this.Activate();
|
||
};
|
||
}
|
||
else
|
||
{
|
||
windowInstance.Activate();
|
||
return;
|
||
}
|
||
|
||
this.Hide();
|
||
windowInstance.Show();
|
||
}
|
||
|
||
#endregion
|
||
|
||
|
||
}
|
||
}
|