Excel interop prevent showing password dialog

后端 未结 3 1177
Happy的楠姐
Happy的楠姐 2020-12-17 19:37

I am writing a program to clean excel files from empty rows and columns, i started from my own question Fastest method to remove Empty rows and Columns From Excel Files usin

3条回答
  •  别那么骄傲
    2020-12-17 19:49

    I found a solution, but i will accept other working answers

    Problem

    When passing an empty string as password the excel consider it as nothing. So it ask for a password and show the dialog.

    Solution

    The solution is to pass a single quotation as a password, excel will consider it as empty string. If workbook is not password protected it will open, else it will throw the following exception

    The password you supplied is not correct. Verify that the CAPS LOCK key is off and be sure to use the correct capitalization

    The code will be:

    m_xlWrkb = m_xlWrkbs.Open(strFile, Password:="'")
    

    Note

    In microsoft excel, single quotation on the beginning of a value is used to force text formatting.

    example; '0 is readed as a text of value 0

提交回复
热议问题