🙂 İNSANLARIN EN HAYIRLISI INSANLARA FAYDALI OLANDIR 🙂

Ramazan HABER / C# RMOS / GetExcelSheetNames excel sayfa isimlerini alma

1-) C# RMOS - GetExcelSheetNames excel sayfa isimlerini alma

private String[] GetExcelSheetNames(string strFile)

        {

            OleDbConnection objConn = null;

            System.Data.DataTable dt = null;

 

            try

            {

                string strConnectionString = "";

                if (strFile.Trim().EndsWith(".xlsx"))

                {

                    strConnectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1\";", strFile);

                }

                else if (strFile.Trim().EndsWith(".xls"))

                {

                    strConnectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\";", strFile);

                }

                else if (strFile.Trim().EndsWith(".xlsm"))

                {

                    strConnectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0 Macro;HDR=Yes;IMEX=1\";", strFile);

                }

                objConn = new OleDbConnection(strConnectionString);

                // Open connection with the database.

                objConn.Open();

                // Get the data table containg the schema guid.

                dt = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);

 

                if (dt == null)

                {

                    return null;

                }

 

                String[] excelSheets = new String[dt.Rows.Count];

                int i = 0;

 

                // Add the sheet name to the string array.

                foreach (DataRow row in dt.Rows)

                {

                    excelSheets[i] = row["TABLE_NAME"].ToString();

                    i++;

                }

 

                // Loop through all of the sheets if you want too...

                for (int j = 0; j < excelSheets.Length; j++)

                {

                    // Query each excel sheet.

                }

 

                return excelSheets;

            }

            catch (Exception ex)

            {

                return null;

            }

            finally

            {

                // Clean up.

                if (objConn != null)

                {

                    objConn.Close();

                    objConn.Dispose();

                }

                if (dt != null)

                {

                    dt.Dispose();

                }

            }

        }

 2021 Ocak 18 Pazartesi
 454