Removing password from excel file in c#
-
Thursday, April 19, 2007 6:52 AM
Hi,
i want to read excel file. But many times it is password protected.
So it there any way to remove the password once it is opened using
known password through code.
I am using aspose.cells for opening excel files as_excel.Open(excelFileName, FileFormatType.Default, password);
I have also tried
_excel.Unprotect(password);but even then password is not removed from the excel file.
Please provide me the code for this asap
Thanks
All Replies
-
Monday, April 23, 2007 9:19 AM
Hi,here is a sample allows you to open Excel files with password
http://www.codeproject.com/office/package.asp
You will just need to follow these steps to initialize and use the
VkExcelobject.- Create an object of type
VkExcel:VkExcel excel = new VkExcel(false);. Remember thatVkExcel(...)takes a parameter for visibility of the application. - Open an Excel file:
string status = excel.OpenFile( filename, password );, pass in the filename or fullname if you are using aOpenFileDialog. If the file is not password protected, set password tonull. - Check to see if the file was opened successfully:
if( status.equal("OK"))... - Retrieve Excel sheets:
excel.GetExcelSheets();will get all sheets internally in the object. - Search for specific sheet:
excel.FindExcelWorksheet(worksheetName);will look for any sheet in a given file. Pass in theworksheetNameas parameter. - Retrieve data given a range:
string[] A4D4 = excel.GetRange("A4this will return values in the range in a
4");string[].
That's all there is to it!
Regards.
- Create an object of type
-
Tuesday, April 24, 2007 9:35 AM
okk but now after opening the file with password i want to remove the password.
So that next time i can open the file without password. Can password be removed from the excel file using c# code.
-
Tuesday, April 24, 2007 1:06 PM
hi friend,
the code given is used for creating excel file with password
What i need is removal of password once it is opened from known password so that next time when i open the excel file it doesnt asks me for password.
Thanks
-
Thursday, April 26, 2007 7:45 AM
You can over write(save) the Excel file with empty password. Add following function into the class in the project http://www.codeproject.com/office/package.asp
Code Snippet
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);
}
Set the value of vk_read_only to vk_false in the VkExcel class.
Invoke this method where you want to remove the password of the excel file.


