none
Removing password from excel file in c#

    Question

  • 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

    Thursday, April 19, 2007 6:52 AM

Answers

  • 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.
    Thursday, April 26, 2007 7:45 AM

All replies

  • 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 VkExcel object.

    1. Create an object of type VkExcel: VkExcel excel = new VkExcel(false);. Remember that VkExcel(...) takes a parameter for visibility of the application.
    2. Open an Excel file: string status = excel.OpenFile( filename, password );, pass in the filename or fullname if you are using a OpenFileDialog. If the file is not password protected, set password to null.
    3. Check to see if the file was opened successfully: if( status.equal("OK")) ...
    4. Retrieve Excel sheets: excel.GetExcelSheets(); will get all sheets internally in the object.
    5. Search for specific sheet: excel.FindExcelWorksheet(worksheetName); will look for any sheet in a given file. Pass in the worksheetName as parameter.
    6. Retrieve data given a range: string[] A4D4 = excel.GetRange("A4Big Smile4"); this will return values in the range in a string[].

    That's all there is to it!

    Regards.

    Monday, April 23, 2007 9:19 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 9:35 AM
  • 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

    Tuesday, April 24, 2007 1:06 PM
  • 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.
    Thursday, April 26, 2007 7:45 AM