条码查询工具(Excel版)

代码主要实现扫描条形码后再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在做其他操作";
                    else
                        this.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,则尝试获取第一个sheet
                        sheet = workbook.GetSheetAt(0);
                }
                else
                    sheet = 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;
                    }
                    else
                        startRow = 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();
        }
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值