0

How do I copy an array of strings to the clipboard to paste to OpenOffice or LibreOffice?

This CSV version works for Excel, but not for OpenOffice or LibreOffice.

Input example = "A" "B" "C" "D" "E" "F" "G" "H" "I" "J"

Desired output (next row after every 8 columns)

Here is my current code:

 Private Sub SaveToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SaveToolStripMenuItem.Click

    ' This returns array of strings for csv
    Dim csvparts As String() = RichTextBox1.Text.Split(ControlChars.CrLf.ToCharArray(), StringSplitOptions.RemoveEmptyEntries)

    Dim csv As String = ""

    Dim rowCounter As Integer = 0
    For Each part As String In csvparts

        csv += ("""")

        For x As Integer = 0 To spinbox.SelectedIndex
            csv += (part)
            If spinbox.SelectedIndex > x Then csv += (vbLf)
        Next
        csv += ("""")

        rowCounter += 1

        'Next row after every 8th column
        If rowCounter > 7 Then
            rowCounter = 0
            csv += (vbLf)
        Else
            csv += (",")
        End If
    Next

    Dim dataObject = New System.Windows.DataObject()

    Dim bytes = System.Text.Encoding.UTF8.GetBytes(csv)
    Dim stream = New System.IO.MemoryStream(bytes)
    dataObject.SetData(System.Windows.DataFormats.CommaSeparatedValue, stream)

    System.Windows.Clipboard.SetDataObject(dataObject, True)
End Sub
1
  • csv is csv. If it works in excel it should work in openoffice. Can you show the csv file that you create. Commented Feb 12, 2014 at 2:14

1 Answer 1

1

Ok I got it working. Save your csv as Unicode Text to the clipboard. I did csv(for Excel) and UnicodeText(for Open/LibreOffice)

    Dim dataObject = New DataObject()

    Dim cbytes = System.Text.Encoding.UTF8.GetBytes(csv)
    Dim cstream = New System.IO.MemoryStream(cbytes)
    dataObject.SetData(DataFormats.CommaSeparatedValue, cstream)

    Dim bytes = System.Text.Encoding.Unicode.GetBytes(csv)
    Dim stream = New System.IO.MemoryStream(bytes)
    dataObject.SetData(DataFormats.UnicodeText, stream)

    Clipboard.Clear()

    Clipboard.SetDataObject(dataObject, True)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.