简体   繁体   English

使用C#从Excel到数据库ADO.net的导入数据中选择空格表

[英]select spacific sheet in import data from Excel to database ADO.net using C#

I need to select Sheet Name When I import data from Excel To Database using ADO.net in C# that is how i Import Excel File put i can't select sheet name 在C#中使用ADO.net将数据从Excel导入数据库到数据库时,我需要选择工作表名称,这就是我导入Excel文件的方式,我无法选择工作表名称

OpenFileDialog op = new OpenFileDialog();
                op.Filter = "Excel Workbook| *.xls;*.xlsx;*.xlsm";
                if (op.ShowDialog() == DialogResult.Cancel)
                    return;
                FileStream stream = new FileStream(op.FileName, FileMode.Open);
                IExcelDataReader excelreader = ExcelReaderFactory.CreateOpenXmlReader(stream);
                DataSet result = excelreader.AsDataSet();

How can I select the name of the sheet 如何选择工作表名称

Here's the source code from that library you're using: 这是您正在使用的该库的源代码:

    public System.Data.DataSet AsDataSet(bool convertOADateTime)
    {
        if (!_isValid) return null;

        DataSet dataset = new DataSet();

        for (int ind = 0; ind < _workbook.Sheets.Count; ind++)
        {
            DataTable table = new DataTable(_workbook.Sheets[ind].Name);

//table filling code snipped

            if (table.Rows.Count > 0)
                dataset.Tables.Add(table);
            table.EndLoadData();
        }
        dataset.AcceptChanges();
        Helpers.FixDataTypes(dataset);
        return dataset;
    }

It looks to me like that dataset it produces has N number of DataTable objects inside, each one has a table name of whatever name was given to the sheet when the constructor was called: new DataTable(_workbook.Sheets[ind].Name); 在我看来,它生成的数据集内部有N个DataTable对象,每个数据集都有一个表名,该表名的名称与构造函数调用时给表的名称相同: new DataTable(_workbook.Sheets[ind].Name);

You called your DataSet result . 您调用了DataSet result This code would hence list out the names of the tables inside of your dataset: 因此,此代码将列出您的数据集中的表的名称:

foreach(DataTable dt in result.Tables)
  Console.Out.WriteLine(dt.TableName);

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

相关问题 使用ADO.net从Excel导入数据库的验证 - validation on import from Excel to Database using ADO.net 如何在C#中使用ODBC读取特定Excel工作表中的所有数据行到ADO.NET数据表中 - How to read all data rows in a specific Excel sheet into an ADO.NET Datatable with out using ODBC in C# 在 C# 中通过 ADO.Net 导入 Excel - Excel import through ADO.Net in C# 使用ADO.net将数据从Azure数据库添加到下拉列表中C# - Adding data to dropdown list from Azure database using ADO.net c# 在第一行中使用没有标题的ADO.NET从Excel Sheet获取数据 - Getting Data from Excel Sheet using ADO.NET w/o Header in first row 如何使用C#ADO.NET更新Excel行? - How to update a excel row using C# ADO.NET? ADO.net如何使用C#和连接的模型在页面加载时将特定数据库表单元格中的数据绑定到转发器控件上? - ADO.net how to bind data from specific database table cell to a repeater control on page load using C# & the connected model? 使用 ADO.NET 在代码隐藏 C# 中使用 select 语句获取所有行数据 - Get all the row data with select statement in codebehind C# using ADO.NET 有没有一种方法可以使用C#从ado.net查询中提取Excel文件并向其中添加自定义列 - Is there a way for extracting an Excel file from an ado.net query and adding custom columns to it using c# 使用C#Ado.net从数据库下载.zip文件 - Download a .zip file from database using C# Ado.net
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM