新增工作表并激活工作表
Contents
[
Hide
]
在处理模板文件时,有时需要向工作簿中添加额外的工作表以收集数据。每个工作表中的指定位置和位置将填充新的单元格中的数据。
同样地,您可能需要一个特定的工作表在打开Microsoft Excel时处于活动状态并首先查看。“活动工作表"是工作簿中正在处理的工作表。活动工作表的标签名称默认为粗体。
添加工作表并设置哪个工作表是活动的是开发人员需要知道如何执行的常见且简单的任务。在本文中,我们使用 VSTO 和 Aspose.Cells for .NET 执行了这些任务。
添加工作表并激活工作表
为了这个迁移提示的目的:
- 向现有的微软Excel文件添加新的工作表。
- 向每个新工作表的单元格中填充数据。
- 在工作簿中激活工作表。
- 另存为微软Excel文件。
下面是 VSTO(C#、VB)和 Aspose.Cells for .NET(C#、VB)的并行代码片段,展示如何实现这些任务。
VSTO
C#
.......
using Microsoft.VisualStudio.Tools.Applications.Runtime;
using Excel = Microsoft.Office.Interop.Excel;
using Office = Microsoft.Office.Core;
using System.Reflection;
.......
//Instantiate the Application object.
Excel.Application excelApp = new Excel.ApplicationClass();
//Specify the template excel file path.
string myPath = @"d:\test\My_Book1.xls";
//Open the excel file.
excelApp.Workbooks.Open(myPath, Missing.Value, Missing.Value,
Missing.Value, Missing.Value,
Missing.Value, Missing.Value,
Missing.Value, Missing.Value,
Missing.Value, Missing.Value,
Missing.Value, Missing.Value,
Missing.Value, Missing.Value);
//Declare a Worksheet object.
Excel.Worksheet newWorksheet;
//Add 5 new worksheets to the workbook and fill some data
//into the cells.
for (int i = 1; i < 6; i++)
{
//Add a worksheet to the workbook.
newWorksheet = Excel.Worksheet)excelApp.Worksheets.Add(Missing.Value, Missing.Value, Missing.Value, Missing.Value);
//Name the sheet.
newWorksheet.Name ="New_Sheet" + i.ToString();
//Get the Cells collection.
Excel.Range cells = newWorksheet.Cells;
//Input a string value to a cell of the sheet.
cells.set_Item(i, i,"New_Sheet" + i.ToString());
}
//Activate the first worksheet by default.
((Excel.Worksheet)excelApp.ActiveWorkbook.Sheets[1]).Activate();
//Save As the excel file.
excelApp.ActiveWorkbook.SaveCopyAs(@"d:\test\out_My_Book1.xls");
//Quit the Application.
excelApp.Quit();
VB
.......
Imports Microsoft.VisualStudio.Tools.Applications.Runtime
Imports Excel = Microsoft.Office.Interop.Excel
Imports Office = Microsoft.Office.Core
Imports System.Reflection
.......
'Instantiate the Application object.
Dim excelApp As Excel.Application = New Excel.ApplicationClass()
'Specify the template excel file path.
Dim myPath As String = "d:\test\My_Book1.xls"
'Open the excel file.
excelApp.Workbooks.Open(myPath, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value)
'Declare a Worksheet object.
Dim newWorksheet As Excel.Worksheet
'Add 5 new worksheets to the workbook and fill some data
'into the cells.
Dim i As Integer
For i = 1 To 5 Step 1
'Add a worksheet to the workbook.
newWorksheet = CType(excelApp.Worksheets.Add(Missing.Value, Missing.Value, Missing.Value, Missing.Value), Excel.Worksheet)
'Name the sheet.
newWorksheet.Name ="New_Sheet" & i.ToString()
'Get the Cells collection.
Dim cells As Excel.Range = newWorksheet.Cells
'Input a string value to a cell of the sheet.
cells.Item(i, i) = "New_Sheet" & i.ToString()
Next
'Activate the first worksheet by default.
CType(excelApp.ActiveWorkbook.Sheets(1), Excel.Worksheet).Activate()
'Save As the excel file.
excelApp.ActiveWorkbook.SaveCopyAs("d:\test\out_My_Book1.xls")
'Quit the Application.
excelApp.Quit()
Aspose.Cells for .NET
C#
.......
using Aspose.Cells;
.......
//Instantiate an instance of license and set the license file
//through its path
Aspose.Cells.License license = new Aspose.Cells.License();
license.SetLicense("Aspose.Cells.lic");
//Specify the template excel file path.
string myPath =@"d:\test\My_Book1.xls";
//Instantiate a new Workbook.
//Open the excel file.
Workbook workbook = new Workbook(myPath);
//Declare a Worksheet object.
Worksheet newWorksheet;
//Add 5 new worksheets to the workbook and fill some data
//into the cells.
for (int i = 0; i < 5; i++)
{
//Add a worksheet to the workbook.
newWorksheet = workbook.Worksheets[workbook.Worksheets.Add()];
//Name the sheet.
newWorksheet.Name = "New_Sheet" + (i+1).ToString();
//Get the Cells collection.
Cells cells = newWorksheet.Cells;
//Input a string value to a cell of the sheet.
cells[i, i].PutValue("New_Sheet" + (i+1).ToString());
}
//Activate the first worksheet by default.
workbook.Worksheets.ActiveSheetIndex = 0;
//Save As the excel file.
workbook.Save(@"d:\test\out_My_Book1.xls");
VB
.......
Imports Aspose.Cells
.......
'Instantiate an instance of license and set the license file
'through its path
Dim license As Aspose.Cells.License = New Aspose.Cells.License
license.SetLicense("Aspose.Cells.lic")
'Specify the template excel file path.
Dim myPath As String ="d:\test\My_Book1.xls"
'Instantiate a new Workbook.
'Open the excel file.
Dim workbook As Workbook = New Workbook(myPath)
'Declare a Worksheet object.
Dim newWorksheet As Worksheet
'Add 5 new worksheets to the workbook and fill some data
'into the cells.
Dim i As Integer
For i = 0 To 4 Step 1
'Add a worksheet to the workbook.
newWorksheet = workbook.Worksheets(workbook.Worksheets.Add())
'Name the sheet.
newWorksheet.Name = "New_Sheet" + (i + 1).ToString()
'Get the Cells collection.
Dim cells As Cells = newWorksheet.Cells
'Input a string value to a cell of the sheet.
cells(i, i).PutValue("New_Sheet" + (i + 1).ToString())
Next
'Activate the first worksheet by default.
workbook.Worksheets.ActiveSheetIndex = 0
'Save As the excel file.
workbook.Save("c:\test\out_My_Book1.xls")