C#使用第三方库ExcelDataReader读取excel数据并呈现
之前有使用Oledb读取Excel的代码:C#读取excel数据并呈现
使用这种方式读取存在需要安装数据库引擎的隐患(如需要自取)文章来源地址https://www.toymoban.com/news/detail-543633.html
using ExcelDataReader;
public static string path;
DataTableCollection tableCollection;
private void 导入ToolStripMenuItem_Click(object sender, EventArgs e)
{
using (OpenFileDialog openFileDialog = new OpenFileDialog() { Filter = "Excel 工作簿|*.xlsx|Excel 97-2003 工作簿|*.xls" })
{
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
path = 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;
comboBox1.Items.Clear();
foreach (DataTable table in tableCollection)
comboBox1.Items.Add(table.TableName);
}
}
}
}
label5.Text = "导入完成";
}
文章来源:https://www.toymoban.com/news/detail-543633.html
到了这里,关于C#使用第三方库ExcelDataReader读取excel数据并呈现的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!