[英]C# Create Excel workbook with 1 sheet by default
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. 我正在尝试使用C#COM互操作创建一个Excel文件,但它似乎默认创建了3张而不是空或只有一张。 What is needed to create it Empty or just with one:
创建它需要什么空或只需一个:
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);
Take a look at MSDN's explanation of Workbooks.Add Method . 看看MSDN对Workbooks.Add方法的解释。
Workbooks.Add(XlWBATemplate.xlWBATWorksheet)
, or Workbooks.Add(XlWBATemplate.xlWBATWorksheet)
,或 xl.SheetsInNewWorkbook
property to 0 or 1. 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));
}
}
}
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));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.