简体   繁体   English

如何在 C# GUI 中搜索 Excel 工作表?

[英]How can I search through an Excel sheet in a C# GUI?

I have this code我有这个代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TabeleExcel
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void cboSheet_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataTable dt = tableCollection[cboSheet.SelectedItem.ToString()];
            dataGridView1.DataSource = dt;
        }

        DataTableCollection tableCollection;

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            using(OpenFileDialog openFileDialog=new OpenFileDialog() { Filter="Excel|*.xls|Excel|*.xlsx" })
            {
                if(openFileDialog.ShowDialog()==DialogResult.OK)
                {
                    txtFilename.Text = openFileDialog.FileName;
                    using(var stream=File.Open(openFileDialog.FileName, FileMode.Open, FileAccess.Read))
                    {
                        using (IExcelDataReader reader = ExcelReaderFactory.CreateReader(stream))
                        {
                            DataSet result = reader.AsDataSet(new ExcelDataSetConfiguration()
                            {
                                ConfigureDataTable=(_)=>new ExcelDataTableConfiguration() { UseHeaderRow=true }
                            });
                            tableCollection = result.Tables;
                            cboSheet.Items.Clear();
                            foreach (DataTable table in tableCollection)
                                cboSheet.Items.Add(table.TableName);
                        }
                    }
                }
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {


        }
    }
}

The code makes a GUI in .NET framework and it lets you upload an.xls or.xlsx, the Excel file will be shown as a DataGrid view.该代码在 .NET 框架中创建了一个 GUI,它允许您上传.xls 或.xlsx,Excel 文件将显示为 DataGrid 视图。 I implemented a new textbox ( the textBox1_TextChanged(object sender, EventArgs e) method), and based on the user's input, I want to it to display only the rows that correspond with the input.我实现了一个新的文本框( textBox1_TextChanged(object sender, EventArgs e) 方法),并根据用户的输入,我希望它只显示与输入对应的行。 How am I supposed to do that?我该怎么做? Thanks in advance!提前致谢!

You have 3 options for working with office formats:您有 3 种使用办公格式的选项:

  • if you only need to support the new formats (.xslx) you can use the OpenXML SDK , any of the wrappers people wrote for it.如果您只需要支持新格式 (.xslx),您可以使用OpenXML SDK ,人们为它编写的任何包装器。 Or even the ZipArchive and XMLReader classes.甚至是 ZipArchive 和 XMLReader 类。 It is well known and easily processed众所周知且易于加工
  • If you need to support the old format as well (.xls) you have to use the (t)rusty Office COM interop.如果您还需要支持旧格式 (.xls),则必须使用 (t)rusty Office COM 互操作。 That one has the usualy issues of COM interop, plus some unique problems.那个有 COM 互操作的常见问题,以及一些独特的问题。 Like requiring a Interactive Session due to poor design choices.就像由于设计选择不佳而需要交互式 Session 一样。
  • For any given problem, any given file format, any given GUI technology there might be another way.对于任何给定的问题、任何给定的文件格式、任何给定的 GUI 技术,都可能有另一种方法。 But those are far and few in between但这些在两者之间是非常少的

My standing advice is to use the OpenXML way and learn to live without the old formats altogether.我的一贯建议是使用 OpenXML 方式并学会完全摆脱旧格式。 They are way more trouble to support then it is worth.支持他们的麻烦要多得多,这是值得的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM