2

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?

1
  • You can't open a password-protected Excel workbook in OleDb without first opening it using another method. Have a look here. Commented Nov 30, 2012 at 16:35

2 Answers 2

1

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\"";
 }
Sign up to request clarification or add additional context in comments.

2 Comments

how connect WorkbookObject object to my connection string?
This answer is using two totally different techniques for access Excel: Interop and OleDb. They don't work together particularly well and you certainly won't be able to set a password in Interop by only connecting to a workbook through OleDb.
0

Try for this code:

strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + FileName + ";Password=password;Extended Properties=\"Excel 8.0;HDR=" + HDR + ";IMEX=0\"";

but still the exception popped:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.