2

我正在尝试使用以下代码从 Excel 文件中读取列的值:

FileStream stream = File.Open("excelfile.xlsx", FileMode.Open, FileAccess.Read);

            //2. Reading from a OpenXml Excel file (2007 format; *.xlsx)
            IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
            //...
            //4. DataSet - Create column names from first row
            excelReader.IsFirstRowAsColumnNames = true;
            DataSet result = excelReader.AsDataSet();

            //5. Data Reader methods
            Console.WriteLine("Results: " +excelReader.ResultsCount);
            while (excelReader.Read())
            {
                object[] values = new object[excelReader.FieldCount];
                excelReader.GetValues(values);

            }

            //6. Free resources (IExcelDataReader is IDisposable)
            excelReader.Close();

但是它引发了一个异常:

“System.NotSupportedException 未处理 Message="O método especificado não é suportado." Source="Excel" StackTrace: 在 Excel.ExcelOpenXmlReader.GetValues(Object[] values)

我正在使用另一种方式来读取值,但我想稍微清理一下代码......这是上一个读取周期:

FileStream stream = File.Open("excelfile.xlsx", FileMode.Open, FileAccess.Read);

        //2. Reading from a OpenXml Excel file (2007 format; *.xlsx)
        IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
        //...
        //4. DataSet - Create column names from first row
        excelReader.IsFirstRowAsColumnNames = true;
        DataSet result = excelReader.AsDataSet();

        //5. Data Reader methods
        Console.WriteLine("Results: " +excelReader.ResultsCount);
        while (excelReader.Read())
        {
            Console.WriteLine("Row:"+excelReader.GetValues())
            for (int i = 0; i < excelReader.FieldCount; i++ )
            {
                Console.Write(excelReader.GetValue(i)+ "|");
            }
            Console.WriteLine("");
        }

        //6. Free resources (IExcelDataReader is IDisposable)
        excelReader.Close();

我在 getValues 上做错了吗?我以前用它来阅读 SQL 记录......也许这是我正在使用的库的限制。

4

2 回答 2

4

这确实是一个执行问题。希望有足够多的人会发现此功能对 Ian 有用,以便在未来的版本中实现它。

ExcelDataReader 目前不支持 IDataReader 的这种特定方法。不过,我会将这个问题保持开放,以便我们看看其他人是否也想要它

--by Ian1971 Excel Reader 开发者

似乎有一个实现问题,所以我将使用第一种方法。

于 2013-05-22T22:57:12.607 回答
1

此代码适用于我:请考虑:我尝试ExcelDataReader了 V2.1.2.3 版本,使用 V3.4.0 等更高版本。有问题.IsFirstRowAsColumnNames

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

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

        DataSet result;

        private void btnOpen_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog ofd = new OpenFileDialog() { Filter = "Excel Wprkbook 97-2003|*.xls| Excel Workbook|*.xlsx", ValidateNames = true })
            {
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    FileStream fs = File.Open(ofd.FileName, FileMode.Open, FileAccess.Read);
                    IExcelDataReader reader;
                    if (ofd.FilterIndex == 1)
                        reader = ExcelReaderFactory.CreateBinaryReader(fs);
                    else
                        reader = ExcelReaderFactory.CreateOpenXmlReader(fs);
                    reader.IsFirstRowAsColumnNames = true;
                    result = reader.AsDataSet();
                    cboSheet.Items.Clear();
                    foreach (DataTable dt in result.Tables)
                        cboSheet.Items.Add(dt.TableName);
                    reader.Close();

                }
            }
        }

        private void dataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }


        private void cbosheet_SelectedIndexChanged(object sender, EventArgs e)
        {
            dataGridView.DataSource = result.Tables[cboSheet.SelectedIndex];
        }
    }
}
于 2018-02-12T07:23:08.287 回答