Excel管理控件Aspose.Cells開發(fā)者指南(二十九):創(chuàng)建和管理工作表
Aspose.Cells for .NET是Excel電子表格編程API,可加快電子表格管理和處理任務(wù),支持構(gòu)建具有生成,修改,轉(zhuǎn)換,呈現(xiàn)和打印電子表格功能的跨平臺應(yīng)用程序。
在接下來的系列教程中,將為開發(fā)者帶來Aspose.Cells for .NET的一系列使用教程,例如關(guān)于加載保存轉(zhuǎn)換、字體、渲染、繪圖、智能標(biāo)記等等。本文重點(diǎn)介紹如何復(fù)制和移動工作表。
>>Aspose.Cells for .NET已經(jīng)更新至v20.4,支持多個單元作為范圍的并集,添加用于更新PowerQueryFormulaItems的源字段的選項(xiàng),支持ODS的數(shù)據(jù)欄,色標(biāo)和圖標(biāo)集條件格式,修復(fù)諸多Bug,點(diǎn)擊下載體驗(yàn)
第七章:關(guān)于工作表的使用
▲第二節(jié):創(chuàng)建和管理工作表
Aspose.Cells提供了一個 代表Excel文件的Workbook類。該Workbook類包含一個工作表 集合允許訪問在Excel文件中的每個工作表
將工作表添加到新的Excel文件
- 建工作簿 類的對象。
- 調(diào)用WorksheetCollection 類的Add 方法。一個空的工作表將自動添加到Excel文件中??梢酝ㄟ^將新工作表的工作表索引傳遞到Worksheets集合進(jìn)行引用。
- 獲取工作表參考。
- 在工作表上執(zhí)行工作。
- 通過調(diào)用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");
將工作表添加到設(shè)計(jì)器電子表格
將工作表添加到設(shè)計(jì)器電子表格的過程與添加新工作表的過程相同,除了Excel文件已經(jīng)存在,因此應(yīng)在添加工作表之前將其打開。可以通過Workbook 類打開設(shè)計(jì)器電子表格。
// 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);
使用工作表名稱刪除工作表
要從文件中刪除工作表,請調(diào)用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");
使用工作表索引刪除工作表
當(dāng)已知工作表的名稱時,按名稱刪除工作表效果很好。如果您不知道工作表的名稱,請使用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是目前國內(nèi)外非?;鸨夜δ軓?qiáng)大的文件格式敏捷開發(fā)控件,但因?yàn)楫a(chǎn)品眾多、技術(shù)問題復(fù)雜等因素,也常常遭受開發(fā)人員吐槽。如果您也正在使用Aspose相關(guān)產(chǎn)品,點(diǎn)擊下方按鈕,來談?wù)凙spose的優(yōu)劣,您的感受對我們相當(dāng)寶貴哦~