将一张工作簿的一行逐行复制到另一张工作簿的另一张工作表中。
public static void Main()
{
CopyExcelData();
}
public static void CopyExcelData()
{
Excel.Application excelapp = new Excel.Application();
excelapp.Workbooks.Add();
string path = @"E:\test\hello.xlsx";
Excel.Workbook workbook = excelapp.Workbooks.Open(path);
Excel.Worksheet workSheet = workbook.Worksheets.get_Item(i);
workSheet.Range["A2:B2"].Copy(workSheet.Range["A3:B3"]);
workSheet.Range["A3:B3"].Insert(Excel.XlInsertShiftDirection.xlShiftDown);
var source = workSheet.Range["A2:B2"].Insert(Excel.XlInsertShiftDirection.xlShiftDown);
Excel.Range dest = workSheet.Range["A3:B3"];
workSheet.Range["A2:B2"].Copy(dest);
excelapp.ActiveWorkbook.SaveAs("C:\\Users\\my\\Desktop\\test " + DateTime.Now.ToString("MM_dd_yyyy") + ".xlsx");
//excelapp.Visible = false;
excelapp.ActiveWorkbook.Close();
}
我使用了以下代码,但在此我必须指定硬编码的单元格,我想动态地执行。