Excel Data Reader Issues, column Names, and Sheet Selection

家住魔仙堡 提交于 2019-12-07 17:50:28

问题


I am using Excel Data Reader to read some data in to an Entity Framework Database

The code below is working but i need some further refinements

First of all IsFirstRowAsColumnNames does not seem to be working as intended and I have to use .Read instead.

The fudge i had in originally to select a particular sheet was has scuppered plans, can anyone help with this excelReader.Name at the moment is pointless unless i can specifically loop through or select a sheet, which I originally used .Read to achieve hence the conflict.

It would also be nice to refer to the actual column header names to retrieve the data rather than indexes such as var name = reader["applicationname"].ToString() in SQL client;

Is there perhaps a better Extension i could use to read in excel data if i can't achieve the above.

public static void DataLoadAliases(WsiContext context)
    {
        const string filePath = @"Alias Master.xlsx";

        var stream = File.Open(filePath, FileMode.Open, FileAccess.Read);

        var excelReader = filePath.Contains(".xlsx")
                      ? ExcelReaderFactory.CreateOpenXmlReader(stream)
                      : ExcelReaderFactory.CreateBinaryReader(stream);

       excelReader.IsFirstRowAsColumnNames = true;


        excelReader.Read(); //skip first row

        while (excelReader.Read())
        {

            if (excelReader.Name == "Alias Master")
            {
                var aliasId = excelReader.GetInt16(0);
                var aliasName = excelReader.GetString(1);

                //Prevent blank lines coming in from excel;
                if (String.IsNullOrEmpty(aliasName)) continue;

                context.Aliases.Add(new ApplicationAlias
                {
                    AliasId = aliasId,
                    Name = aliasName,
                });
            }
            else
            {
                excelReader.NextResult();
            }
        }

        excelReader.Close();
        context.SaveChanges();
    }

回答1:


for .XLSX file i use OpenXML SDK : http://www.microsoft.com/en-us/download/details.aspx?id=30425

for XLS file i use a OleDbConnection as see below :

 OleDbConnection oledbConn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + FilePath+ ";Extended Properties='Excel 12.0;HDR=NO;IMEX=1;';");
            oledbConn.Open();
            OleDbCommand cmd = new OleDbCommand();
            OleDbDataAdapter oleda = new OleDbDataAdapter();
            DataSet ds = new DataSet();

            DataTable dt = oledbConn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null);
            string workSheetName = (string)dt.Rows[0]["TABLE_NAME"];

            cmd.Connection = oledbConn;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "SELECT * FROM [" + workSheetName + "]";

            oleda = new OleDbDataAdapter(cmd);

            oleda.Fill(ds, "Donnees");

            oledbConn.Close();
            return ds.Tables[0];



回答2:


        DataTable DT = new DataTable(); 
        FileStream stream = File.Open(Filepath, FileMode.Open, FileAccess.Read);
        IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
        DataSet result = excelReader.AsDataSet();
        excelReader.Close();
        DT = result.Tables[0];


来源:https://stackoverflow.com/questions/21849756/excel-data-reader-issues-column-names-and-sheet-selection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!