Multi-page import with oledb network "_xlnm # _FilterDatabase" as sheet names

I have excel with several sheets that I want to import the
code is pretty simple and simple and should work
but my sheets keep coming back as "_xlnm # _FilterDatabase" in the debugger
and is the root of my problem

here is the relevant piece of code:

        string sheetName = "";
        file = HostingEnvironment.MapPath("~/files/master1.xlsx");
        xConnStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + file + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1\";";
        using (OleDbConnection connection = new OleDbConnection(xConnStr))
        {
            // get sheet names
            connection.Open();
            DataTable sheets = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);

            // eeo10 (2nd tab: first sheet to be imported)
            sheetName = sheets.Rows[1]["TABLE_NAME"].ToString();
            OleDbCommand command = new OleDbCommand("Select * FROM ["+sheetName+"]", connection);

            // Create DbDataReader to Data Worksheet                
            using (OleDbDataReader dr = command.ExecuteReader())
            {

                // Bulk Copy to SQL Server
                using (SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlConnx))
                {
                    bulkCopy.ColumnMappings.Add("code", "id");
                    bulkCopy.ColumnMappings.Add("category10", "category");

                    bulkCopy.DestinationTableName = "eeo10";
                    bulkCopy.WriteToServer(dr);
                }
            }

            // eeo14 (3rd tab: second sheet to be imported)
            sheetName = sheets.Rows[2]["TABLE_NAME"].ToString();
            command = new OleDbCommand("Select * FROM [" + sheetName + "]", connection);

            DataTable cols = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Columns, new object[] { null, null, sheetName, null });
            foreach (DataRow r in cols.Rows)
            {
                System.Diagnostics.Debug.WriteLine("{0} = {1}", r["COLUMN_NAME"], r["ORDINAL_POSITION"]);
            }

            // Create DbDataReader to Data Worksheet                
            using (OleDbDataReader dr = command.ExecuteReader())
            {

                // Bulk Copy to SQL Server
                using (SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlConnx))
                {
                    bulkCopy.ColumnMappings.Add("code", "id");
                    bulkCopy.ColumnMappings.Add("category14", "category");

                    bulkCopy.DestinationTableName = "eeo14";
                    bulkCopy.WriteToServer(dr);
                }
            }
        }

as indicated above, the
debugger returns sheetName = "_ xlnm # _FilterDatabase"
which is rather strange, the first eeo10 works
but eeo14 does not, because it is still trying to work with the eeo10 sheet

:
-
- , , .


!

+4
1

Excel , , , . , , System.Data.OleDb:

class Retriever
{
    public List<SheetName> GetSheetNames(OleDbConnection conn)
    {
        List<SheetName> sheetNames = new List<SheetName>();
        if (conn.State != ConnectionState.Open)
        {
            conn.Open();
        }
        DataTable excelSchema = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
        foreach (DataRow row in excelSchema.Rows)
        {
            if (!row["TABLE_NAME"].ToString().Contains("FilterDatabase"))
            {
               sheetNames.Add(new SheetName() { sheetName = row["TABLE_NAME"].ToString(), sheetType = row["TABLE_TYPE"].ToString(), sheetCatalog = row["TABLE_CATALOG"].ToString(), sheetSchema = row["TABLE_SCHEMA"].ToString() });
            }
        }
        conn.Close();
        return sheetNames;
     }
} 

class SheetName
{
     public string sheetName { get; set; }
     public string sheetType { get; set; }
     public string sheetCatalog { get; set; }
     public string sheetSchema { get; set; }
}

, , - .

!

+20

Source: https://habr.com/ru/post/1536252/


All Articles