我想通过C#创建Excel工作簿后只添加一张工作表

发布于 2024-11-25 05:16:57 字数 434 浏览 1 评论 0原文

代码是这样的

        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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

晚风撩人 2024-12-02 05:16:57

这是创建 Excel 应用程序对象并打开仅包含一张工作表的工作簿并根据需要命名的代码:

Excel.Application appC = new Excel.Application();    
appC.SheetsInNewWorkbook = 1;       
appC.Visible = true;     
Excel.Workbook bookC = appC.Workbooks.Add();    
Excel.Worksheet sheetC = appC.Sheets.get_Item(1);   
sheetC.Name = "name-of-sheet";

This is the code to create an Excel application object and open a workbook with only ONE sheet and name it as you wish:

Excel.Application appC = new Excel.Application();    
appC.SheetsInNewWorkbook = 1;       
appC.Visible = true;     
Excel.Workbook bookC = appC.Workbooks.Add();    
Excel.Worksheet sheetC = appC.Sheets.get_Item(1);   
sheetC.Name = "name-of-sheet";
遗忘曾经 2024-12-02 05:16:57

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.]

雾里花 2024-12-02 05:16:57

我遇到了同样的问题。您需要添加这样的工作表:

//add 1 sheet
_workbookTemp.Sheets.Add(Type.Missing, Type.Missing, 1, Type.Missing);

//move this sheet to the last position
_workbookTemp.ActiveSheet.Move(After: _workbookTemp.Sheets[_workbookTemp.Sheets.Count]);

I faced the same problem. You need to add a sheet like this:

//add 1 sheet
_workbookTemp.Sheets.Add(Type.Missing, Type.Missing, 1, Type.Missing);

//move this sheet to the last position
_workbookTemp.ActiveSheet.Move(After: _workbookTemp.Sheets[_workbookTemp.Sheets.Count]);
箹锭⒈辈孓 2024-12-02 05:16:57

如果您使用的是 vs 2010,则不同,您可以使用下面的代码将工作表添加到工作簿中,我已经在 VS 2010 中尝试过,这对我有用,我使用 excel 2007 工作簿项目模板

void AddSheet()
{
 OpenFileDialog excelSheetToOpen = new OpenFileDialog();
            excelSheetToOpen.Filter = "Excel 97- 2003 WorkBook (*.xls)| *.xls | Excel 2007 WorkBook (*.xlsx) | *.xlsx | All files (*.*)|*.*";
            excelSheetToOpen.FilterIndex = 3;
            excelSheetToOpen.Multiselect = false;

             Excel.Worksheet ws = Globals.ThisWorkbook.Worksheets.get_Item("RunningParameters");


             if (excelSheetToOpen.ShowDialog() == DialogResult.OK)
             {

                 Excel.Application excelApp = new Excel.Application();
                 String workbookPath = excelSheetToOpen.FileName;
                 Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(workbookPath);
                 Excel.Sheets excelWorkBookSheets = excelWorkbook.Sheets;

                 Excel.Range _UsedRangeOftheWorkSheet;


                 foreach (Excel.Worksheet _Sheet in excelWorkBookSheets)
                 {
                     if (_Sheet.Name == ws.get_Range("B3").Value)
                     {
                         _Sheet.UsedRange.Copy();
                         _UsedRangeOftheWorkSheet = _Sheet.UsedRange;

                         Object [,] s = _UsedRangeOftheWorkSheet.Value;                        


                         Excel.Worksheet _WorkingSheet = Globals.ThisWorkbook.Sheets.Add(ws);
                         _WorkingSheet.Name = "WorkingSheet";
                         _WorkingSheet.Paste();



                     }
                 }  

             }


}

此代码直接从我的项目请根据需要添加代码希望这将有助于解决您的问题

谢谢

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

void AddSheet()
{
 OpenFileDialog excelSheetToOpen = new OpenFileDialog();
            excelSheetToOpen.Filter = "Excel 97- 2003 WorkBook (*.xls)| *.xls | Excel 2007 WorkBook (*.xlsx) | *.xlsx | All files (*.*)|*.*";
            excelSheetToOpen.FilterIndex = 3;
            excelSheetToOpen.Multiselect = false;

             Excel.Worksheet ws = Globals.ThisWorkbook.Worksheets.get_Item("RunningParameters");


             if (excelSheetToOpen.ShowDialog() == DialogResult.OK)
             {

                 Excel.Application excelApp = new Excel.Application();
                 String workbookPath = excelSheetToOpen.FileName;
                 Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(workbookPath);
                 Excel.Sheets excelWorkBookSheets = excelWorkbook.Sheets;

                 Excel.Range _UsedRangeOftheWorkSheet;


                 foreach (Excel.Worksheet _Sheet in excelWorkBookSheets)
                 {
                     if (_Sheet.Name == ws.get_Range("B3").Value)
                     {
                         _Sheet.UsedRange.Copy();
                         _UsedRangeOftheWorkSheet = _Sheet.UsedRange;

                         Object [,] s = _UsedRangeOftheWorkSheet.Value;                        


                         Excel.Worksheet _WorkingSheet = Globals.ThisWorkbook.Sheets.Add(ws);
                         _WorkingSheet.Name = "WorkingSheet";
                         _WorkingSheet.Paste();



                     }
                 }  

             }


}

This code is directly extracted from my projecte please ammed the code as needed hope this will help to solve your problem

thanks

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文