我想通过C#创建Excel工作簿后只添加一张工作表
代码是这样的
Excel.Application appC = new Excel.Application();
appC.Visible = true;
Excel.Workbook bookC = appC.Workbooks.Add(1);
Excel.Worksheet sheetC = bookC.Worksheets.Add();
sheetC.Name = "something";
命令 Workbook.Add()
采用一个参数,该参数应该确定将在工作簿中创建多少张工作表......对吗?
那么为什么我会得到 2 张纸……一张名为“something”,一张名为“sheet 2”? 我做错了什么?
The code is this
Excel.Application appC = new Excel.Application();
appC.Visible = true;
Excel.Workbook bookC = appC.Workbooks.Add(1);
Excel.Worksheet sheetC = bookC.Worksheets.Add();
sheetC.Name = "something";
The command Workbook.Add()
takes one parameter that is supposed to determine how many sheets will be created in the workbook... right?
So why do I get 2 sheets... one named "something" and one named "sheet 2"?
What am I doing wrong??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是创建 Excel 应用程序对象并打开仅包含一张工作表的工作簿并根据需要命名的代码:
This is the code to create an Excel application object and open a workbook with only ONE sheet and name it as you wish:
Workbooks.Add 的参数不指定工作表的数量。
请参阅 Add 方法的 MSDN 描述。
您可能应该使用常量 xlWBATWorksheet 而不仅仅是“1”。
[我不在工作,手边没有 Excel;该常量的值可能实际上是 1,在这种情况下,这不会产生(功能上)差异。另一种方法是在创建工作簿之前设置 SheetsInNewWorkbook 属性,或者在创建工作簿后简单地删除不需要的工作表。]
The parameter to Workbooks.Add does NOT specify the number of sheets.
See the MSDN description of the Add method.
You should probably use the constant xlWBATWorksheet rather than just "1".
[I'm not at Work and don't have Excel handy; it may be that the value of that constant is actually 1, in which case this will make no (functional) difference. The alternative is to set the SheetsInNewWorkbook property before creating the workbook, or simply deleting the unwanted sheets after creating the workbook.]
我遇到了同样的问题。您需要添加这样的工作表:
I faced the same problem. You need to add a sheet like this:
如果您使用的是 vs 2010,则不同,您可以使用下面的代码将工作表添加到工作簿中,我已经在 VS 2010 中尝试过,这对我有用,我使用 excel 2007 工作簿项目模板
此代码直接从我的项目请根据需要添加代码希望这将有助于解决您的问题
谢谢
if you are using vs 2010 it is diffrent you can use the below code to add a work sheet to a work book this i have tried this in VS 2010 this works for me im using excel 2007 work book project template
This code is directly extracted from my projecte please ammed the code as needed hope this will help to solve your problem
thanks