用以下代码可成功连接excel文档
public DataTable ExcelData(string FileName, string SheetName)
        {
            DataSet dsExcel = new DataSet();
            DataTable DT = new DataTable();            string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="c:\\book.xls";Extended Properties=Excel 8.0";
            OleDbConnection oleConn = new OleDbConnection(strConn);
            oleConn.Open();
            OleDbCommand myOleDbCommand = new OleDbCommand("select * from [" + SheetName + "$]", oleConn);
            OleDbDataAdapter dataAda = new OleDbDataAdapter(myOleDbCommand);
            oleConn.Close();
            dsExcel = new DataSet();
            dataAda.Fill(dsExcel, "[" + SheetName + "$]");
            DT = dsExcel.Tables[0];
            return DT;
        }请问: 已知打开密码和修改密码的Excel文档, 如何用程序连接?

解决方案 »

  1.   

    Connection strings for Excel 2007
    Connection strings for Excel
      

  2.   

    楼上提供的打开看到如下:
    If the Excel workbook is protected by a password, you cannot open it for data access, even by supplying the correct password with your connection string. If you try, you receive the following error message: "Could not decrypt file."
    说明有密码的excel是无法连接的.虽然问题无法解决, 但是仍然感谢楼上.另外, 不能数据连接有密码的excel, 打开总行的吧? 怎么做呢?
      

  3.   

      string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:Database Password=123456;Data Source =" + filepath;
      

  4.   

    按wuyq11的试过了
    string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:Database Password=123456;Data Source =" + filepath; 
    这样还是不行啊?
      

  5.   

    还可以用文件流的方式读取Excel数据,
    详见:C#通过文件流将Excel(支持97~2003格式)转DataTable,不需要MS Office支持
      

  6.   

    我现在正好也遇见这个问题,我有一个Excel文件,我通过程序读取数据后返回一张表,但是我不想让用户看到这个文件的内容,加密之后又没办法打开,不知楼主怎么解决的啊?下面是打开没有加密的文件方法:

    public static DataTable ReadXLS(string xlsDir)
            {            DataSet ds;
                string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;" +
                                "Extended Properties=Excel 8.0;" +
                                "data source=" + xlsDir;
                OleDbConnection myConn = new OleDbConnection(strCon);
                string strCom = " SELECT * FROM [Sheet1$]";
                myConn.Open();
                OleDbDataAdapter myCommand = new OleDbDataAdapter(strCom, myConn);
                ds = new DataSet();
                myCommand.Fill(ds);
                myConn.Close();
                return ds.Tables[0];        }