简体   繁体   English

在C#vsto Excel中粘贴特殊功能

[英]Paste Special in C# vsto Excel

I am working on the C# vsto Excel application. 我正在使用C#vsto Excel应用程序。

Whenever user pastes something in the excel template from another excel sheet,it also pastes Cell format along with the cell data in the excel template. 每当用户从另一个Excel工作表中粘贴excel模板中的内容时,它还会将单元格格式与单元格数据一起粘贴到Excel模板中。 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. Paste special将仅粘贴内容,不会改变当前工作表的格式。

I want to introduce paste special option in my vsto application. 我想在我的vsto应用程序中引入粘贴特殊选项。

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/ http://globalmousekeyhook.codeplex.com/下载dll
  2. Add Reference MouseKeyboardActivityMonitor.dll 添加参考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 我正在工作的工作表,我已经在名为commonData的类中删除了静态工作表

class CommonData
    {
      public static Worksheet DATASHEET;

    }

after that, i used that worksheet in ThisWorkbook.cs 之后,我在ThisWorkbook.cs中使用了该工作表

On the ThisWorkbook Start up, i replaced PASTE(^v) by VBA Function Paste_cell ThisWorkbook Start up上,我用VBA Function Paste_cell替换了PASTE(^ v)

    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 ie VBA Macros Editor. 打开您正在使用的Excel工作表, 按ALT + F11即VBA宏编辑器。

Tools >> Macros >> Create new macro , It will create Module 1 in the Project Explorer, Paste the following code in the module1 工具>>宏>>创建新宏 ,它将在Project Explorer中创建模块1,将以下代码粘贴到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. 现在,如果您复制粘贴任何Excel工作表中的任何值,它将只粘贴单元格数据,并且不会粘贴其格式。它将提示以下消息以提醒用户。 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 我使用以下代码在VSTO 2010中使用PasteSpecial仅复制粘贴值和格式

            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. Clipboard.Clear()行与VBA CutCopyMode = False相同,用于取消选择要复制的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM