AppendFromExcel.xaml.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. using MeterVision.db;
  2. using MeterVision.Dlg;
  3. using OfficeOpenXml;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows;
  11. using System.Windows.Controls;
  12. using System.Windows.Data;
  13. using System.Windows.Documents;
  14. using Microsoft.Win32;
  15. using System.Windows.Input;
  16. using System.Windows.Media;
  17. using System.Windows.Media.Imaging;
  18. using System.Windows.Shapes;
  19. namespace MeterVision.Stand
  20. {
  21. /// <summary>
  22. /// AppendFromExcel.xaml 的交互逻辑
  23. /// </summary>
  24. public partial class AppendFromExcel : Window
  25. {
  26. //新增的TStand
  27. //public TStand mTStand { get; set; }
  28. public List<TStandDetail> mStandDetailList;
  29. public AppendFromExcel()
  30. {
  31. InitializeComponent();
  32. }
  33. private void BtnImportExcel_Click(object sender, RoutedEventArgs e)
  34. {
  35. // 打开文件选择对话框
  36. var openFileDialog = new OpenFileDialog
  37. {
  38. //Filter = "Excel Files (*.xls;*.xlsx)|*.xls;*.xlsx",
  39. Filter = "Excel Files (*.xlsx)|*.xlsx",
  40. Title = "选择Excel文件"
  41. };
  42. if (openFileDialog.ShowDialog() == true)
  43. {
  44. // 显示选择的文件名
  45. txtExcelFile.Text = openFileDialog.FileName;
  46. /*if (string.IsNullOrWhiteSpace(txtStandName.Text.Trim()))
  47. {
  48. txtStandName.Text = ThisApp.GetFileNameWithoutExtension(txtExcelFile.Text.Trim());
  49. }*/
  50. }
  51. }
  52. private async void BtnOK_Click(object sender, RoutedEventArgs e)
  53. {
  54. string filePath = txtExcelFile.Text.Trim();
  55. if (string.IsNullOrWhiteSpace(filePath))
  56. {
  57. MessageBox.Show(Application.Current.MainWindow, "请选择Excel文件!", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
  58. return;
  59. }
  60. bool blLoad = await LoadTStandDetailList(filePath);
  61. this.DialogResult = true;
  62. this.Close();
  63. }
  64. //加载列表数据
  65. private async Task<bool> LoadTStandDetailList(string fileName)
  66. {
  67. string titleInfo = "正在获取数据,请稍候...";
  68. WaitImportDlg waitWindow = new WaitImportDlg(titleInfo)
  69. {
  70. Owner = this,
  71. WindowStartupLocation = WindowStartupLocation.CenterOwner
  72. };
  73. waitWindow.Show();
  74. try
  75. {
  76. await Task.Run(() =>
  77. {
  78. //ImportExcel(mTStand.StandFile, waitWindow);
  79. ImportExcel(fileName, waitWindow);
  80. });
  81. // 如果mStandDetailList不为空,则表示导入成功
  82. return mStandDetailList != null && mStandDetailList.Count > 0;
  83. }
  84. catch (Exception ex)
  85. {
  86. MessageBox.Show(this, $"获取Excel数据失败:{ex.Message}", "错误",
  87. MessageBoxButton.OK, MessageBoxImage.Error);
  88. return false;
  89. }
  90. finally
  91. {
  92. waitWindow.Close();
  93. }
  94. }
  95. public void ImportExcel(string filePath, WaitImportDlg importDlg)
  96. {
  97. mStandDetailList = new List<TStandDetail>();
  98. Console.WriteLine($"正在读取文件: {filePath}");
  99. using (var package = new ExcelPackage(new FileInfo(filePath)))
  100. {
  101. Console.WriteLine($"工作表数量: {package.Workbook.Worksheets.Count}");
  102. //检查是否存在工作表
  103. if (package.Workbook.Worksheets.Count == 0)
  104. {
  105. throw new InvalidOperationException("Excel文件中没有工作表。");
  106. }
  107. // 获取第一个工作表
  108. var worksheet = package.Workbook.Worksheets[1];
  109. // 获取总行数
  110. int rowCount = worksheet.Dimension?.Rows ?? 0;
  111. Console.WriteLine($"行数: {rowCount}");
  112. importDlg.PrgCount = rowCount;
  113. // 遍历每一行
  114. for (int row = 2; row <= rowCount; row++) // 假设第一行是标题行
  115. {
  116. importDlg.PrgIndex = row;
  117. //判断图像是否符合要求的规格
  118. string srcImage = worksheet.Cells[row, 1].Text.Trim();
  119. //为了导入excel表格中的内容,先暂时不判断文件是否满足要求
  120. /*if (!ThisApp.IsImageDimensionsValid(srcImage))
  121. {
  122. continue;
  123. }*/
  124. var standDetail = new TStandDetail
  125. {
  126. StandDetailId = Guid.NewGuid().ToString(),
  127. CreateTime = ThisApp.GetNowTime_yyyyMMddHHmmss(),
  128. //StandId = mTStand.StandId,
  129. // 读取图像路径(假设图像路径在A列)
  130. SrcImage = worksheet.Cells[row, 1].Text.Trim(),
  131. // 读取标准答案(假设标准答案在B列)
  132. StandValue = worksheet.Cells[row, 2].Text.Trim(),
  133. DeviceSn = worksheet.Cells[row, 3].Text.Trim(),
  134. StationId = worksheet.Cells[row, 4].Text.Trim(),
  135. SampleTime = worksheet.Cells[row, 5].Text.Trim(),
  136. ENumCount = ThisApp.ConvertToInt(worksheet.Cells[row, 6].Text.Trim()),
  137. ELastUnit = ThisApp.ConvertToInt(worksheet.Cells[row, 7].Text.Trim())
  138. };
  139. // 添加到结果列表
  140. mStandDetailList.Add(standDetail);
  141. }
  142. }
  143. }
  144. private void BtnClose_Click(object sender, RoutedEventArgs e)
  145. {
  146. this.Close();
  147. }
  148. //---------------------------------------------------------------------------
  149. }
  150. }