[英]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 种使用办公格式的选项:
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.