在我的c#应用程序中,我使用OLEDB连接字符串“Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\test.xls;Extended Properties=\"Excel 8.0;HDR=NO;ReadOnly=true;IMEX=1\"
”来读取Excel文件。 为了读取受密码保护的文件,我尝试在连接字符串中添加密码字段,但无法读取文件。 我想知道有没有办法使用OLEDB读取密码保护的Excel文件,如果我事先知道密码的话。使用C#中的OLEDB读取密码保护的excel文件
11
A
回答
5
这里是different ways to connect to an Excel file,包括OLEDB。据此,您不能用标准方法打开受密码保护的文件。您必须使用解决方法。
如果Excel工作簿是由 密码保护的,你不能打开它 数据访问,甚至用您的连接字符串 供应 正确的密码。如果您尝试,您会收到 以下错误信息:“无法 解密文件
This is the solution,尽管不是在C#中,但你可以轻松适应它为您的目的
如果你不”。自己知道密码,另一种是重新编写的文件没有密码,您可以使用this handy project和下面的程序添加到它:。
public void SaveFile()
{
this.excelWorkbook.SaveAs(
this.excelWorkbook.FullName,
vk_format,
"",
vk_write_res_password,
vk_read_only,
null,
Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange,
null,
vk_add_to_mru,
null,null,vk_local);
}
2
您可以使用OoXmlCrypto stream访问Office 2007加密文件。开放源代码,包含修改后的ExcelPackage。
示例代码:
using (OfficeCryptoStream stream = OfficeCryptoStream.Open("a.xlsx", "password"))
{
// Do stuff (e.g. create System.IO.Packaging.Package or
// ExcelPackage from the stream, make changes and save)
// Change the password (optional)
stream.Password = "newPassword";
// Encrypt and save the file
stream.Save();
}
5
如果您使用查询来读取Excel文件,它不会如一些板的保护重要:它仍然可以正常工作。
private string ExcelConnection(string fileName)
{
return
@"Provider=Microsoft.Jet.OLEDB.4.0;" +
@"Data Source=" + fileName + ";" +
@"Extended Properties=" + Convert.ToChar(34).ToString() +
@"Excel 8.0" + Convert.ToChar(34).ToString() + ";";
}
private DataTable readExcel(string fileName, string sql)
{
OleDbConnection conn = new OleDbConnection(ExcelConnection(fileName));
OleDbCommand cmd = new OleDbCommand(sql, conn);
OleDbDataAdapter adp = new OleDbDataAdapter();
adp.SelectCommand = cmd;
DataTable dt = new DataTable();
try
{
adp.FillSchema(dt, SchemaType.Source);
adp.Fill(dt);
}
catch
{
}
return dt;
}
1
经过我反复研究,终于找到了2件东西。
1.使用OLEDB,无法读取密码保护的excel文件。
2.即使Interop可以读取excel文件,无论密码保护与否,其性能都不如OLEDB。
所以,我结合
1 OLEDB它具有非常好的性能和
2.互操作可以读取每一个Excel文件创建下面的代码。
public DataTable ReadPasswordProtectedExcel(string ExcelFilePath, string Password)
{
String TempExcelFilePath = string.Empty;
DataTable _DataTable = new DataTable();
#region Get ExcelFile and Remove Password
{
String TempExcelFileName = string.Empty;
String DirectoryPath = string.Empty;
Microsoft.Office.Interop.Excel.Application excelapp = new Microsoft.Office.Interop.Excel.Application();
excelapp.Visible = false;
Microsoft.Office.Interop.Excel.Workbook newWorkbook = excelapp.Workbooks.Open(ExcelFilePath, 0,
true, 5, Password, "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true,
false, 0, true, false, false);
TempExcelFileName = string.Format("{0}_{1}", "__", Path.GetFileName(ExcelFilePath)); // __xxx.xlsx
TempExcelFilePath = String.Format("{0}/{1}", Path.GetDirectoryName(ExcelFilePath), TempExcelFileName);
/// Create new excel file and remove password.
newWorkbook.SaveAs(TempExcelFilePath, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookDefault, "", "",
false, false, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
newWorkbook.Close(true, "", false);
excelapp.Quit();
Marshal.ReleaseComObject(excelapp);
}
#endregion
#region Get data from excel file by using OLEDB
{
_DataTable = ReadExcelFileInOLEDB(TempExcelFilePath);
///Delete excel file
File.Delete(TempExcelFilePath);
}
#endregion
return _DataTable;
}
public DataTable ReadExcelFileInOLEDB(string _ExcelFilePath)
{
string ConnectionString = string.Empty;
string SheetName = string.Empty;
DataTable _DataTable = null;
DataSet _DataSet = null;
try
{
ConnectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 12.0;HDR=YES;IMEX=0;'", _ExcelFilePath);
using (OleDbConnection _OleDbConnection = new OleDbConnection(ConnectionString))
{
_OleDbConnection.Open();
_DataTable = _OleDbConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
if (_DataTable == null)
return null;
SheetName = _DataTable.Rows[0]["TABLE_NAME"].ToString();
ConnectionString = string.Format("SELECT * FROM [{0}]", SheetName);
using (OleDbCommand _OleDbCommand = new OleDbCommand(ConnectionString, _OleDbConnection))
{
using (OleDbDataAdapter _OleDbDataAdapter = new OleDbDataAdapter())
{
_OleDbDataAdapter.SelectCommand = _OleDbCommand;
_DataSet = new DataSet();
_OleDbDataAdapter.Fill(_DataSet, "PrintInfo");
return _DataSet.Tables["PrintInfo"];
}
}
}
}
catch (Exception ex)
{
throw ex;
}
}
最后,如果你想在Excel中检索数据删除空行,请this link和下面的代码
SELECT * FROM NAMED_RANGE WHERE [YourColumnTitle] IS NOT NULL
相关问题
- 1. 读取密码保护excel文件,而不使用OLEDB在c#
- 2. 读取密码保护的Excel文件使用Java
- 3. 密码使用Python保护Excel文件
- 4. 保护密码如何读取数据的Excel文件与Informatica
- 5. ExcelQueryFactory读取密码保护excel
- 6. 使用matlab读密码保护excel文件
- 7. 保存密码保护的Excel文件在C#中的XML(我知道密码)
- 8. 使用OleDB读取c#中的excel文件?
- 9. 我们如何读取使用POI API的受保护密码excel文件(.xls)
- 10. C#Excel文件OLEDB读取HTML导入
- 11. C#NUnrar库(提取无密码密码保护的rar文件)
- 12. 如何将密码保护的excel文件读入r?
- 13. 如何读写密码保护的excel文件?
- 14. 密码保护excel
- 15. 如何在java中读取密码保护的.xls文件?
- 16. 使用DotNetZip解密密码保护的压缩文件C#
- 17. 使用特定文化读取使用OLEDB的excel 2007文件
- 18. 用密码保护pdf文件c#
- 19. 读取/写入Excel 2007密码保护文档
- 20. 使用OLEDB读取excel
- 21. 如何使用PHP生成密码保护的Excel文件?
- 22. 如何使用python打开密码保护的excel文件?
- 23. 使用ireport生成Excel文件的密码保护
- 24. 读取OLEDB打开的Excel文件
- 25. itextsharp读密码保护文件到流
- 26. 如何读取密码保护的Excel在python
- 27. 使用OLEDB从打开的Excel中读取数据文件
- 28. ASP.NET从密码保护网络中读取文件共享
- 29. 使用密码保护Android .txt文件
- 30. 使用web.config密码保护文件夹
PWD =密码你试过吗? – Havenard 2009-08-31 06:20:42