Open a spreadsheet document for read-only access

2016-01-15 06:45:34 Written by  support iceblue
Rate this item
(0 votes)

class Program
    {
        static void Main(string[] args)
        {
            OpenSpreadsheetDocumentReadonly(@"..\..\Documents\Sheet8.xlsx");
        }
        public static void OpenSpreadsheetDocumentReadonly(string filepath)
        {
            // Open a SpreadsheetDocument based on a filepath.
            using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open(filepath, false))
            {
                // Attempt to add a new WorksheetPart.
                // The call to AddNewPart generates an exception because the file is read-only.
                WorksheetPart newWorksheetPart = spreadsheetDocument.WorkbookPart.AddNewPart();

                // The rest of the code will not be called.
            }
        }
    }