在Excel中使用C#复制/粘贴单元格可以通过以下步骤实现:
using Excel = Microsoft.Office.Interop.Excel;
Excel.Application excelApp = new Excel.Application();
Excel.Workbook workbook = excelApp.Workbooks.Open("路径/文件名.xlsx");
Excel.Worksheet worksheet = workbook.Sheets[1];
Excel.Range sourceRange = worksheet.Range["A1"];
string sourceValue = sourceRange.Value.ToString();
Excel.Range targetRange = worksheet.Range["B1"];
targetRange.Value = sourceValue;
workbook.Save();
workbook.Close();
完整的代码示例:
using Excel = Microsoft.Office.Interop.Excel;
namespace ExcelCopyPasteExample
{
class Program
{
static void Main(string[] args)
{
Excel.Application excelApp = new Excel.Application();
Excel.Workbook workbook = excelApp.Workbooks.Open("路径/文件名.xlsx");
Excel.Worksheet worksheet = workbook.Sheets[1];
Excel.Range sourceRange = worksheet.Range["A1"];
string sourceValue = sourceRange.Value.ToString();
Excel.Range targetRange = worksheet.Range["B1"];
targetRange.Value = sourceValue;
workbook.Save();
workbook.Close();
excelApp.Quit();
}
}
}
这是一个简单的示例,演示了如何使用C#在Excel中复制/粘贴单元格。根据实际需求,你可以进一步扩展和优化代码。
领取专属 10元无门槛券
手把手带您无忧上云