LIBSWHINETOOMUCH
New Member
I am trying to unlock a protected Excel workbook in C#.NET on a hosted server (WinHost). Locally my code runs fine, however when putting it in a hosted enviorment (I can't assume they have Excel installed and I doubt they do) the code breaks. Is there a way to accomplish this? Here's my current code:\[code\] Application excelApp = new Application { Visible = false }; Workbook excelWorkbook = excelApp.Workbooks.Open(document, 0, false, 5, "", "", false, XlPlatform.xlWindows, "", true, false, 0, true, false, false); try { Sheets excelSheets = excelWorkbook.Worksheets; Worksheet sheet = (Worksheet)excelSheets.Item["Workbook Name"]; if (sheet == null) { message = "No worksheet was found entitled 'Workbook Name'. Please update your template to contain a worksheet entitled 'Workbook Name'."; return false; } sheet.Unprotect(Password: "password"); // do more stuff after this } catch (Exception e) { return false; } finally { excelWorkbook.Close(false, document, null); System.Runtime.InteropServices.Marshal.ReleaseComObject(excelWorkbook); excelApp.Quit(); }\[/code\]I'm pretty unfamiliar with using Excel with .NET so I'm happy to try another route if this wont work on a hosted server. I originally tried the following but had problems with not being able to unlock the Excel protection:\[code\] IExcelDataReader excelReader = null; try { FileStream stream = File.Open(document, FileMode.Open, FileAccess.Read); if (document.Contains(".xlsx")) excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream); else { excelReader = ExcelReaderFactory.CreateBinaryReader(stream); excelReader.Read(); } }\[/code\]I'm happy to go back to this code if I can do something about the protection, or I'll just have to make the workbook unprotected although that would be a last resort.Thanks for the help, let me know if more information is needed!Best,Tom