将工作表添加到当前工作簿

时间:2017-02-13 16:18:59

标签: c# excel c#-4.0

下面的语法将工作表添加到新工作簿中,如何添加到同一工作簿?

private void AddWorksheetsAndCopyData()
{
    xlApp = new Excel.Application();
    xlApp.Visible = true;
    Excel.Worksheet addedSheet;
    string filePath = @"C:\sample.xlsx";
    xlWorkBook = xlApp.Workbooks.Open(filePath);

    //checking if cell value is null - if it is not adding worksheet to copy data to

    xlWorkBook = xlApp.Workbooks.Add(misValue);
    addedSheet = (Excel.Worksheet)xlWorkBook.Worksheets.Add();

    range.Copy(newSeet.Range["A1"]);
}

2 个答案:

答案 0 :(得分:0)

尝试

xlWorkBook.Sheets.Add(After: xlWorkBook.Sheets[xlWorkBook.Sheets.Count]); 

答案 1 :(得分:0)

Excel.Worksheet newWorksheet; newWorksheet = (Excel.Worksheet)Globals.this.Application.ActiveWorkbook.Worksheets.Add(); newWorksheet.Name = "新工作表";