PasteSpecial 函数不复制日期 - VSTO

PasteSpecial function not copying date - VSTO

我正在开发一个 VSTO 插件,我试图将一个 sheet 的值复制到另一个,但仅粘贴为文本。在excel中,我可以手动打开一个新的sheet,将单元格设置为文本格式,然后将数据复制过来。我正在尝试以编程方式执行此操作。这是一些代码:

    Worksheet currentSheet; // This is the sheet I'm copying from
    Excel.Range currentRange = currentSheet.Cells.Application.Selection.SpecialCells(
                                 Excel.XlCellType.xlCellTypeVisible,
                                 Type.Missing);

    Excel.Worksheet newWorksheet; // This is the sheet I'm copyng to
    newWorksheet.Cells.NumberFormat = "@"; // set the format of cells to text

    currentRange.Copy(); // Copy the data
    newWorksheet.Range["A1"].PasteSpecial(XlPasteType.xlPasteValues,
    XlPasteSpecialOperation.xlPasteSpecialOperationNone,
    System.Type.Missing,
    System.Type.Missing);

这没问题,但是,有时我从中复制的数据会包含日期(例如 2019-10-01),当我将其复制到新的 sheet 时,它最终会变成数字,即使我使用 XlPasteType.xlPasteValues 我只希望将文本值带到新单元格中。这可能吗? 谢谢

此处的快速测试表明 PasteSpecial(XlPasteType.xlPasteValuesAndNumberFormats) 保留了单元格的 text 格式。

或者,在粘贴后应用格式

newWorksheet.Range["A1"].PasteSpecial(XlPasteType.xlPasteValues,
  XlPasteSpecialOperation.xlPasteSpecialOperationNone,
  System.Type.Missing,
  System.Type.Missing);
newWorksheet.Cells.NumberFormat = "@"; 

FWIW 我没有看到日期格式明确地变成了数字格式。我看到单元格格式已经应用于目标单元格。就我而言,它是 "general".