using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; 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.Navigation; using System.Windows.Shapes; using Aitex.Triton160.Common; using Aitex.Triton160.UI.ViewModel; using DataAnalysisControl.Core; using Aitex.Core.Util; using ExcelLibrary.SpreadSheet; using Triton160.UI.ViewModel; namespace Aitex.Triton160.UI.Views { /// /// Interaction logic for DataLogView.xaml /// public partial class DataLogView : UserControl { private DataLogViewModel _viewModel; private DateTime BeginTime { get { var BeginDate = dateBegin.SelectedDate.HasValue ? dateBegin.SelectedDate.Value : DateTime.Now; return new DateTime(BeginDate.Year, BeginDate.Month, BeginDate.Day, timeBegin.Value.Value.Hour, timeBegin.Value.Value.Minute, timeBegin.Value.Value.Second); } } private DateTime EndTime { get { var EndDate = dateEnd.SelectedDate.HasValue ? dateEnd.SelectedDate.Value : DateTime.Now; return new DateTime(EndDate.Year, EndDate.Month, EndDate.Day, timeEnd.Value.Value.Hour, timeEnd.Value.Value.Minute, timeEnd.Value.Value.Second, 999); } } public DataLogView() { InitializeComponent(); _viewModel = new DataLogViewModel(); DataContext = _viewModel; var now = DateTime.Now; dateBegin.DisplayDate = now - new TimeSpan(1, 0, 0, 0); dateEnd.DisplayDate = now; dateBegin.SelectedDate = now; dateEnd.SelectedDate = now; timeBegin.Value = new DateTime(now.Year, now.Month, now.Day, 0, 0, 0, 0); timeEnd.Value = new DateTime(now.Year, now.Month, now.Day, 23, 59, 59, 999); dataGrid_LotList.CanUserAddRows = false; _viewModel.UpdateRecipeList(BeginTime, EndTime); IsVisibleChanged += DataLogView_IsVisibleChanged; } void DataLogView_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e) { if (!IsLoaded) return; if (IsVisible) _viewModel.UpdateRecipeList(BeginTime, EndTime); } private void buttonQuery_Click(object sender, RoutedEventArgs e) { _viewModel.UpdateDataLogList(BeginTime, EndTime, recipeName.Text,LotTextBox.BarcodeText.Trim()); List DataLogList = _viewModel.DataLogList; ButtonExportList.IsEnabled = DataLogList != null && DataLogList.Count > 0; } private void dateBegin_SelectedDateChanged(object sender, SelectionChangedEventArgs e) { if (!IsLoaded) return; _viewModel.UpdateRecipeList(BeginTime, EndTime); } private void timeBegin_ValueChanged(object sender, RoutedPropertyChangedEventArgs e) { if (!IsLoaded) return; _viewModel.UpdateRecipeList(BeginTime, EndTime); } private void dateEnd_SelectedDateChanged(object sender, SelectionChangedEventArgs e) { if (!IsLoaded) return; _viewModel.UpdateRecipeList(BeginTime, EndTime); } private void timeEnd_ValueChanged(object sender, RoutedPropertyChangedEventArgs e) { if (!IsLoaded) return; _viewModel.UpdateRecipeList(BeginTime, EndTime); } private void dataGrid_DataLogList_SelectionChanged(object sender, SelectionChangedEventArgs e) { DataLogItem log = (dataGrid_LotList.SelectedItem) as DataLogItem; _viewModel.UpdateData(log ); ButtonExportData.IsEnabled = log != null ; } private void buttonLotListExport_Click(object sender, RoutedEventArgs e) { List DataLogList = _viewModel.DataLogList; if (DataLogList == null ||DataLogList.Count==0) { return; } Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog(); dlg.DefaultExt = ".xls"; // Default file extension dlg.Filter = "数据表格文件|*.xls"; // Filter files by extension dlg.FileName = string.Format("LotList{0}", BeginTime.ToString("yyyyMMdd")); Nullable result = dlg.ShowDialog();// Show open file dialog box if (result != true) // Process open file dialog box results return; if (File.Exists(dlg.FileName)) { File.Delete(dlg.FileName); } Workbook workbook = new Workbook(); Worksheet worksheet = new Worksheet(BeginTime.ToString("yyyyMMdd")); int col = 0; worksheet.Cells[0, col++] = new Cell(Aitex.Triton160.UI.Properties.Resources.LotName); worksheet.Cells[0, col++] = new Cell(Aitex.Triton160.UI.Properties.Resources.RecipeName); worksheet.Cells[0, col++] = new Cell(Aitex.Triton160.UI.Properties.Resources.ProcessBeginTime); worksheet.Cells[0, col++] = new Cell(Aitex.Triton160.UI.Properties.Resources.ProcessEndTime); worksheet.Cells[0, col++] = new Cell(Aitex.Triton160.UI.Properties.Resources.RecipeResult); for (int i=0; i result = dlg.ShowDialog();// Show open file dialog box if (result != true) // Process open file dialog box results return; if (File.Exists(dlg.FileName)) { File.Delete(dlg.FileName); } Workbook workbook = new Workbook(); Worksheet worksheet = new Worksheet(log.DataLogName); Dictionary colIndex = new Dictionary(); Dictionary rowIndex = new Dictionary(); int colCount = 0; int rowCount = 0; foreach (var item in _viewModel.ProcessData) { if (!rowIndex.ContainsKey(item.dateTime)) { rowCount++; rowIndex[item.dateTime] = rowCount; worksheet.Cells[rowCount, 0] = new Cell(item.dateTime); } if (!colIndex.ContainsKey(item.dbName)) { colCount++; colIndex[item.dbName] = colCount; worksheet.Cells[0, colCount] = new Cell(item.dbName); } worksheet.Cells[rowIndex[item.dateTime], colIndex[item.dbName]] = new Cell(item.value); } workbook.Worksheets.Add(worksheet); workbook.Save(dlg.FileName); } } }