代码主要实现扫描条形码后再Excel中的第一列进行查询 是否存在于Excel中作出提醒
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Windows;
using System.Windows.Input;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using NPOI.HSSF.UserModel;
using System.Linq;
using System.Text;
using System.IO;namespace WpfApp2
{/// <summary>/// MainWindow.xaml 的交互逻辑/// </summary>public partial class MainWindow : Window{public MainWindow(){InitializeComponent();}List<string> str;private void button_Click(object sender, RoutedEventArgs e){try{System.Windows.Forms.OpenFileDialog dialog = new System.Windows.Forms.OpenFileDialog();dialog.Multiselect = true;//该值确定是否可以选择多个文件dialog.Title = "请选择文件夹";dialog.Filter = "所有文件(*.*)|*.*";if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK){str = new List<string>();DataTable dt = ExcelToDataTable(dialog.FileName, true,this.textBox1.Text.Trim());foreach (DataRow item in dt.Rows){str.Add(item.ItemArray[0].ToString());}this.label3.Content = "共:"+ str.Count + "条";this.button.IsEnabled = false;this.button1.Visibility = Visibility.Visible;this.textBox1.IsEnabled = false;this.label.Content = "Excel导入成功";}}catch (Exception ex){this.label.Content = ex.Message;}this.textBox.Text = "";this.textBox.Focus();}private void textBox_KeyDown(object sender, KeyEventArgs e){if (e.Key == Key.Enter){try{string No = this.textBox.Text.Trim().ToUpper();if (string.IsNullOrWhiteSpace(No)){this.label.Content = "无搜索内容";return;}if (string.IsNullOrWhiteSpace(str.FirstOrDefault(p => p.Equals(No)))){listBox.Items.Add(No + ":Excel内无此编号" + (listBox.Items.Count+1));this.label.Content = "无此编号";}else{listBox.Items.Add(No + ":存在于Excel内!!!" + (listBox.Items.Count+1));this.label.Content = "此编号存在";MessageBox.Show("此编号存在Excel内");}listBox.SelectedIndex = listBox.Items.Count - 1;listBox.ScrollIntoView(listBox.SelectedItem);}catch (Exception ex){if (ex.Message == "值不能为 null。\r\n参数名: source")this.label.Content = "请浏览Excel文件\r\n在做其他操作";elsethis.label.Content = ex.Message;}this.textBox.Text = "";this.textBox.Focus();}}/// <summary>/// 将excel中的数据导入到DataTable中/// </summary>/// <param name="fileName">excel工作薄路径</param>/// <param name="sheetName">excel工作薄sheet的名称</param>/// <param name="isFirstRowColumn">第一行是否是DataTable的列名</param>/// <returns>返回的DataTable</returns>public DataTable ExcelToDataTable(string fileName, bool isFirstRowColumn, string sheetName = "Sheet1"){IWorkbook workbook = null;FileStream fs = null;ISheet sheet = null;DataTable data = new DataTable();int startRow = 0;try{fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);if (fileName.IndexOf(".xlsx") > 0) // 2007版本workbook = new XSSFWorkbook(fs);else if (fileName.IndexOf(".xls") > 0) // 2003版本workbook = new HSSFWorkbook(fs);if (sheetName != null){sheet = workbook.GetSheet(sheetName);if (sheet == null) //如果没有找到指定的sheetName对应的sheet,则尝试获取第一个sheetsheet = workbook.GetSheetAt(0);}elsesheet = workbook.GetSheetAt(0);if (sheet != null){IRow firstRow = sheet.GetRow(0);int cellCount = firstRow.LastCellNum; //一行最后一个cell的编号 即总的列数if (isFirstRowColumn){for (int i = firstRow.FirstCellNum; i < cellCount; ++i){ICell cell = firstRow.GetCell(i);if (cell != null){string cellValue = cell.StringCellValue;if (cellValue != null){DataColumn column = new DataColumn(cellValue);data.Columns.Add(column);}}}startRow = sheet.FirstRowNum + 1;}elsestartRow = sheet.FirstRowNum;//最后一列的标号int rowCount = sheet.LastRowNum;for (int i = startRow; i <= rowCount; i++){data.Rows.Add(sheet.GetRow(i).GetCell(0).ToString());}}return data;}catch (Exception ex){MessageBox.Show(ex.Message);return null;}}private void button1_Click(object sender, RoutedEventArgs e){str = null;this.button.IsEnabled = true;this.button1.Visibility = Visibility.Hidden;this.textBox1.IsEnabled = true;this.label.Content = "请选择要导入Excel";this.textBox.Text = "";this.textBox.Focus();}private void Window_Loaded(object sender, RoutedEventArgs e){this.label.Content = "请选择要导入Excel";this.button1.Visibility = Visibility.Hidden;this.textBox.Text = "";this.textBox.Focus();}}
}