123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- using MeterVision.db;
- using MeterVision.Dlg;
- using OfficeOpenXml;
- using System;
- using System.Collections.Generic;
- 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 Microsoft.Win32;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Shapes;
- namespace MeterVision.Stand
- {
- /// <summary>
- /// AppendFromExcel.xaml 的交互逻辑
- /// </summary>
- public partial class AppendFromExcel : Window
- {
- //新增的TStand
- //public TStand mTStand { get; set; }
- public List<TStandDetail> mStandDetailList;
- public AppendFromExcel()
- {
- InitializeComponent();
- }
- private void BtnImportExcel_Click(object sender, RoutedEventArgs e)
- {
- // 打开文件选择对话框
- var openFileDialog = new OpenFileDialog
- {
- //Filter = "Excel Files (*.xls;*.xlsx)|*.xls;*.xlsx",
- Filter = "Excel Files (*.xlsx)|*.xlsx",
- Title = "选择Excel文件"
- };
- if (openFileDialog.ShowDialog() == true)
- {
- // 显示选择的文件名
- txtExcelFile.Text = openFileDialog.FileName;
- /*if (string.IsNullOrWhiteSpace(txtStandName.Text.Trim()))
- {
- txtStandName.Text = ThisApp.GetFileNameWithoutExtension(txtExcelFile.Text.Trim());
- }*/
- }
- }
- private async void BtnOK_Click(object sender, RoutedEventArgs e)
- {
- string filePath = txtExcelFile.Text.Trim();
- if (string.IsNullOrWhiteSpace(filePath))
- {
- MessageBox.Show(Application.Current.MainWindow, "请选择Excel文件!", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
- return;
- }
- bool blLoad = await LoadTStandDetailList(filePath);
- this.DialogResult = true;
- this.Close();
- }
- //加载列表数据
- private async Task<bool> LoadTStandDetailList(string fileName)
- {
- string titleInfo = "正在获取数据,请稍候...";
- WaitImportDlg waitWindow = new WaitImportDlg(titleInfo)
- {
- Owner = this,
- WindowStartupLocation = WindowStartupLocation.CenterOwner
- };
- waitWindow.Show();
- try
- {
- await Task.Run(() =>
- {
- //ImportExcel(mTStand.StandFile, waitWindow);
- ImportExcel(fileName, waitWindow);
- });
- // 如果mStandDetailList不为空,则表示导入成功
- return mStandDetailList != null && mStandDetailList.Count > 0;
- }
- catch (Exception ex)
- {
- MessageBox.Show(this, $"获取Excel数据失败:{ex.Message}", "错误",
- MessageBoxButton.OK, MessageBoxImage.Error);
- return false;
- }
- finally
- {
- waitWindow.Close();
- }
- }
- public void ImportExcel(string filePath, WaitImportDlg importDlg)
- {
- mStandDetailList = new List<TStandDetail>();
- Console.WriteLine($"正在读取文件: {filePath}");
- using (var package = new ExcelPackage(new FileInfo(filePath)))
- {
- Console.WriteLine($"工作表数量: {package.Workbook.Worksheets.Count}");
- //检查是否存在工作表
- if (package.Workbook.Worksheets.Count == 0)
- {
- throw new InvalidOperationException("Excel文件中没有工作表。");
- }
- // 获取第一个工作表
- var worksheet = package.Workbook.Worksheets[1];
- // 获取总行数
- int rowCount = worksheet.Dimension?.Rows ?? 0;
- Console.WriteLine($"行数: {rowCount}");
- importDlg.PrgCount = rowCount;
- // 遍历每一行
- for (int row = 2; row <= rowCount; row++) // 假设第一行是标题行
- {
- importDlg.PrgIndex = row;
- //判断图像是否符合要求的规格
- string srcImage = worksheet.Cells[row, 1].Text.Trim();
- //为了导入excel表格中的内容,先暂时不判断文件是否满足要求
- /*if (!ThisApp.IsImageDimensionsValid(srcImage))
- {
- continue;
- }*/
- var standDetail = new TStandDetail
- {
- StandDetailId = Guid.NewGuid().ToString(),
- CreateTime = ThisApp.GetNowTime_yyyyMMddHHmmss(),
- //StandId = mTStand.StandId,
- // 读取图像路径(假设图像路径在A列)
- SrcImage = worksheet.Cells[row, 1].Text.Trim(),
- // 读取标准答案(假设标准答案在B列)
- StandValue = worksheet.Cells[row, 2].Text.Trim(),
- DeviceSn = worksheet.Cells[row, 3].Text.Trim(),
- StationId = worksheet.Cells[row, 4].Text.Trim(),
- SampleTime = worksheet.Cells[row, 5].Text.Trim(),
- ENumCount = ThisApp.ConvertToInt(worksheet.Cells[row, 6].Text.Trim()),
- ELastUnit = ThisApp.ConvertToInt(worksheet.Cells[row, 7].Text.Trim())
- };
- // 添加到结果列表
- mStandDetailList.Add(standDetail);
- }
- }
- }
- private void BtnClose_Click(object sender, RoutedEventArgs e)
- {
- this.Close();
- }
- //---------------------------------------------------------------------------
- }
- }
|