mengshunguo před 1 měsícem
rodič
revize
a84486ab56
1 změnil soubory, kde provedl 37 přidání a 16 odebrání
  1. 37 16
      SCADA_DAQ/Customer/UctFrmMain.xaml.cs

+ 37 - 16
SCADA_DAQ/Customer/UctFrmMain.xaml.cs

@@ -3,6 +3,7 @@ using NPOI.HSSF.UserModel;
 using NPOI.SS.Formula.Functions;
 using NPOI.SS.UserModel;
 using NPOI.XSSF.UserModel;
+using QRCoder;
 using SCADA.Comm;
 using SCADA.CommonCtrl;
 using SCADA.CommonCtrl.AnimationHelper;
@@ -21,6 +22,7 @@ using System.Data;
 using System.IO;
 using System.IO.Ports;
 using System.Linq;
+using System.Runtime.InteropServices;
 using System.Runtime.InteropServices.WindowsRuntime;
 using System.Text;
 using System.Threading.Tasks;
@@ -48,12 +50,12 @@ namespace SCADA_DAQ.Customer
         /// <summary>
         /// excel文件信息
         /// </summary>
-        private DataTable ExcelData = new DataTable();
+        private DataTable ExcelData;
 
         /// <summary>
         /// 条码对应的长宽信息
         /// </summary>
-        private Dictionary<string, BarcodeInformation> BarcodeInfo = new Dictionary<string, BarcodeInformation>();
+        private Dictionary<string, BarcodeInformation> BarcodeInfo;
         /// <summary>
         /// 条形码的索引
         /// </summary>
@@ -76,6 +78,9 @@ namespace SCADA_DAQ.Customer
         {
             InitializeComponent();
             ToolBar.Items.Add(myButton = new OpenFileButton() { Margin = new Thickness(15, 0, 0, 0) });
+            //var comTest = new ToolBarButton() { ButtonKind = SCADA.CommonCtrl.ButtonKind.StartButton, Title = "清除" };
+            //ToolBar.Items.Add(comTest);
+            //comTest.Click += ComTest_Click; ;
             myButton.Click += MyButton_Click;
             if (BarcodeReader1 != null)
             {
@@ -84,6 +89,13 @@ namespace SCADA_DAQ.Customer
             }
         }
 
+        //private void ComTest_Click(object sender, RoutedEventArgs e)
+        //{
+        //    ExcelData = new DataTable();
+        //    BarcodeInfo = new Dictionary<string, BarcodeInformation>();
+        //    DataGridDt.DataContext = "";
+        //}
+
         private void BarcodeReader1_BarCodeReceived(object sender, SCADA.BarcodeReader.BarcodeReceivedEventArgs e)
         {
             string barCode = null;
@@ -157,26 +169,35 @@ namespace SCADA_DAQ.Customer
 
         private void MyButton_Click(object sender, OpenFileButton.OpenFileButtonMouseEventArgs e)
         {
+            ExcelData = new DataTable();
+            BarcodeInfo = new Dictionary<string, BarcodeInformation>();
             string fileName = $@"{e.OpenFileName}";
             string fileExt = Path.GetExtension($@"{e.OpenFileName}").ToLower();
-            IWorkbook workbook;
-            using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read))
+            IWorkbook workbook = null;
+            try
             {
-                if (fileExt == ".xlsx")
-                {
-                    workbook = new XSSFWorkbook(fs);
-                }
-                else if (fileExt == ".xls")
+                using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read))
                 {
-                    workbook = new HSSFWorkbook(fs);
-                }
-                else
-                {
-                    workbook = null;
-                    MessageBox.Show("未能选择正确的Excel表");
-                    return;
+                    if (fileExt == ".xlsx")
+                    {
+                        workbook = new XSSFWorkbook(fs);
+                    }
+                    else if (fileExt == ".xls")
+                    {
+                        workbook = new HSSFWorkbook(fs);
+                    }
+                    else
+                    {
+                        MessageBox.Show("未能选择正确的Excel表");
+                        return;
+                    }
                 }
             }
+            catch (Exception)
+            {
+                MessageBox.Show("Excel表已在另一进程打开,请关闭选择的Excel表");
+                return;
+            }
             ISheet sheet = workbook.GetSheetAt(0);
             IRow header = sheet.GetRow(sheet.FirstRowNum);
             int startRow = sheet.FirstRowNum + 1;