0

如何将字符串数组复制到剪贴板以粘贴到 OpenOffice 或 LibreOffice?

此 CSV 版本适用于 Excel,但不适用于 OpenOffice 或 LibreOffice。

输入示例=“A”“B”“C”“D”“E”“F”“G”“H”“I”“J”

所需输出(每 8 列后的下一行)

这是我当前的代码:

 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
4

1 回答 1

1

好的,我让它工作了。将您的 csv 作为 Unicode 文本保存到剪贴板。我做了 csv(用于 Excel)和 UnicodeText(用于 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)
于 2014-02-12T16:43:20.793 回答