开发者

Paste Special in C# vsto Excel

开发者 https://www.devze.com 2023-02-11 15:01 出处:网络
I am working on the C# vsto Excel application. Whenever user pastes something in the excel template from another excel sheet,it also pastes Cell format along with the cell data in the exc开发者_开发百

I am working on the C# vsto Excel application.

Whenever user pastes something in the excel template from another excel sheet,it also pastes Cell format along with the cell data in the exc开发者_开发百科el template. I want to avoid this. So i googled & i came across term paste special.

Paste special will only paste the contents and will no alter the format of the current sheet.

I want to introduce paste special option in my vsto application.

I have code here,

   Application.OnKey("^v", "PasteSpecV");

but its not working... can any one help me with this ?


  1. Download dll From http://globalmousekeyhook.codeplex.com/
  2. Add Reference MouseKeyboardActivityMonitor.dll

        private KeyboardHookListener k_keyListener;
    
        private void ThisWorkbook_Startup(object sender, System.EventArgs e)
        {
            k_keyListener = new KeyboardHookListener(new AppHooker());
            k_keyListener.Enabled = true;
            k_keyListener.KeyDown += new KeyEventHandler(k_keyListener_KeyDown);
        }
    
        void k_keyListener_KeyDown(object sender, KeyEventArgs e)
        {
            if (Control.ModifierKeys == Keys.Control)
                if (e.KeyCode == Keys.V)
                {
                    Worksheet actSht = ActiveSheet as Worksheet;
                    Range rng = actSht.Application.Selection as Range;
                    if (MessageBox.Show("You are about to paste values only. Do you want to continue?", "Paste Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        rng.PasteSpecial(XlPasteType.xlPasteValues, XlPasteSpecialOperation.xlPasteSpecialOperationNone, false, false);
                    }
                    e.Handled = true;
                }
        }
    


After lots of search and try and error methods,i finally managed to do "Paste Special". The sheet on which i am working , i have delacred as Static Worksheet in the class called commonData

class CommonData
    {
      public static Worksheet DATASHEET;

    }

after that, i used that worksheet in ThisWorkbook.cs

On the ThisWorkbook Start up, i replaced PASTE(^v) by VBA Function Paste_cell

    private void ThisWorkbook_Startup(object sender, System.EventArgs e)
    {
      // replacing paste by macro function Paste_cell
      CommonData.DATASHEET.Application.OnKey("^v", "Paste_cell"); 

    }

Open excel sheet on which you are working, Press ALT + F11 i.e. VBA Macros Editor.

Tools >> Macros >> Create new macro, It will create Module 1 in the Project Explorer, Paste the following code in the module1

Sub Paste_cell()

If MsgBox("You are about to Paste only Values and not the format, proceed?", vbQuestion + vbOKCancel, GSAPPNAME) = vbOK Then
On Error Resume Next

ActiveSheet.PasteSpecial Format:="Text", Link:=False, DisplayAsIcon:=False
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False

End If

End Sub

Now,if you copy paste any value from any excel sheet,It will only paste the cell data and it will not paste its Format.It will prompt following message in order to alert user. So the original Format will not change.

Cheers, :-)


I used the following code to copy-paste only values and formatting using PasteSpecial in VSTO 2010

            Excel.Worksheet lastSheet = Application.ActiveSheet;
            Excel.Workbook wb = Application.Workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);

            for (int i = this.Worksheets.Count; i >= 1; i--)
            {
                Excel.Worksheet source = this.Worksheets[i];
                source.Activate();
                Application.Cells.Select();
                Application.Selection.Copy();

                Excel.Worksheet sheet = wb.Worksheets.Add();
                sheet.Activate();
                Application.Selection.PasteSpecial(Excel.XlPasteType.xlPasteValues);
                Application.Selection.PasteSpecial(Excel.XlPasteType.xlPasteFormats);
                sheet.Name = source.Name;
                sheet.Range["A1"].Select();
                Clipboard.Clear();
            }
            lastSheet.Activate();
            lastSheet.Range["A1"].Select();

            wb.SaveAs(fileName);
            wb.Close();

The line Clipboard.Clear() is the same as VBA CutCopyMode = False, to deselect what was selected to copy.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号