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