在默认情况下,用1张纸创建Excel工作簿。

时间:2022-01-24 20:53:27

I'm trying to create an Excel file with C# COM interop but seems it create it by default with 3 sheets instead of empty or only one. What is needed to create it Empty or just with one:

我试着用c# COM interop创建一个Excel文件,但是它默认是用3个表来创建的,而不是空的或者只有一个。需要什么来创建一个空的或者只有一个:

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

// Create a new instance of Excel from scratch
xl = new Excel.Application();
xl.Visible = true;     
wb = (Excel._Workbook)(xl.Workbooks.Add(Missing.Value));

wb.SaveAs(@"C:\a.xls", Excel.XlFileFormat.xlWorkbookNormal,
 null, null, false, false, Excel.XlSaveAsAccessMode.xlShared,
 false, false, null, null, null);

2 个解决方案

#1


20  

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

看看MSDN对工作簿的解释。添加方法。

  1. Try Workbooks.Add(XlWBATemplate.xlWBATWorksheet), or
  2. 尝试Workbooks.Add(XlWBATemplate.xlWBATWorksheet),或
  3. See if you can set the xl.SheetsInNewWorkbook property to 0 or 1.
  4. 看看你能不能设置xl。SheetsInNewWorkbook属性为0或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));

        }
    }
}

#2


2  

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));

#1


20  

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

看看MSDN对工作簿的解释。添加方法。

  1. Try Workbooks.Add(XlWBATemplate.xlWBATWorksheet), or
  2. 尝试Workbooks.Add(XlWBATemplate.xlWBATWorksheet),或
  3. See if you can set the xl.SheetsInNewWorkbook property to 0 or 1.
  4. 看看你能不能设置xl。SheetsInNewWorkbook属性为0或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));

        }
    }
}

#2


2  

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));