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. Wrting bytes to a file(xls)

Wrting bytes to a file(xls)

Scheduled Pinned Locked Moved C#
help
4 Posts 3 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.
  • G Offline
    G Offline
    gauthee
    wrote on last edited by
    #1

    Hi, I have been writing bytes to a file as per requirement. It works fine when writing to files of type doc and ppt however error occurs when trying to write a file of xls type It throws an error stating the file is read-only, cannot access...... The sample code is as follows: FileStream fs; fs = File.Open(@"C:\Book1.xls", FileMode.Open, FileAccess.Read); long nBytes=fs.Length - 1; Byte[] b = new Byte[fs.Length - 1]; fs.Read(b, 0, (int)nBytes); fs.Close(); fs = null; FileStream fs1; fs1 = File.Open(@"C:\Dummy.xls", FileMode.Create, FileAccess.Write,FileShare.ReadWrite); fs1.Write(b, 0, b.Length); fs1.Close(); fs1=null;

    gauthee

    J L 2 Replies Last reply
    0
    • G gauthee

      Hi, I have been writing bytes to a file as per requirement. It works fine when writing to files of type doc and ppt however error occurs when trying to write a file of xls type It throws an error stating the file is read-only, cannot access...... The sample code is as follows: FileStream fs; fs = File.Open(@"C:\Book1.xls", FileMode.Open, FileAccess.Read); long nBytes=fs.Length - 1; Byte[] b = new Byte[fs.Length - 1]; fs.Read(b, 0, (int)nBytes); fs.Close(); fs = null; FileStream fs1; fs1 = File.Open(@"C:\Dummy.xls", FileMode.Create, FileAccess.Write,FileShare.ReadWrite); fs1.Write(b, 0, b.Length); fs1.Close(); fs1=null;

      gauthee

      J Offline
      J Offline
      Justin Perez
      wrote on last edited by
      #2

      at which line are you getting the error?

      I get all the news I need from the weather report - Paul Simon (from "The Only Living Boy in New York")

      G 1 Reply Last reply
      0
      • J Justin Perez

        at which line are you getting the error?

        I get all the news I need from the weather report - Paul Simon (from "The Only Living Boy in New York")

        G Offline
        G Offline
        gauthee
        wrote on last edited by
        #3

        Justin, There is no error in the code written however when trying to open the new xls file created i get the follwing pop-up: 'Dummy.xls'cannot be accessed. The file may be read-only, or you may be trying to access a read only location. Or, the server the document is stored on may not be responding. Why is this happening? Thanks,

        gauthee

        1 Reply Last reply
        0
        • G gauthee

          Hi, I have been writing bytes to a file as per requirement. It works fine when writing to files of type doc and ppt however error occurs when trying to write a file of xls type It throws an error stating the file is read-only, cannot access...... The sample code is as follows: FileStream fs; fs = File.Open(@"C:\Book1.xls", FileMode.Open, FileAccess.Read); long nBytes=fs.Length - 1; Byte[] b = new Byte[fs.Length - 1]; fs.Read(b, 0, (int)nBytes); fs.Close(); fs = null; FileStream fs1; fs1 = File.Open(@"C:\Dummy.xls", FileMode.Create, FileAccess.Write,FileShare.ReadWrite); fs1.Write(b, 0, b.Length); fs1.Close(); fs1=null;

          gauthee

          L Offline
          L Offline
          lmoelleb
          wrote on last edited by
          #4

          If your XSL file is for example 400 bytes long, your read operation will read somewhere between 1 and 399 bytes (both extremes included). It can never 400 as you tell it to read fs.Length - 1 It is not even certain it will read 399 bytes, as any stream can choose to chunk any way it likes. The only thing you know for sure is that it will return minimum 1 byte (as long end of file has not been reached) and maximum the number you specify. Either loop the read (you get the number of bytes read in the return value), or use a BinaryReader which offer the possibility to read a fixed number of bytes without any chunking (it will simply do the looping for you).

          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