【问题标题】:Creating xls file with too many sheets using Microsoft.Office.Interop.Excel使用 Microsoft.Office.Interop.Excel 创建包含太多工作表的 xls 文件
【发布时间】:2012-03-22 07:34:12
【问题描述】:

如何只创建一张工作表?我所做的是这样的:

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 文件,其中第一张纸命名为。

【问题讨论】:

  • 离题:SaveAs() 中的文件名不应有扩展名,除非文件将被创建为“QResults.xls.xls 或任何其他类型的附加结尾

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


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-17
    • 2011-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多