Home » excel » C# Paste Clipboard content into Excel worksheet

C# Paste Clipboard content into Excel worksheet

Posted by: admin April 23, 2020 Leave a comment


As the title says, I try to actually paste what is in my Clipboard into an Excel.

I’ve following code:

sheet.Range("A1").Value = Clipboard.GetText(); 

Actually, the variable html contains a html code file, when I do it like that, I actually paste only the html content into the Range, but, if I open Excel and do by hand, Paste Special… I can paste the html code, but it transforms the code into a real table instead of a html code and this is the real result that I want, without doing it by hand.

Excel.Range.Copy() paste with Clipboard.GetText()

Another way was:

foreach (Excel.Worksheet sheet in workbook.Sheets)
    foreach (Excel.Shape shape in sheet.Shapes)

        //doesn't work:                              
        sheet.Range("A1").Value = sheet.PasteSpecial(Clipboard.GetText()); 

        sheet.PasteSpecial(Clipboard.GetText()); //throws error

But this way doesn’t work too. I can do it with an html -> image and paste the image, but real values should be accessible and not a picture.

Hope someone can clarify how to solve it.


How to&Answers:

You may try to use SetData instead of SetText:

 Clipboard.SetData(DataFormats.Html, html);

to copy the string into the clipboard and tag it as HTML (if this does not work in your case, SetTextmay be ok).

The call to PasteSpecial for the cell range where you want the insert to happen, taking regard to your comments:

         false, false);

Note that assigning a cell a new value by using the Value property never copies any formats etc.


my simple C# Copy & Paste ( from Input/Sheet[1] to Output/Sheet[1] )

using System.Reflection;
using System.Runtime.InteropServices;
using Excel = Microsoft.Office.Interop.Excel;

// prepare Input
        Excel.Application xlApp = new Excel.Application();
        xlApp.DisplayAlerts = false;
        Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(fileNameIn);
        Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];
        Excel.Range xlRange = xlWorksheet.UsedRange;      //I copy everything
// prepare Output
        Excel.Application oXL = new Excel.Application();
        oXL.DisplayAlerts = false;
        Excel.Workbook mWorkBook = oXL.Workbooks.Open(fileNameOut, 0, false, 5,
                                "", "", false, Excel.XlPlatform.xlWindows,
                                "", true, false, 0, true, false, false);
        Excel.Worksheet mWSheet1 = mWorkBook.Sheets[1];
// make Copy&Paste in PC memory
        Excel.Range targetRange = mWSheet1.Cells[11, 1];   //initial cell for Paste
// save Output
        mWorkBook.SaveAs(fileNameOut, Excel.XlFileFormat.xlWorkbookNormal, 
                                Missing.Value, Missing.Value,
                                Missing.Value, Missing.Value,  
                                Missing.Value, Missing.Value,
                                Missing.Value, Missing.Value, Missing.Value);
// clean the waste !
        mWorkBook.Close(Missing.Value, Missing.Value, Missing.Value);
        mWSheet1 = null; mWorkBook = null; oXL.Quit();
        GC.WaitForPendingFinalizers(); GC.Collect(); GC.WaitForPendingFinalizers();
        GC.Collect(); GC.WaitForPendingFinalizers();
        Marshal.ReleaseComObject(xlRange); Marshal.ReleaseComObject(xlWorksheet);
        xlWorkbook.Close(); Marshal.ReleaseComObject(xlWorkbook);
        xlApp.Quit(); Marshal.ReleaseComObject(xlApp);


I managed to get the copied cells from the Clipboard by using

    Clipboard.GetData("XML Spreadsheet");

Then, after I did some copy-pasting through my code, I put it back with

    Clipboard.SetData("XML Spreadsheet", originalObject);

Happy coding!


Use this:
xlWorkSheet.PasteSpecial(Missing.Value, false, false, Missing.Value, Missing.Value, Missing.Value, Missing.Value);

//below is the complete code

            Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
            Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;

            Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
            xlApp.Visible = true;
            xlApp.UserControl = true;
            xlApp.WindowState = Excel.XlWindowState.xlMaximized;
            xlWorkBook = xlApp.Workbooks.Add(Type.Missing);
           // xlWorkBook = xlApp.Workbooks.Open(excel_filename);
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
            xlWorkSheet.PasteSpecial(Missing.Value, false, false, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
            Console.WriteLine("saved file name" + save_file_name);


this code is for dump html string into the excel

using Microsoft.Office.Interop.Excel;

private void ExcelExport()
    var excel = new Application { Visible = true };
    excel.WindowState = XlWindowState.xlMaximized;
    Workbook workbook = excel.Workbooks.Add(XlSheetType.xlWorksheet);
    Worksheet sheet = workbook.Sheets[1];

    string s = "<html><body><table>";

    for (int i = 1; i <= 100; i++)
        s += "<tr>";

        for (int f = 1; f <= 100; f++)
            s += "<td>" + i.ToString() + "," + f.ToString() + "</td>";

        s += "</tr>";

    s += "</table></body></html>";


    sheet.PasteSpecial(Type.Missing, false, false, Type.Missing, Type.Missing, Type.Missing, Type.Missing);