用以下代码可成功连接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文档, 如何用程序连接?
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文档, 如何用程序连接?
Connection strings for Excel
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, 打开总行的吧? 怎么做呢?
string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:Database Password=123456;Data Source =" + filepath;
这样还是不行啊?
详见:C#通过文件流将Excel(支持97~2003格式)转DataTable,不需要MS Office支持
。
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]; }