简体   繁体   English

从Excel工作表C#中读取

[英]Read from Excel sheet c#

I am trying to read an excel sheet using this code 我正在尝试使用此代码阅读Excel工作表

Dictionary<string, DataTable> tables = new Dictionary<string, DataTable>();//Microsoft.Jet.OLEDB.4.0 
string sConnection = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 12.0;HDR=yes'", filePath);
OleDbConnection oleExcelConnection = new OleDbConnection(sConnection);

foreach(string sheet in Sheets)
{
    DataTable dt = GetDataTable("SELECT * from [" + sheet + "$]", sConnection);

    tables.Add(sheet, dt); 
}

After I run it got this exception The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine. 运行后,出现此异常The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine.

and I downloaded the component from here ' https://www.microsoft.com/en-us/download/confirmation.aspx?id=23734 ' and worked correctly but when I deploy it it doesn't work anyone can help ? 我从这里' https://www.microsoft.com/en-us/download/confirmation.aspx?id=23734 '下载了该组件,并且可以正常工作,但是当我部署该组件时,任何人都无法提供帮助?

Please use execlDatareader for nugget package and use this code .Please change path to user file location. 请对块包使用execlDatareader并使用此代码。请将路径更改为用户文件位置。

execldatareader work everywhere but ole db must be installed on machine if u goes live with ole db can give u error. execldatareader可以在任何地方工作,但是如果您使用ole db上线,则必须在计算机上安装ole db会给您错误。

string path1 = Path.Combine(HttpContext.Server.MapPath("~/App_Data/" + sessionManagement.GetUserId()), Path.GetFileName(model.FileName));
                FileStream stream = System.IO.File.Open(path1, FileMode.Open, FileAccess.Read);

                IExcelDataReader reader = null;
                DataSet result = new DataSet();
                try
                {
                    if (path1.EndsWith(".xls"))
                    {
                        reader = ExcelReaderFactory.CreateBinaryReader(stream);
                        reader.IsFirstRowAsColumnNames = true;
                    }
                    if (path1.EndsWith(".xlsx"))
                    {
                        reader = ExcelReaderFactory.CreateOpenXmlReader(stream);
                        reader.IsFirstRowAsColumnNames = true;
                    }
                    result = reader.AsDataSet();
                    reader.Close();
                }
                catch (Exception ex)
                {
                    Dispose();
   }
                DataTable dt = result.Tables[0];

Your MS ACE driver version (x86 / x64) must match targeted platform of your VS solution. 您的MS ACE驱动程序版本(x86 / x64)必须与VS解决方案的目标平台匹配。 Click on Project menu>Properties. 单击项目菜单>属性。 Select "Build" tab. 选择“构建”选项卡。 On your screen, locate a.combobox placed immediate right of label [Platform target:].change value of combobox from "Any CPU" to x86 or x64 whichever version you have and you will get rid of that error. 在屏幕上找到位于标签[Platform target:]右边的a.combobox。将combobox的值从“ Any CPU”更改为x86或x64,无论使用哪种版本,您都将摆脱该错误。

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

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