using AutoMapper; using GalaSoft.MvvmLight; using GalaSoft.MvvmLight.Command; using GroupLine.App.ModelDto; using GroupLine.Model; using NLog; using NPOI.HSSF.Util; using NPOI.SS.UserModel; using NPOI.XSSF.UserModel; using System; using System.Collections.ObjectModel; using System.IO; using System.Linq; using System.Windows; using System.Windows.Forms; using MessageBox = System.Windows.MessageBox; namespace GroupLine.App.ViewModel { public class KPBarrelShrinkFitViewModel : ViewModelBase { private static Logger _Logger = LogManager.GetCurrentClassLogger(); private string CurrentMachineName = "KP型筒体烧嵌"; private string CurrentMachineNameTemplate = "KPBarrelShrinkFitTemplate.xlsx"; private IMapper autoMapper; public KPBarrelShrinkFitViewModel() { ListModelDto = new ObservableCollection(); SearchStartDate = DateTime.Now.AddDays(-1).ToShortDateString(); SearchEndDate = DateTime.Now.AddDays(2).ToShortDateString(); MachineName = CurrentMachineName + " - 搜索条件"; var config = new MapperConfiguration(cfg => cfg.CreateMap()); autoMapper = config.CreateMapper(); } private ObservableCollection _ListModelDto; public ObservableCollection ListModelDto { get { return _ListModelDto; } set { _ListModelDto = value; } } private string machineName; public string MachineName { get { return machineName; } set { machineName = value; RaisePropertyChanged(() => MachineName); } } private string searchBarrelNo; public string SearchBarrelNo { get { return searchBarrelNo; } set { searchBarrelNo = value; RaisePropertyChanged(() => SearchBarrelNo); } } private string searchStartDate; public string SearchStartDate { get { return searchStartDate; } set { searchStartDate = value; RaisePropertyChanged(() => SearchStartDate); } } private string searchEndDate; public string SearchEndDate { get { return searchEndDate; } set { searchEndDate = value; RaisePropertyChanged(() => SearchEndDate); } } private RelayCommand searchCmd; public RelayCommand SearchCmd { get { if (searchCmd == null) return new RelayCommand(() => Search()); return searchCmd; } set { searchCmd = value; } } private void Search() { try { var mulConQueryable = FSqlContext.FDb.Select(); if (!string.IsNullOrEmpty(SearchBarrelNo)) { mulConQueryable = mulConQueryable.Where(t => t.BarrelNo.Contains(SearchBarrelNo)); } if (!string.IsNullOrEmpty(SearchStartDate)) { mulConQueryable = mulConQueryable.Where(t => t.CreateTime.Date >= Convert.ToDateTime(SearchStartDate)); } if (!string.IsNullOrEmpty(SearchEndDate)) { mulConQueryable = mulConQueryable.Where(t => t.CreateTime.Date < Convert.ToDateTime(SearchEndDate).AddDays(1)); } var listData = mulConQueryable.OrderByDescending(a => a.CreateTime).ToList(); ListModelDto.Clear(); foreach (var item in listData) { ListModelDto.Add(autoMapper.Map(item)); } } catch (Exception ex) { _Logger.Error(string.Format("ErrSource : {0} ErrMsg : {1}", ex.StackTrace, ex.Message)); } } private RelayCommand _OutputDataCmd; public RelayCommand OutputDataCmd { get { if (_OutputDataCmd == null) return new RelayCommand(() => OutputDataAction()); return _OutputDataCmd; } set { _OutputDataCmd = value; } } private void OutputDataAction() { try { if (ListModelDto != null && ListModelDto.Count > 0) { string filePath = string.Empty; FolderBrowserDialog dialog = new FolderBrowserDialog(); dialog.Description = "请选择文件夹"; dialog.ShowNewFolderButton = true; if (dialog.ShowDialog() == DialogResult.OK) { if (string.IsNullOrEmpty(dialog.SelectedPath)) { MessageBox.Show("文件夹路径不能为空", "温馨提示", MessageBoxButton.OK, MessageBoxImage.Information); return; } filePath = dialog.SelectedPath; } var listData = ListModelDto.ToList(); string templateFilePath = Environment.CurrentDirectory; string tPath = templateFilePath + @"\ReportFile\" + CurrentMachineNameTemplate; XSSFWorkbook wk; using (FileStream fs = File.Open(tPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { wk = new XSSFWorkbook(fs); fs.Close(); } ISheet sheet = wk.GetSheetAt(0); IDataFormat dataformat = wk.CreateDataFormat(); ICellStyle style0 = wk.CreateCellStyle(); ICellStyle styleBag = wk.CreateCellStyle(); styleBag.FillForegroundColor = HSSFColor.Red.Index; styleBag.FillPattern = FillPattern.SolidForeground; for (int i = 0; i < listData.Count; i++) { IRow row = sheet.CreateRow(i + 1); row.CreateCell(0).SetCellValue(listData[i].BarrelNo.ToString()); row.CreateCell(1).SetCellValue(listData[i].StatorNo.ToString()); row.CreateCell(2).SetCellValue(listData[i].OperatorNo.ToString()); row.CreateCell(3).SetCellValue(listData[i].BarrelTemp.ToString()); row.CreateCell(4).SetCellValue(listData[i].CreateTime.ToString("yyyy-MM-dd HH:mm")); style0.DataFormat = dataformat.GetFormat("yyyy-MM-dd HH:mm:ss"); row.GetCell(4).CellStyle = style0; } using (MemoryStream ms = new MemoryStream()) { if (!Directory.Exists(filePath)) { Directory.CreateDirectory(filePath); } filePath = filePath + @"\" + Convert.ToDateTime(SearchStartDate).ToString("yyyy-MM-dd") + "-" + Convert.ToDateTime(SearchEndDate).ToString("yyyy-MM-dd") + "-" + CurrentMachineName + ".xlsx"; wk.Write(ms); using (FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write)) { byte[] data = ms.ToArray(); fs.Write(data, 0, data.Length); fs.Flush(); } } MessageBox.Show("生成成功"); } else { MessageBox.Show("请先搜索数据后再导出", "提示", MessageBoxButton.OK, MessageBoxImage.Information); } } catch (Exception ex) { _Logger.Error(string.Format("ErrSource : {0} ErrMsg : {1}", ex.StackTrace, ex.Message)); } } } }