Excel读取 ExcelDataReader

using (var stream = System.IO.File.Open(textBox1.Text, FileMode.Open, FileAccess.Read))
{
    using (IExcelDataReader reader = ExcelReaderFactory.CreateReader(stream))
    {
        DataSet ds = reader.AsDataSet(new ExcelDataSetConfiguration()
        {
            ConfigureDataTable = (_) => new ExcelDataTableConfiguration() { UseHeaderRow = true }
        });

        DataTableCollection tableCollection = ds.Tables;

        if (tableCollection.Count > 0)
        {
             DataTable dt = tableCollection[0];
             if(dt.Rows.Count > 0)
             {
                  //                  
             }
             else
             {
                  MessageBox.Show("Excel内容为空!");
             }
         }
     }
}

posted @ 2022-10-24 15:32  highlightyys  阅读(95)  评论(0编辑  收藏  举报