Excel管理控件Aspose.Cells開發者指南(二十九):創建和管理工作表
Aspose.Cells for .NET是Excel電子表格編程API,可加快電子表格管理和處理任務,支持構建具有生成,修改,轉換,呈現和打印電子表格功能的跨平臺應用程序。
在接下來的系列教程中,將為開發者帶來Aspose.Cells for .NET的一系列使用教程,例如關于加載保存轉換、字體、渲染、繪圖、智能標記等等。本文重點介紹如何復制和移動工作表。
>>Aspose.Cells for .NET已經更新至v20.4,支持多個單元作為范圍的并集,添加用于更新PowerQueryFormulaItems的源字段的選項,支持ODS的數據欄,色標和圖標集條件格式,修復諸多Bug,點擊下載體驗
第七章:關于工作表的使用
▲第二節:創建和管理工作表
Aspose.Cells提供了一個 代表Excel文件的Workbook類。該Workbook類包含一個工作表 集合允許訪問在Excel文件中的每個工作表
將工作表添加到新的Excel文件
- 建工作簿 類的對象。
- 調用WorksheetCollection 類的Add 方法。一個空的工作表將自動添加到Excel文件中。可以通過將新工作表的工作表索引傳遞到Worksheets集合進行引用。
- 獲取工作表參考。
- 在工作表上執行工作。
- 通過調用Workbook 類的Save 方法,用新的工作表保存新的Excel文件。
// The path to the documents directory. string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); // Create directory if it is not already present. bool IsExists = System.IO.Directory.Exists(dataDir); if (!IsExists) System.IO.Directory.CreateDirectory(dataDir); // Instantiating a Workbook object Workbook workbook = new Workbook(); // Adding a new worksheet to the Workbook object int i = workbook.Worksheets.Add(); // Obtaining the reference of the newly added worksheet by passing its sheet index Worksheet worksheet = workbook.Worksheets[i]; // Setting the name of the newly added worksheet worksheet.Name = "My Worksheet"; // Saving the Excel file workbook.Save(dataDir + "output.out.xls");
將工作表添加到設計器電子表格
將工作表添加到設計器電子表格的過程與添加新工作表的過程相同,除了Excel文件已經存在,因此應在添加工作表之前將其打開。可以通過Workbook 類打開設計器電子表格。
// The path to the documents directory. string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); string InputPath = dataDir + "book1.xlsx"; // Creating a file stream containing the Excel file to be opened FileStream fstream = new FileStream(InputPath, FileMode.Open); // Opening the Excel file through the file stream Workbook workbook = new Workbook(fstream); // Adding a new worksheet to the Workbook object int i = workbook.Worksheets.Add(); // Obtaining the reference of the newly added worksheet by passing its sheet index Worksheet worksheet = workbook.Worksheets[i]; // Setting the name of the newly added worksheet worksheet.Name = "My Worksheet"; // Saving the Excel file workbook.Save(dataDir + "output.xlsx");
使用工作表名稱訪問工作表
通過指定其名稱或索引來訪問任何工作表。
// The path to the documents directory. string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); string InputPath = dataDir + "book1.xlsx"; // Creating a file stream containing the Excel file to be opened FileStream fstream = new FileStream(InputPath, FileMode.Open); // Instantiating a Workbook object // Opening the Excel file through the file stream Workbook workbook = new Workbook(fstream); // Accessing a worksheet using its sheet name Worksheet worksheet = workbook.Worksheets["Sheet1"]; Cell cell = worksheet.Cells["A1"]; Console.WriteLine(cell.Value);
使用工作表名稱刪除工作表
要從文件中刪除工作表,請調用WorksheetCollection 類的 RemoveAt 方法。將工作表名稱傳遞給RemoveAt 方法以刪除特定的工作表。
// The path to the documents directory. string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); // Creating a file stream containing the Excel file to be opened FileStream fstream = new FileStream(dataDir + "book1.xls", FileMode.Open); // Instantiating a Workbook object // Opening the Excel file through the file stream Workbook workbook = new Workbook(fstream); // Removing a worksheet using its sheet name workbook.Worksheets.RemoveAt("Sheet1"); // Save workbook workbook.Save(dataDir + "output.out.xls");
使用工作表索引刪除工作表
當已知工作表的名稱時,按名稱刪除工作表效果很好。如果您不知道工作表的名稱,請使用RemoveAt 方法的重載版本,該方法使用工作表的工作表索引而不是工作表名稱。
// The path to the documents directory. string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); // Creating a file stream containing the Excel file to be opened FileStream fstream = new FileStream(dataDir + "book1.xls", FileMode.Open); // Instantiating a Workbook object // Opening the Excel file through the file stream Workbook workbook = new Workbook(fstream); // Removing a worksheet using its sheet index workbook.Worksheets.RemoveAt(0); // Save workbook workbook.Save(dataDir + "output.out.xls");
Aspose是目前國內外非常火爆且功能強大的文件格式敏捷開發控件,但因為產品眾多、技術問題復雜等因素,也常常遭受開發人員吐槽。如果您也正在使用Aspose相關產品,點擊下方按鈕,來談談Aspose的優劣,您的感受對我們相當寶貴哦~