I have the following lines of code:
Dim ms As New MemoryStream(my_memory_stream)
workbook As New Workbook(ms)
workbook.Save("C:\book1.xlsx")
My purpose is to save the stream contained in my_memory_stream into an xlsx file named "book1": the problem is that when I run this code an exception occurr (Invalid Excel2007Xlsx file format).
Does anyone know what I'm doing wrong?
Thanks so much!
Most probably, the problem is the base64 format. It is in the form of ASCII characters. You need to convert the Base64 data to binary. And then load the binary data in Workbook class.
' Decode base64
Dim binaryBytes As Byte() = Convert.FromBase64CharArray(base64Data, 0, base64Data.Length)
' Load in MemoryStream
Dim binaryStream As New MemoryStream(binaryBytes)
' Pass memory stream to Workbook
Dim workbook As New Aspose.Cells.Workbook(my_memory_stream)
workbook.Save(dataDir + "workbook-out.xlsx")
Also note that, if your decoding is correct, you can directly save the binary byte or stream to disk and verify that the Excel is saved correctly.
PS. I am a Developer Evangelist at Aspose.
It seems that you are trying to save a stream directly to a Excel file. Excel files have a fixed format, and the stream will not be adhering to the standard file protocol of excel.
I think you would like to use Workbook object of Microsoft.office.Interop.Excel namespace.
Related
Can someone please give me an explanation to this code, and for your information I am trying to save a picture which is displayed in a picture box and save it to the Microsoft access database, I don't understand what anything means, and especially the 0.
If Not PictureBox1.Image Is Nothing Then
Dim fsreader As New FileStream(OpenFileDialog1.FileName, FileMode.Open, FileAccess.Read)
Dim breader As New BinaryReader(fsreader)
Dim imgbuffer(fsreader.Length) As Byte
breader.Read(imgbuffer, 0, fsreader.Length)
fsreader.Close()
Your snippet code is code to save a binary of picture to imgbuffer variable in VB.Net. This snippet code didn't save this images into Ms Access. But...
I will try to explain it, how this code works :
If Not PictureBox1.Image Is Nothing Then 'This code for checking if there's any images in the picture box, if it's so run next code.
Dim fsreader As New FileStream(OpenFileDialog1.FileName, FileMode.Open, FileAccess.Read) 'This variable declared for getting file name, access file and the read mode. And open it using `Filestream` variable, so after it's opened, the length of images will be saved into `fsreader` variable
Dim breader As New BinaryReader(fsreader) 'This code for reading the binary of images from `fsreader` variable and put the image buffer from range of byte to a variable
Dim imgbuffer(fsreader.Length) As Byte 'This variable is use to collect image buffer with specified length of binary images from `fsreader` variable.
breader.Read(imgbuffer, 0, fsreader.Length) 'This code is use for reading binary of images with specified length of byte and put the image buffer into a imgbuffer variable. The number `0` in the brackets means the binary images will be put fully in imgbuffer. Why `0` ? can I use larger than `0` ? yes you can but the image will be corrupt or differ from the original, so you must read the binary images from 0 to the image file size.
fsreader.Close() 'This is for closing `fsreader` from accessing images file.
So, what's next ?
From that snippet code, you can continued it to process the imgbuffer variable to save it into Ms-Access using some library collection that provided in VB.Net, like OleDB or anything else. At last, I would prefer using MemoryStream to put image buffer and save it into Bitmap variable.
Hope this explain your snippet code.
The below given code is returning corrupted data to the variable 'mystr'
like
PK ! ��fѲ � [Content_Types].xml �(� �UMk�#���^��N%�9�ɱ
It was reading the word files correctly. Suddenly it started happening without any change in code or software versions or source files!! Any word file I try to run through the below code gives the same corrupted output. I'm able to open the file in MsWord without any issue.
Dim myStreamReader As System.IO.StreamReader
Dim myStr As String
myStreamReader = System.IO.File.OpenText("c:\test.docx")
myStr = myStreamReader.ReadToEnd()
myStreamReader.Close()
Any suggestions why it is happening ?
You can't read a Word document using the StreamReader class as the file itself isn't plain text (it contains the data you find 'corrupted').
This link will help you reading text from a Word document.
I've been trying to use StreamReader to read a log file. I cannot verify what it is encoded in, as when I open it in notepad++ and select ANSI encoding, I get this result:
I'm getting the characters needed when using ANSI but they are followed by things like [NULL][EOT][SOH][NUL][SI]
When I try and read the file in VB (using StreamReader or ReadAll) with ANSI encoding selected the resulting string I get back is completely wrong.
How could I read a file like this in VB.net?
You could use the IO.File.ReadAllText("File Location", encoding as System.Text.Encoding) method,
Dim textFromFile as string = IO.File.ReadAllText("C:\Users\Jason\Desktop\login20130417.rdb", System.Text.Encoding.ASCII) 'Or Unicode, UFT32, UFT8, UFT7, BigEndianUnicode or default. Default is ANSI.
If you still don't get the text you need by using the default encoding (ANSI), then you can always try the other 6 different encoding methods.
Update...
It appears that your file is corrupt, using the code below I was able to get a binary representation of whatever is in the file, I got this,
1111111111111101000001110000010000000000000001010000000000010011000000000000100000000000000111100000000000100110000000000011100000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000111111111111110100000111000001000000000000000101000000000001001100000000000010000000000000011110000000000010100000000000111111111111110100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000110011100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
The massive amount of null data would suggest that the file is corrupt, which would also explain why we are not getting a lot of data whenever we try to read the file.
The code,
Dim fileData As String = IO.File.ReadAllText("C:\Users\Jason\Desktop\login20130417.rdb")
Dim i As Integer = 0
Dim binaryData As String = ""
Dim ch As String = ""
Do Until i = fileData.Length
ch = fileData.Chars(i)
bin = bin & System.Convert.ToString(AscW(ch), 2).PadLeft(8, "0")
i = i + 1
Loop
As #Daniel A. White suggested in his comment, that file does not appear to be encoded like a "normal" text file. A StreamReader will not work in this situation. I would attempt to use a BinaryReader.
Rdb file? Never heard of it. Quick google makes it less clear - n64 database file, Darkbot, etc...
However considering the name you have, and the general look of the opened file, i would say its a binary file.
If you want to read the file in vb.net you'll need a library of sorts, and i can't help you with one until you are able to shed some light on what the file may be, or what it was created with.
I am trying to retrieve an email attachment and save it to specific directory in filesystem using the code below.
Dim objMail As Message = New Message(Encoding.ASCII.GetBytes(strMessage))
For Each att In objMail.FindAllAttachments()
Dim Stream as FileStream = New FileStream("D:\XX\" & att.FileName.ToString(),
FileMode.Create)
Dim BinaryStream As New BinaryWriter(Stream)
BinaryStream.Write(BitConverter.ToString(att.Body))
BinaryStream.Close()
Next
I have also tried att.GetBodyasText()
Using this code I am able to save attachment file in desired folder. But while opening a file, I am getting errorL
File is not in proper Format or not Decoded Properly.
I am new to MIME encoding/decoding.
I am a developer of OpenPop.NET.
There are multiple issues with the code you are using to instantiate the Message class:
Where is the strMessage contents comming from?
How do you know it is encoded in only ASCII?
These are two major issues that will likely make a big difference.
You should NOT be using a string to contain the message, instead you should be using raw bytes!
For example (in C#):
byte[] byteMessage = someFileStream.ReadToEnd();
Message message = new Message(byteMessage);
In this way, you will not destroy the message by using a wrong encoding on the bytes. Typically the email will include a header which tells how to decode to bytes to a string, which is what the OpenPop Message class will do for you.
Now let me explain attachments. Attachments are typically raw bytes, for example a PNG picture is some bytes that a PNG picture reader will understand. For the PNG picture reader to understand the picture, the attachments raw bytes must be saved to a file. You can get the raw bytes using att.Body.
There are also attachments where the raw bytes does not make sense - for example a text attachment encoded in BASE64 is not very useful for a text reader program, and such an attachment must be converted to text before saved. You can get the text using att.GetBodyAsText().
What you are doing is taking the raw bytes for the attachment and then using a BitConverter to convert it into hexadecimal numbers - which I cannot make any meaning of. Instead, you should change your:
BinaryStream.Write(BitConverter.ToString(att.Body))
to
BinaryStream.Write(att.Body)
in case your attachment is a picture or some more complex file.
I hope this can help with your problem.
I have a picture box, and if I use snipet below:
Clipboard.SetImage(PictureBox.image)
Then I can only paste the image into things like Paint and MS word. I can't paste it as a file into a folder/desktop.
So how can I copy the image to the clipboard and if gets pasted to a folder then it becomes a file?
If you're using .net and your ultimate goal is to save the file, there's a LOT easier way,
Here the code in C#, porting it into VB.net won't be hard, I'm just too lazy to do that :)
Anyway, you do have to save it somewhere before you can paste it so...
It loads the file to the Picture box and again saves it to a file, (lame, I know)
and set the clipboard data as a copy operation
then when you paste (Ctrl+V) it, it gets pasted.
C#
__
Bitmap bmp;
string fileName=#"C:\image.bmp";
//here I assume you load it from a file, you might get the image from somewhere else, your code may differ
pictureBox1.Image=(Image) Bitmap.FromFile(fileName);
bmp=(Bitmap)pictureBox1.Image;
bmp.Save(#"c:\image2.bmp");
System.Collections.Specialized.StringCollection st = new
System.Collections.Specialized.StringCollection();
st.Add(#"c:\image2.bmp");
System.Windows.Forms.Clipboard.SetFileDropList(st);
</pre>
and viola tries pasting in a folder the file image2.bmp will be pasted.
Here's basically what #Vivek posted but ported to VB. Up-vote his if this works for you. What you have to understand is that explorer will only allow you to paste files, not objects (AFAIK anyway). The reason is because if you copy image data to the clipboard, what format should it paste in? PNG, BMP, JPG? What compression settings? So like #Vivek said, you need to think those over, create a file on your own somewhere on the system and use SetFileDropList which will add the temp file to the clipboard.
' Add it as an image
Clipboard.SetImage(PictureBox1.Image)
'Create a JPG on disk and add the location to the clipboard
Dim TempName As String = "TempName.jpg"
Dim TempPath As String = System.IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.Temp, TempName)
Using FS As New System.IO.FileStream(TempPath, IO.FileMode.Create, IO.FileAccess.Write, IO.FileShare.Read)
PictureBox1.Image.Save(FS, System.Drawing.Imaging.ImageFormat.Jpeg)
End Using
Dim Paths As New System.Collections.Specialized.StringCollection()
Paths.Add(TempPath)
Clipboard.SetFileDropList(Paths)