使用Microsoft.Office.Interop.Excel创建包含太多工作表的xls文件

时间:2012-03-22 07:34:13

标签: c# excel office-interop worksheet-function

如何只创建一张纸?我所做的就是:

public static void CreateExcel(string year)
{
    Application xlApp = new Application();
    Workbook xlWorkBook;
    Worksheet xlWorkSheet;
    object misValue = System.Reflection.Missing.Value;

    xlWorkBook = xlApp.Workbooks.Add(misValue);
    xlWorkSheet = (Worksheet)xlWorkBook.Worksheets.Item[1];
    xlWorkSheet.Name = year;
    xlWorkSheet.Cells[1, 1] = "Share";
    xlWorkSheet.Cells[1, 2] = "Q1";
    xlWorkSheet.Cells[1, 3] = "Q2";
    xlWorkSheet.Cells[1, 4] = "Q3";
    xlWorkSheet.Cells[1, 5] = "Q4";

    xlWorkBook.SaveAs(@"H:\\QResults.xls", XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
    xlWorkBook.Close(true, misValue, misValue);
    xlApp.Quit();

    ReleaseObject(xlWorkSheet);
    ReleaseObject(xlWorkBook);
    ReleaseObject(xlApp);
}

它会创建一个包含3张纸的xls文件,第一张纸名为。

1 个答案:

答案 0 :(得分:2)

新创建的工作簿将始终包含3个工作表。您可以使用索引和工作簿对象以编程方式删除其他2。