none
What format to write a Stringbuilder and images to a file RRS feed

  • Question

  • Hello everyone,

    I am currently  using the code below to write a  StringBuilder to a csv file and it works fine because I only has text data.

    But as part of an enhancement the resulting files must also have images and text.

    Csv files do not allow to add  images, so I would like to know how I can create a fie that can be opened by a user when receiving this file as an attachment  and contains text and images.

    Thanks

     

    Sub ExportCSV(strHeader As String, ReportData As StringBuilder, strProjectID As String)

                Dim strRelFilename As String
                strRelFilename = "C:\Workspace\scratchfolder\" + strProjectID + ".csv"
                Try
                    If File.Exists(strRelFilename) Then
                        File.Delete(strRelFilename)
                    End If

                    Dim sw As New StreamWriter(strRelFilename)
                    Dim s As String = String.Empty

                    sw.Write(strHeader)
                    sw.Write(vbNewLine)
                    sw.Write(ReportData.ToString)
                    sw.Close()

                Catch ex As Exception
                    errorMsgs = "ExportCSV " & vbNewLine _
                               & "Error Message: " & ex.Message _
                               & "Date" & DateTime.Now.ToShortDateString
                    Console.WriteLine(ex.Message)
                End Try
      End Sub

    Wednesday, November 27, 2019 1:01 PM

All replies

  • You would need to structure the file as a known file type that permits images e.g. a RTF document or a Word document for instance.

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Wednesday, November 27, 2019 1:25 PM
    Moderator
  • An XML file.

    But first the image has to be serialized. 

    A very easy way is to use a DataSet and write that as 

    TheDataSet.WriteXML(Path)


    Success
    Cor

    Wednesday, November 27, 2019 1:43 PM
  • Hi

    Here is one way. Using this, the recipient needs only Microsoft WordPad on their computer.

    A setup declaration is used to set the FOLDER and FILENAME (extension will be added automatically).

    	' set EXPORT ROOT FOLDER & NAME 
    	Dim ExportRoot As String = IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.Desktop, "FILENAME")
    

    then the export sub which will add the ext3nsion and dare/time stamp and then save. Here I use a Button,but the ExportRTF sub could be called directly from anyn point.

    	Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
    		' EXPORT BUTTON
    		ExportRTF("Freddy")
    	End Sub
    	Sub ExportRTF(strProjectID As String)
    		' EXPORT RTF TO FILE
    		Dim ExportPath As String = ExportRoot & " " & Now.ToString("dd-MMM-yyyy-HH-mm-ss") & ".rtf"
    
    		RichTextBox1.SaveFile(ExportPath, RichTextBoxStreamType.RichText)
    	End Sub


    Regards Les, Livingston, Scotland

    Wednesday, November 27, 2019 2:03 PM
  • how I can create a fie that can be opened by a user when receiving this file as an attachment and contains text and images.

    Probably you do not want to store the image in the CSV. I think you will get better answers if you can be more specific. You say attachment so I assume you are sending the data as an email message. If you want to send a message that is convenient for the recipient to see then you will want to format it as HTML. That is the format that most email messages are in, except for plain-text that has no images and no formatting. So you need to determine what would be the preferred format in terms of what the recipient will see; not the technical specification, the format stated in terms that the non-technical person would understand.

    Images are typically sent in base-64 format, but you probably do not need to understand that much. You probably just need to know how to attach images to messages.

    The MSDN search feature has been improved and is now useless. I used Google to find the following.



    Sam Hobbs
    SimpleSamples.Info

    Thursday, November 28, 2019 9:26 PM