Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Locking excel file while writing

Locking excel file while writing

Scheduled Pinned Locked Moved C#
tutorial
8 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    KaurGurpreet
    wrote on last edited by
    #1

    Hi all, I need to know how to either prevent anyone from open an excel file when it's open by my application and written to or only let the file being opened write-protected during the write session.

    Gurpreet

    A R 2 Replies Last reply
    0
    • K KaurGurpreet

      Hi all, I need to know how to either prevent anyone from open an excel file when it's open by my application and written to or only let the file being opened write-protected during the write session.

      Gurpreet

      A Offline
      A Offline
      Abhinav S
      wrote on last edited by
      #2

      You might want to look at sharing and the MultiUserEditing[^] flag. Maybe this[^] can also help.

      My signature "sucks" today

      K 1 Reply Last reply
      0
      • A Abhinav S

        You might want to look at sharing and the MultiUserEditing[^] flag. Maybe this[^] can also help.

        My signature "sucks" today

        K Offline
        K Offline
        KaurGurpreet
        wrote on last edited by
        #3

        I need to support Office 2005 as well so MultiUserEditing flag will not help. also MultiUserEditing is readonly property so I cannot set it :( Also, protection property will not help in this case :(

        Gurpreet

        1 Reply Last reply
        0
        • K KaurGurpreet

          Hi all, I need to know how to either prevent anyone from open an excel file when it's open by my application and written to or only let the file being opened write-protected during the write session.

          Gurpreet

          R Offline
          R Offline
          Rhys Jacob
          wrote on last edited by
          #4

          If you are opening the excel file using a filestream you could use the fileshare parameter to prevent any other users from reading the file.

          K 1 Reply Last reply
          0
          • R Rhys Jacob

            If you are opening the excel file using a filestream you could use the fileshare parameter to prevent any other users from reading the file.

            K Offline
            K Offline
            KaurGurpreet
            wrote on last edited by
            #5

            No I am not using file stream. This is how I open and use _objAppln = new Excel.Application(); // To initialize excel file //_objAppln.Visible = true; if (_objAppln != null) { _objWorkBooks = _objAppln.Workbooks; _objWorkBook = _objWorkBooks.Add(Type.Missing); // To add workbook with sheets in excel file _objWorkSheet = (Excel.Worksheet)_objAppln.ActiveSheet; // To get the current active sheet in excel file .... Excel.Range objRange = _objWorkSheet.get_Range(columnName + _currentRowIndex, LastColumnName + _currentRowIndex); objRange.Value2 = GroupName; ... _objWorkBook.SaveAs(_fileName, Excel.XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, false, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

            Gurpreet

            P 1 Reply Last reply
            0
            • K KaurGurpreet

              No I am not using file stream. This is how I open and use _objAppln = new Excel.Application(); // To initialize excel file //_objAppln.Visible = true; if (_objAppln != null) { _objWorkBooks = _objAppln.Workbooks; _objWorkBook = _objWorkBooks.Add(Type.Missing); // To add workbook with sheets in excel file _objWorkSheet = (Excel.Worksheet)_objAppln.ActiveSheet; // To get the current active sheet in excel file .... Excel.Range objRange = _objWorkSheet.get_Range(columnName + _currentRowIndex, LastColumnName + _currentRowIndex); objRange.Value2 = GroupName; ... _objWorkBook.SaveAs(_fileName, Excel.XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, false, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

              Gurpreet

              P Offline
              P Offline
              Pete OHanlon
              wrote on last edited by
              #6

              Potentially you could change the read only attribute on the file as soon as you open it, and manage it that way (i.e. convert it back to read/write for the file save). This isn't perfect, though, because it would be in line for a race condition occurring.

              "WPF has many lovers. It's a veritable porn star!" - Josh Smith

              As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

              My blog | My articles | MoXAML PowerToys | Onyx

              K 1 Reply Last reply
              0
              • P Pete OHanlon

                Potentially you could change the read only attribute on the file as soon as you open it, and manage it that way (i.e. convert it back to read/write for the file save). This isn't perfect, though, because it would be in line for a race condition occurring.

                "WPF has many lovers. It's a veritable porn star!" - Josh Smith

                As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

                My blog | My articles | MoXAML PowerToys | Onyx

                K Offline
                K Offline
                KaurGurpreet
                wrote on last edited by
                #7

                I need to add rows to that sheet. How can I change it to readonly?

                Gurpreet

                P 1 Reply Last reply
                0
                • K KaurGurpreet

                  I need to add rows to that sheet. How can I change it to readonly?

                  Gurpreet

                  P Offline
                  P Offline
                  Pete OHanlon
                  wrote on last edited by
                  #8

                  As soon as you open the file, change the readonly file attribute to true. Add your rows, and change the attribute to false - save - then reset to true.

                  "WPF has many lovers. It's a veritable porn star!" - Josh Smith

                  As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

                  My blog | My articles | MoXAML PowerToys | Onyx

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups