private void btnSaveReport_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                //先关掉Excel
                Process[] procs = Process.GetProcessesByName("excel");
                foreach (Process pro in procs)
                {
                    pro.Kill();//没有更好的方法,只有杀掉进程
                    GC.Collect();
                }

                Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
                dlg.Filter   = "xls files(*.xls)|*.xls";
                dlg.FileName = "ND1633IQC测试报告" + System.DateTime.Now.ToString("yyyy-MM-dd") + ".xls";
                //dlg.InitialDirectory = "D:\\";
                dlg.AddExtension     = false;
                dlg.RestoreDirectory = true;
                System.Nullable <bool> result = dlg.ShowDialog();
                if (result == true)
                {
                    string filePath = dlg.FileName.ToString();
                    if (File.Exists(filePath))
                    {
                        File.Delete(filePath);
                    }
                    File.Copy(sourceFilePath, filePath);
                    tbFilePath.Text   = filePath;
                    excel             = new Microsoft.Office.Interop.Excel.Application();
                    excel.Visible     = false;
                    excel.UserControl = true;
                    object missing = System.Reflection.Missing.Value;
                    wb = excel.Application.Workbooks.Open(tbFilePath.Text.Trim(), missing, missing, missing, missing, missing, missing, missing, missing,
                                                          missing, missing, missing, missing, missing, missing);
                    Worksheet worksheet = (Worksheet)wb.Worksheets.get_Item(1);//取得第一个工作簿
                    worksheet.Cells[1, 2] = DateTime.Now.ToString("yyyy/MM/dd");
                    worksheet.Cells[2, 2] = "1633-PH";
                    excel.DisplayAlerts   = false;
                    wb.Save();

                    AutoClosedMsgBox.Show("报告生成成功!", "提示", 1000, 64);
                    SnList.Clear();
                    index = 8;
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "提示", MessageBoxButton.OK, MessageBoxImage.Information);
                Process[] procs = Process.GetProcessesByName("excel");
                foreach (Process pro in procs)
                {
                    pro.Kill();//没有更好的方法,只有杀掉进程
                }
            }
        }
        private void btnOpenReport_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                //先关掉Excel
                Process[] procs = Process.GetProcessesByName("excel");
                foreach (Process pro in procs)
                {
                    pro.Kill();//没有更好的方法,只有杀掉进程
                    GC.Collect();
                }
                wb = null; excel = null;
                Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
                dlg.Filter           = "xls files(*.xls)|*.xls";
                dlg.AddExtension     = false;
                dlg.RestoreDirectory = true;
                System.Nullable <bool> result = dlg.ShowDialog();
                if (result == true)
                {
                    string filePath = dlg.FileName.ToString();
                    if (!filePath.Contains("ND1633IQC测试报告"))
                    {
                        if (MessageBoxResult.No == MessageBox.Show("选择的文件的文件名不包含特定字符,该文件可能与模板不一致,是否继续打开?", "提示", MessageBoxButton.YesNo, MessageBoxImage.Information))
                        {
                            return;
                        }
                    }
                    excel             = new Microsoft.Office.Interop.Excel.Application();
                    excel.Visible     = false;
                    excel.UserControl = true;
                    object missing = System.Reflection.Missing.Value;
                    wb = excel.Application.Workbooks.Open(filePath, missing, missing, missing, missing, missing, missing, missing, missing,
                                                          missing, missing, missing, missing, missing, missing);
                    Worksheet worksheet     = (Worksheet)wb.Worksheets.get_Item(1);//取得第一个工作簿
                    bool      isfitTemplate = false;
                    if (((Range)worksheet.Cells[1, 1]).Text == "time" && ((Range)worksheet.Cells[2, 1]).Text == "model" && ((Range)worksheet.Cells[2, 2]).Text == "1633-PH")
                    {
                        tbFilePath.Text = filePath;
                        isfitTemplate   = true;
                        int rowsNum = worksheet.UsedRange.Cells.Rows.Count;
                        index = 8;
                        SnList.Clear();
                        for (int i = index; i <= rowsNum; i++)
                        {
                            string sn = ((Range)worksheet.Cells[i, 1]).Text;
                            if (!string.IsNullOrEmpty(sn))
                            {
                                SnList.Add(sn);
                            }
                            index++;
                        }
                    }
                    else
                    {
                        isfitTemplate = false;
                    }

                    excel.DisplayAlerts = false;
                    wb.Save();
                    if (isfitTemplate)
                    {
                        AutoClosedMsgBox.Show("报告打开成功!", "提示", 1000, 64);
                    }
                    else
                    {
                        MessageBox.Show("打开的Excel文件格式和模板格式不一致,请检查!", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
                    }
                }
            }
            catch (Exception ex)
            {
                Process[] procs = Process.GetProcessesByName("excel");
                foreach (Process pro in procs)
                {
                    pro.Kill();//没有更好的方法,只有杀掉进程
                    GC.Collect();
                }
            }
        }
 private void btnSaveData_Click(object sender, RoutedEventArgs e)
 {
     try
     {
         if (string.IsNullOrEmpty(tbFilePath.Text.Trim()))
         {
             MessageBox.Show("报告保存路径不能为空!请先生成测试报告!", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
             return;
         }
         if (string.IsNullOrEmpty(tbSn.Text.Trim()))
         {
             MessageBox.Show("PCB条码不能为空!请先扫条码!", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
             return;
         }
         if (!File.Exists(tbFilePath.Text.Trim()))
         {
             MessageBox.Show(string.Format("报告 {0} 不存在,请确认!", tbFilePath.Text.Trim()), "提示", MessageBoxButton.OK, MessageBoxImage.Information);
             return;
         }
         if (SnList.Contains(tbSn.Text.Trim()))
         {
             MessageBox.Show(string.Format("条码 {0} 已存在!", tbSn.Text.Trim()), "提示", MessageBoxButton.OK, MessageBoxImage.Information);
             return;
         }
         btnSaveData.IsEnabled   = false;
         btnBuildBatch.IsEnabled = false;
         //Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
         //excel.Visible = false;
         //excel.UserControl = true;
         //object missing = System.Reflection.Missing.Value;
         //Workbook wb = excel.Application.Workbooks.Open(tbFilePath.Text.Trim(), missing, missing, missing, missing, missing, missing, missing, missing,
         //              missing, missing, missing, missing, missing, missing);
         if (excel != null && wb != null)
         {
             Worksheet ws = (Worksheet)wb.Worksheets.get_Item(1); //取得第一个工作簿
                                                                  //ws.Cells[1, 2] = DateTime.Now.ToString("yyyy/MM/dd");
                                                                  //ws.Cells[2, 2] = "1633-PH";
             WriteTestData(ws, tbSn.Text.Trim());
             //int offset = 1;
             //ws.Cells[index, offset] = tbSn.Text.Trim(); offset++;
             //Random ovrandom = new Random(); ws.Cells[index, offset] = GetRandomNumber(ovrandom, OVItem.minValue, OVItem.maxValue, OVItem.decimalLen); offset++;
             //Random ocdsgrandom = new Random(); ws.Cells[index, offset] = GetRandomNumber(ocdsgrandom, OCDSGItem.minValue, OCDSGItem.maxValue, OCDSGItem.decimalLen); offset++;
             //Random cuvrandom = new Random();
             //foreach (var it in CUVItemList)
             //{
             //    Thread.Sleep(1);
             //    ws.Cells[index, offset] = GetRandomNumber(cuvrandom, it.minValue, it.maxValue, it.decimalLen);
             //    offset++;
             //}
             //Random covrandom = new Random();
             //foreach (var it in COVItemList)
             //{
             //    Thread.Sleep(1);
             //    ws.Cells[index, offset] = GetRandomNumber(covrandom, it.minValue, it.maxValue, it.decimalLen);
             //    offset++;
             //}
             //Random cclrandom = new Random();
             //Thread.Sleep(1);
             //ws.Cells[index, offset] = GetRandomNumber(cclrandom, CCLItem.minValue, CCLItem.maxValue, CCLItem.decimalLen); offset++;
             //Random balrandom = new Random();
             //foreach (var it in BALItemList)
             //{
             //    Thread.Sleep(1);
             //    ws.Cells[index, offset] = GetRandomNumber(balrandom, it.minValue, it.maxValue, it.decimalLen);
             //    offset++;
             //}
             //Random nscrandom = new Random();
             //double totalNSC = 0;
             //foreach (var it in NSCItemList)
             //{
             //    Thread.Sleep(1);
             //    double val = GetRandomNumber(nscrandom, it.minValue, it.maxValue, it.decimalLen);
             //    ws.Cells[index, offset] = val;
             //    totalNSC += val;
             //    offset++;
             //}
             //ws.Cells[index, offset] = totalNSC;
             //offset++;
             //double totalSSC = 0;
             //Random sscrandom = new Random();
             //foreach (var it in SSCItemList)
             //{
             //    Thread.Sleep(1);
             //    double val = GetRandomNumber(sscrandom, it.minValue, it.maxValue, it.decimalLen);
             //    ws.Cells[index, offset] = val;
             //    totalSSC += val;
             //    offset++;
             //}
             //ws.Cells[index, offset] = totalSSC;
             //offset++;
             //ws.Cells[index, offset] = "pass";
             index++;
             excel.DisplayAlerts = false;
             wb.Save();
             AutoClosedMsgBox.Show("数据保存成功!", "提示", 1000, 64);
             SnList.Add(tbSn.Text.Trim());
             btnBuildBatch.IsEnabled = true;
             btnSaveData.IsEnabled   = true;
             tbSn.Text = string.Empty;
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message, "提示", MessageBoxButton.OK, MessageBoxImage.Information);
         btnBuildBatch.IsEnabled = true;
         btnSaveData.IsEnabled   = true;
         Process[] procs = Process.GetProcessesByName("excel");
         foreach (Process pro in procs)
         {
             pro.Kill();//没有更好的方法,只有杀掉进程
         }
         GC.Collect();
     }
 }