C# Create Excel workbook with 1 sheet by default

孤街浪徒 提交于 2019-12-04 01:48:46

Take a look at MSDN's explanation of Workbooks.Add Method.

  1. Try Workbooks.Add(XlWBATemplate.xlWBATWorksheet), or
  2. See if you can set the xl.SheetsInNewWorkbook property to 0 or 1.

I went ahead and verified this. Here is the code:

using Microsoft.Office.Interop.Excel;
using System.Reflection;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Application xl = null;
            _Workbook wb = null;

            // Option 1
            xl = new Application();
            xl.Visible = true;
            wb = (_Workbook)(xl.Workbooks.Add(XlWBATemplate.xlWBATWorksheet));

            // Option 2
            xl = new Application();
            xl.SheetsInNewWorkbook = 1;
            xl.Visible = true;
            wb = (_Workbook)(xl.Workbooks.Add(Missing.Value));

        }
    }
}
RAYANED
Excel.Application xl = null;
Excel._Workbook wb = null;

xl = new Excel.Application();
xl.SheetsInNewWorkbook = 1;
xl.Visible = true;

wb = (_Workbook)(xl.Workbooks.Add(Missing.Value));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!