C# Paste Clipboard content into Excel worksheet

19,461

Solution 1

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:

ActiveSheet.Range("A1").PasteSpecial(
         Excel.Enums.XlPasteType.xlPasteAll,
         Excel.Enums.Xl‌​PasteSpecialOperation.xlPasteSpecialOperationNone,
         false, false);

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

Solution 2

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);
            xlApp.ActiveWorkbook.Sheets[1].Activate();
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
            xlWorkSheet.PasteSpecial(Missing.Value, false, false, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
            xlWorkBook.SaveAs(save_file_name);
            Console.WriteLine("saved file name" + save_file_name);
            xlWorkBook.Close();
            xlApp.Quit();

Solution 3

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!

Solution 4

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
        xlRange.Copy(Type.Missing);
        Excel.Range targetRange = mWSheet1.Cells[11, 1];   //initial cell for Paste
        mWSheet1.Paste(targetRange);
// save Output
        mWorkBook.SaveAs(fileNameOut, Excel.XlFileFormat.xlWorkbookNormal, 
                                Missing.Value, Missing.Value,
                                Missing.Value, Missing.Value,  
                                Excel.XlSaveAsAccessMode.xlExclusive,
                                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);
Share:
19,461
mike27015
Author by

mike27015

MSc student in France. Fascinated by Business Analytics and Networking, I like to teach and study in same time. Explore a new area in CS is a requirement not an option! IT Security is supposed to be fun, go and read a book here: http://books.slashdot.org/story/13/04/15/1410216/book-review-the-death-of-the-internet Email: micaelleal890[AT]hotmail[DOT]com Do not anticipate trouble, or worry about what may never happen. Keep in the sunlight. BENJAMIN FRANKLIN

Updated on June 05, 2022

Comments

  • mike27015
    mike27015 almost 2 years

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

    I've following code:

    Clipboard.SetText(html);
    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)
        {
            Clipboard.SetText(html);
    
            //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.

    Thanks.

  • mike27015
    mike27015 about 11 years
    I was thinking about that way, but it still throws me an exception, I'm using NetOffice instead of Microsoft.Office libraries that's why, because I can just do a .PasteSpecial without parameters. I'll wait a reply from admin there to ask if it might be an assembly issue or a wrong implementation. It seems to compile with : sheet.Range("A1").PasteSpecial(Excel.Enums.XlPasteType.xlPas‌​teAll,Excel.Enums.Xl‌​PasteSpecialOperatio‌​n.xlPasteSpecialOper‌​ationAdd,false, false); But still get some exception error.
  • mike27015
    mike27015 about 11 years
    I did it with xlPasteSpecialOperationAdd , because I already have existing values from an template ... And want to do calculations from previous existing values, if I leave it with xlPasteSpecialOperationNone, old values are kept, but still get an exception error. RTFM was read, since you even gave me the right Operation.
  • mike27015
    mike27015 about 11 years
    Just for copying it works with Clipboard.SetText(html); sheet.Range("A1").PasteSpecial(); He doesn't like the Clipboard.SetData(DataFormats.Htlm, html);
  • Doc Brown
    Doc Brown about 11 years
    @mike27015: ok, changed my answer accordingly.