none
Convert byte-array to object RRS feed

  • Question

  • Hi,

    in my project I use an Excel workbook as template. Right now, this workbook is located in the same directory as the exe-assembly and can be opened and manipulated by the user. To avoid this, I thought about adding the workbook to a ressource. That works. Problem: how can I convert the resulting byte-array/stream back to a workbook-object?

    I tried like this, but - obviously - the conversion fails:

      Public Shared Sub test()
        Dim wkb As Excel.Workbook
        Dim bytes As Byte() = My.Resources.Template_1
        Dim mStream As New System.IO.MemoryStream
        mStream.Write(bytes, 0, bytes.Length)
        wkb = CType(mStream, Excel.Workbook)
      End Sub
    

    Thanks for help and regards!

    Monday, August 16, 2010 7:50 AM

All replies

  • A resource can also not opened and manipulated by the user, what do you want to gain by this approach. 

    (It is easier to deploy but that is another reason).


    Success
    Cor
    Monday, August 16, 2010 7:56 AM
  • Hi Cor,

    the excel template should NOT be able to get manipulated by the user (as written). AFTER the not modfied template was loaded from the ressource-file, it should be manipulated programmatically.

    Regards

    Monday, August 16, 2010 8:01 AM
  • Yes I was misinterpreting it. 

    I came something further but workbooks are one of those things which are not really my special preference.

    I come to this, be aware it stops. I don't know if that has to do that in fact the wks is no Net object.

      Dim bf As New Runtime.Serialization.Formatters.Binary.BinaryFormatter
      Dim mStream As New System.IO.MemoryStream
      mStream.Write(My.Resources.Map1, 0, My.Resources.Map1.Length)
      Dim wkb As Microsoft.Office.Interop.Excel.Workbook = CType(bf.Deserialize(mStream), Microsoft.Office.Interop.Excel.Workbook)

    Sorry, I could also nothing find about the given error that it was passing the end of the stream before it was ready.

     


    Success
    Cor
    Monday, August 16, 2010 9:03 AM