How Load Excel File With Password In C#

11,460

Please : use this lib:

using Microsoft.Office.Interop.Excel

Please , provide password :

WorkbookObject.Password = password;

Please, Change ConnString:

string conn = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + s + ";Password=password;Extended Properties='Excel 8.0;HDR=YES'";

Here, your answer is:

 if (FileName.Substring(FileName.LastIndexOf('.')).ToLower() == ".xlsx")
 {
     strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + FileName + ";Password=password;Extended Properties=\"Excel 12.0;HDR=" + HDR + ";IMEX=0\"";
 }
 else
 {
     strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + FileName + ";Password=password;Extended Properties=\"Excel 8.0;HDR=" + HDR + ";IMEX=0\"";
 }
Share:
11,460
Mahdi Radi
Author by

Mahdi Radi

Updated on June 04, 2022

Comments

  • Mahdi Radi
    Mahdi Radi almost 2 years

    I Work On C# Project

    I Use Below Code For Import XLS Or XLSX File On DataSet.

    public static DataSet ImportExcelXLS(string FileName, bool hasHeaders)
    {
        string HDR = hasHeaders ? "Yes" : "No";
        string strConn;
        if (FileName.Substring(FileName.LastIndexOf('.')).ToLower() == ".xlsx")
        {
                strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + FileName + ";Extended Properties=\"Excel 12.0;HDR=" + HDR + ";IMEX=0\"";
        }
        else
        {
                strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + FileName + ";Extended Properties=\"Excel 8.0;HDR=" + HDR + ";IMEX=0\"";
        }
    
        DataSet output = new DataSet();
    
        using (OleDbConnection conn = new OleDbConnection(strConn))
        {
            conn.Open();
    
            DataTable schemaTable = conn.GetOleDbSchemaTable(
                OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
    
            foreach (DataRow schemaRow in schemaTable.Rows)
            {
                string sheet = schemaRow["TABLE_NAME"].ToString();
    
                if (!sheet.EndsWith("_"))
                {
                    try
                    {
                        OleDbCommand cmd = new OleDbCommand("SELECT * FROM [" + sheet + "]", conn);
                        cmd.CommandType = CommandType.Text;
    
                        sheet = sheet.Replace("$", "");
                        DataTable outputTable = new DataTable(sheet);
                        output.Tables.Add(outputTable);
                        new OleDbDataAdapter(cmd).Fill(outputTable);
                    }
                    catch (Exception ex)
                    {
                        throw new Exception(ex.Message + string.Format("Sheet:{0}.File:F{1}", sheet, FileName), ex);
                    }
                }
            }
        }
        return output;
    }
    

    This Code Work Correctly For Load Excel File Without Password. But When Run For Load a File With password Show Under error:

    Cannot start your application. The workgroup information file is missing or opened exclusively by another user.

    Now How Change My Code For Load Encrypt File?