C# Excel 2010 Workbook Open error

前端 未结 3 655
無奈伤痛
無奈伤痛 2021-01-01 03:26

We recently upgraded from Excel 2007 to Excel 2010, and we have found that existing code started failing.

Exception Message:

Office has detect

相关标签:
3条回答
  • 2021-01-01 03:55

    Have a look at using Application.FileValidation Property (Excel) before your Open statement.

    Returns or sets how Excel will validate files before opening them. Read/write

    Files that do not pass validation will be opened in a Protected View window. If you set the FileValidation property, that setting will remain in effect for the entire session the application is open.

    You can set it to one of the enum values in MsoFileValidationMode Enumeration

    msoFileValidationDefault

    msoFileValidationSkip

    if you set it to msoFileValidationSkip before the Open statement, it should bypass the check.

    Something like

    excelApp.FileValidation = MsoFileValidationMode.msoFileValidationSkip;
    

    before the open statement.

    0 讨论(0)
  • 2021-01-01 04:01

    We had the same issue. Our SSIS package at SQL server uses Excel.Interop to parse files. One day we installed Office 2010 x64 on new server and for some files started getting error:

    Office has detected a problem with this file. To help protect your computer this file cannot be opened.

    At the same time, other servers work good. We found distinguish in versions of Excel: 14.04763.1000 doesn't work, but 14.0.7015.1000 works for us. The last version number belong to Office 2010 SP2. Eventually we downloaded SP2 and installed it,as result, the error has gone.

    0 讨论(0)
  • 2021-01-01 04:21

    Late to the game here, but this is a common annoyance: you need to define a 'Trusted Location'.

    You're not the only developers encountering this problem when your code tries to open a spreadsheet file, and "Office has detected a problem with this file. To help protect your computer this file cannot be opened." is an extremely unhelpful error message.

    Look up the Trusted Location code published by Daniel Pineault on DevHut.net in 2010:

    DevHut code example: Trusted Location using VBScript

    I'll get downvoted to hellandgone for posting VBA in a C# forum, so I'd better not post my implementation of Daniel's code (yes, I'm a VBA developer, bashing out VBA macros all day, not a real coder working with pointy things and curly braces). If you really want to see the VBA, it's in a reply to another post:

    https://stackoverflow.com/questions/2962728/office-trusted-locations/28115700#28115700

    I believe the proper salutation is 'Share and Enjoy'.

    Do, please, put an acknowledgement of the original author, Daniel Pineault, if you reuse the code: it's been widely published without attribution on 'Expert' sites, and that's rather rude.

    0 讨论(0)
提交回复
热议问题