0

我正在尝试使用 pastespecial 方法将格式化的单元格从现有的 excel 工作表复制到 vb.net(2010) 中的新工作表,但它将单元格值粘贴为图片而不是文本。在这方面的任何帮助将不胜感激:

    Public Sub set_header(ByVal r As Integer, ByVal c As Integer, ByVal dt1 As Date, ByVal dt2 As Date, ByVal sheetname As String)

    Dim wbCopy As Excel.Workbook
    Dim wsCopy As Excel.Worksheet
    Dim rngCopy As Excel.Range
    Dim wbPaste As Excel.Workbook
    Dim wsPaste As Excel.Worksheet
    Dim rngPaste As Excel.Range
    Dim xlsApp As Excel.Application
    xlsApp = New Excel.Application
    wbCopy = xlsApp.Workbooks.Open("C:\Windows\rta2.xlsx") 
    wsCopy = wbCopy.Worksheets(sheetname)
    rngCopy = wsCopy.Range("A7:O9")
    Dim addr As Object
    addr = ExcelSheet.Cells(r, c).Address(False, False)
    rngPaste = ExcelSheet.Range(addr) 
    rngCopy.Copy()
    rngPaste.PasteSpecial(Excel.XlPasteType.xlPasteAll, Excel.XlPasteSpecialOperation.xlPasteSpecialOperationNone,false,false)
    ExcelSheet.Cells(r + 2, c + 4) = "from " & dt1 & " to " & dt2
    Set wbCopy = Nothing
    Set wsCopy = Nothing
    Set rngCopy = Nothing
    Set wbPaste = Nothing
    Set wsPaste = Nothing
    Set rngPaste = Nothing
    ExcelApp.Quit
   End sub

谢谢

4

2 回答 2

0

我也有同样的问题。我首先检查剪贴板的内容是否为 excel 格式。如果是,我将使用 Excel 的 PasteSpecial 作为值 如果不是,我将使用 MSForms.DataObject 从剪贴板粘贴值

下面是我的代码,你可以看看它是否解决了问题:

' Code Block to prevent pasting Images with PasteSpecial
If IsClipboardContentInExcelFormat Then
    Selection.PasteSpecial Paste:=xlValues
Else
    Selection.Value = GetClipBoardText           
End If    


Function GetClipBoardText()
   ' src: http://stackoverflow.com/questions/9022245/get-text-from-clipboard-using-gettext-avoid-error-on-empty-clipboard
   Dim DataObj As MSForms.DataObject
   Set DataObj = New MSForms.DataObject '<~~ Amended as per jp's suggestion
   Dim myString As String

   On Error GoTo ErrorHandling

   DataObj.GetFromClipboard '~~> Get data from the clipboard.
   myString = DataObj.GetText(1)
   GetClipBoardText = myString

   Exit Function
ErrorHandling:
   If Err <> 0 Then MsgBox "Data on clipboard is not text or is empty"
End Function


' Will use MSForms.DataObject for geting clipboard content
' reference: http://stackoverflow.com/questions/9022245/get-text-from-clipboard-using-gettext-avoid-error-on-empty-clipboard
' reference: http://msdn.microsoft.com/en-us/library/office/ff839748%28v=office.14%29.aspx
Private Function IsClipboardContentInExcelFormat() As Boolean
    IsClipboardContentInExcelFormat = False

    On Error GoTo ErrorHandling

    Dim aFmts, fmt
    aFmts = Application.ClipboardFormats

    For Each fmt In aFmts

        If (fmt = xlClipboardFormatBIFF _
             Or fmt = xlClipboardFormatBIFF12 _
             Or fmt = xlClipboardFormatBIFF2 _
             Or fmt = xlClipboardFormatBIFF3 _
             Or fmt = xlClipboardFormatBIFF4 _
             ) Then
            IsClipboardContentInExcelFormat = True
            Exit Function
        End If
    Next

    Exit Function

ErrorHandling:
    If Err <> 0 Then MsgBox "Cannot Read Data in Clipboard"

End Function    
于 2017-06-09T02:41:37.710 回答
0

对于像我这样用谷歌搜索过这个问题的人。

当我粘贴我的值时,它也作为图像显示在新的工作簿中。我发现我可以通过Excel.XlPasteType.xlPasteValues控制粘贴的格式

Dim TestRange = SourceExcelSheet.Range("B4:I4")
    TestRange.Copy()
    TargetExcelSheet.Range("B4").PasteSpecial(Excel.XlPasteType.xlPasteValues)
于 2022-01-25T15:55:57.470 回答