【发布时间】: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