Hi @牛肉粒 胖胖的 , Welcome to Microsoft Q&A.
I tested the code below without issue.
By passing Excel.XlPasteSpecialOperation.xlPasteSpecialOperationNone as the second parameter to the PasteSpecial method, you can ensure that no operation is performed and only the content is pasted
using Excel = Microsoft.Office.Interop.Excel;
class Program
{
static void Main(string[] args)
{
Excel.Application excel = new Excel.Application();
Excel.Workbook wb_to_copy = excel.Workbooks.Open(@"path/to/source/workbook.xlsx");
Excel.Workbook wb_to_paste = excel.Workbooks.Open(@"path/to/destination/workbook.xlsx");
Excel.Worksheet ws_to_copy = (Excel.Worksheet)wb_to_copy.Worksheets.get_Item(1);
Excel.Worksheet ws_to_paste = (Excel.Worksheet)wb_to_paste.Worksheets.get_Item(1);
int rows1 = ws_to_copy.UsedRange.Cells.Rows.Count;
int cols1 = ws_to_copy.UsedRange.Cells.Columns.Count;
Excel.Range rg1 = ws_to_copy.Range[ws_to_copy.Cells[1, 1], ws_to_copy.Cells[rows1, cols1]];
rg1.Copy();
Excel.Range rg2 = ws_to_paste.Range[ws_to_paste.Cells[1, 1], ws_to_paste.Cells[rows1, cols1]];
rg2.PasteSpecial(Excel.XlPasteType.xlPasteAll, Excel.XlPasteSpecialOperation.xlPasteSpecialOperationNone);
wb_to_paste.Save();
wb_to_paste.Close();
wb_to_copy.Close();
excel.Quit();
}
}
Best Regards,
Jiale
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.