我正在尝试使用我目前正在开发的 Windows 应用程序读取我的 excel 文件。
它由 8 个标头组成,包含近 300 个数据值。
如果您知道解决方案,请帮助我;(
还有一个问题,我可以使用什么 nuget 来操作每个标题以自行扩展,以便数据可读。
我的代码副本:
private void btnBrowse_Click(object sender, EventArgs e)
{
using (OpenFileDialog openFileDialog = new OpenFileDialog() { Filter = "MS Excel 2007 onwards |*xls" })
{
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); //add sheet to combobox
}
}
}