Files

164 lines
5.8 KiB
C#
Raw Permalink Normal View History

2026-04-30 11:34:41 +08:00
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Models.Models;
using SamplePre.ProcessBll.SampleSequence;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using OfficeOpenXml;
namespace SamplePreSystem.UI.ViewModel.QueryManager
{
public partial class SampleQueryViewModel : ObservableObject
{
public event PropertyChangedEventHandler PropertyChanged;
SampleSequenceBll sampleSequenceBll = new SampleSequenceBll();
public SampleQueryViewModel()
{
GetData();
}
private DateTime _startDate = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd") + " 00:00:00");
public DateTime StartDate
{
get { return _startDate; }
set {
SetProperty(ref _startDate, value);
}
}
private DateTime _endDate = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd") + " 00:00:00");
public DateTime EndDate
{
get { return _endDate; }
set
{
SetProperty(ref _endDate, value);
}
}
private List<tube_input> _sampleList = new List<tube_input>();
public List<tube_input> SampleList
{
get { return _sampleList; }
set {
SetProperty(ref _sampleList, value);
}
}
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
[RelayCommand]
public void GetData()
{
DateTime dt1 = DateTime.Parse(StartDate.ToString("yyyy-MM-dd") + " 00:00:00");
DateTime dt2 = DateTime.Parse(EndDate.ToString("yyyy-MM-dd") + " 23:00:00");
SampleList = sampleSequenceBll.GetTubeData(dt1, dt2);
}
[RelayCommand]
public void ExportExcel()
{
if (SampleList == null || SampleList.Count == 0)
{
MessageBox.Show("没有数据可导出", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
return;
}
try
{
// 设置EPPlus许可证为个人使用
ExcelPackage.LicenseContext = OfficeOpenXml.LicenseContext.NonCommercial;
// 创建保存文件对话框
Microsoft.Win32.SaveFileDialog saveFileDialog = new Microsoft.Win32.SaveFileDialog();
saveFileDialog.Filter = "Excel文件 (*.xlsx)|*.xlsx";
saveFileDialog.FileName = "样品查询导出_" + DateTime.Now.ToString("yyyyMMddHHmmss");
if (saveFileDialog.ShowDialog() == true)
{
string filePath = saveFileDialog.FileName;
// 创建Excel包
using (ExcelPackage package = new ExcelPackage())
{
// 创建工作表
ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("样品数据");
// 设置表头
worksheet.Cells[1, 1].Value = "二维码";
worksheet.Cells[1, 2].Value = "样品名称";
worksheet.Cells[1, 3].Value = "标准ID";
worksheet.Cells[1, 4].Value = "标准名称";
worksheet.Cells[1, 5].Value = "录入人员";
worksheet.Cells[1, 6].Value = "录入日期";
worksheet.Cells[1, 7].Value = "批号";
// 设置表头样式
using (var range = worksheet.Cells["A1:G1"])
{
range.Style.Font.Bold = true;
range.Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
range.Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.LightGray);
range.Style.HorizontalAlignment = OfficeOpenXml.Style.ExcelHorizontalAlignment.Center;
}
// 填充数据
for (int i = 0; i < SampleList.Count; i++)
{
var sample = SampleList[i];
worksheet.Cells[i + 2, 1].Value = sample.qrcode;
worksheet.Cells[i + 2, 2].Value = sample.item_name;
worksheet.Cells[i + 2, 3].Value = sample.standrad_id;
worksheet.Cells[i + 2, 4].Value = sample.standrad;
worksheet.Cells[i + 2, 5].Value = sample.input_user;
worksheet.Cells[i + 2, 6].Value = sample.input_date;
worksheet.Cells[i + 2, 7].Value = sample.bach_no;
}
// 自动调整列宽
worksheet.Cells[worksheet.Dimension.Address].AutoFitColumns();
// 保存文件
FileInfo file = new FileInfo(filePath);
package.SaveAs(file);
MessageBox.Show("导出成功!", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
}
}
}
catch (Exception ex)
{
MessageBox.Show("导出失败:" + ex.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}
}