Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将Excel导入数据网格_C#_Excel_Datagrid_Oledb_Oledbdataadapter - Fatal编程技术网

C# 将Excel导入数据网格

C# 将Excel导入数据网格,c#,excel,datagrid,oledb,oledbdataadapter,C#,Excel,Datagrid,Oledb,Oledbdataadapter,我正试图将excel文件带入我的程序,以便我可以处理数据并将其导出。我正在使用OleDB访问访问文件,并试图将其传递到datatable,以便在winform中查看和使用它。以下是我正在使用的代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data; using System.Da

我正试图将excel文件带入我的程序,以便我可以处理数据并将其导出。我正在使用OleDB访问访问文件,并试图将其传递到datatable,以便在winform中查看和使用它。以下是我正在使用的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.Windows;
using System.Windows.Forms;
using System.Globalization;
using System.Runtime.Versioning;
using System.Threading;

namespace DataImportServices
{
    public class ExcelImport : IDataImport
    {
        public DataTable ImportData(OpenFileDialog openFile)
        {
            var dataTable = new DataTable();

            string query;
            var hasHeaders = false;
            var HDR = hasHeaders ? "Yes" : "No";
            string connectionString;

            if (openFile.FileName.Substring(openFile.FileName.LastIndexOf(".")).ToLower() == ".xlsx")
            {
                connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + openFile.FileName + ";Extended Properties=\"Excel 12.0;HDR=" + HDR + ";IMEX=0\"";
            }
            else
            {
                connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + openFile.FileName + ";Extended Properties=\"Excel 8.0;HDR=" + HDR + ";IMEX=0\"";
            }

            using (OleDbConnection cn = new OleDbConnection(connectionString))
            {
                cn.Open();
                DataTable schemaTable = cn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
                DataRow schemaRow = schemaTable.Rows[0];
                string sheet = schemaRow["TABLE_NAME"].ToString();

                if (!sheet.EndsWith("_"))
                {
                    query = "SELECT * FROM [" + openFile.FileName + "]";
                    using (OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, cn))
                    {
                         dataTable.Locale = CultureInfo.CurrentCulture;
                         dataAdapter.Fill(dataTable); //The error occurs here with a "" is not an acceptable. If I put a name ie "Data" it says its not an adorecord.
                    }
                }                
            }
            return (dataTable);
        }
    }
}
我遇到的错误是当我试图用OleDBAdapter(dataAdapter)填充dataTable时。尽管只对DataTable进行了重写,但它仍会抛出一个请求ADODBrecordset的异常。我怎样才能解决这个问题?还是我遗漏了什么


提前谢谢。

您可以使用像ePlus这样的库。这支持excel本机,不使用ole或excel

您可以将数据作为数组加载并直接访问它


您可以使用像EpPlus这样的库。这支持excel本机,不使用ole或excel

您可以将数据作为数组加载并直接访问它


您可以尝试使用Exportable读取excel内容。是我很久以前开发的一个很小的库,现在作为开源在Nuget上发布

基本上,您可以读取如下Excel文件:

IImportEngine engine = new ExcelImportEngine();
var key = engine.AddContainer<DummyPersonWithAttributes>();
engine.SetDocument(pathToFile); //or MemoryStream instance
var data = engine.GetList<DummyPersonWithAttributes>(key);
IImportEngine引擎=新的ExcelImportEngine();
var key=engine.AddContainer();
engine.SetDocument(路径文件)//或内存流实例
var数据=engine.GetList(键);

您可以获得有关

的更多信息。您可以尝试使用Exportable读取excel内容。是我很久以前开发的一个很小的库,现在作为开源在Nuget上发布

基本上,您可以读取如下Excel文件:

IImportEngine engine = new ExcelImportEngine();
var key = engine.AddContainer<DummyPersonWithAttributes>();
engine.SetDocument(pathToFile); //or MemoryStream instance
var data = engine.GetList<DummyPersonWithAttributes>(key);
IImportEngine引擎=新的ExcelImportEngine();
var key=engine.AddContainer();
engine.SetDocument(路径文件)//或内存流实例
var数据=engine.GetList(键);
你可以得到更多的信息