Open a spreadsheet document for read-only access

  • OpenXML SDK
  • Spire.XLS
  • Download Sample Code

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.
            }
        }
    }

class Program
    {
        static void Main(string[] args)
        {
            OpenSpreadsheetDocumentReadonly(@"..\..\Documetns\Sheet8.xlsx");
        }
        public static void OpenSpreadsheetDocumentReadonly(string filepath)
        {
            //Initialize a new Workboook
            Workbook workbook = new Workbook();

            //Load workbook from file path
            workbook.LoadFromFile(filepath);

        }
    }