繁体   English   中英

在表格C#中读取Excel文档时出错

[英]Error reading Excel document in forms C#

我正在尝试获取XLSX数据,并使用ExcelDataReader NuGet包将其放入DataGridView中。 但是我在第35行上得到了未设置为对象错误实例的对象引用

foreach (DataTable dt in result.Tables)

这是完整的代码。

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

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

        DataSet result;

        private void butOpen_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog ofd = new OpenFileDialog())
            {
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    FileStream fs = File.Open(ofd.FileName, FileMode.Open, FileAccess.Read);
                    IExcelDataReader reader = ExcelReaderFactory.CreateBinaryReader(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 cboSheet_SelectedIndexChanged(object sender, EventArgs e)
        {
            dataGridView.DataSource = result.Tables[cboSheet.SelectedIndex];

        }
    }
}

这是错误消息: 错误

针对评论:

属性IsFirstRowAsColumnNames在该软件包的较新版本上不再可用。

从GitHub页面LINK

AsDataSet配置选项

AsDataSet()函数接受一个可选的配置对象,以修改DataSet转换的行为:

var result = reader.AsDataSet(new ExcelDataSetConfiguration() {

    // Gets or sets a value indicating whether to set the DataColumn.DataType 
    // property in a second pass.
    UseColumnDataType = true,

    // Gets or sets a callback to obtain configuration options for a DataTable. 
    ConfigureDataTable = (tableReader) => new ExcelDataTableConfiguration() {

        // Gets or sets a value indicating the prefix of generated column names.
        EmptyColumnNamePrefix = "Column",

        // Gets or sets a value indicating whether to use a row from the 
        // data as column names.
        UseHeaderRow = false,

        // Gets or sets a callback to determine which row is the header row. 
        // Only called when UseHeaderRow = true.
        ReadHeaderRow = (rowReader) => {
            // F.ex skip the first row and use the 2nd row as column headers:
            rowReader.Read();
        }
    }
});

我猜这行UseHeaderRow = true; ExcelDataTAbleConfiguration中将导致所需的行为。

编辑:添加工作示例

本示例为我使用在Excel 2016中创建的新.xlsx文件为我工作。该文件包含两张纸:工作表1和工作表2。两者都包含两列,两行文本。

List<string> tblNames = new List<string>();
OpenFileDialog ofd = new OpenFileDialog();

if (ofd.ShowDialog() ?? false)
{
    using (FileStream fs = File.Open(ofd.FileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
    {
        using (IExcelDataReader reader = ExcelReaderFactory.CreateReader(fs))
        {
            var result = reader.AsDataSet(new ExcelDataSetConfiguration()
            {
                UseColumnDataType = true,

                ConfigureDataTable = (tableReader) => new ExcelDataTableConfiguration()
                {                                
                    UseHeaderRow = true
                }
            });

            foreach (DataTable dt in result.Tables)
                tblNames.Add(dt.TableName);
        }
    }
}

这是WPF应用程序,因此OpenFileDialog的用法看起来有些不同。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM