翻譯|使用教程|編輯:李顯亮|2020-04-28 09:45:18.733|閱讀 423 次
概述:Aspose.Cells for .NET是Excel電子表格編程API,可加快電子表格管理和處理任務,支持構建具有生成,修改,轉換,呈現和打印電子表格功能的跨平臺應用程序。 本文重點介紹如何在Microsoft Excel文件中添加和刪除工作表。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
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文件
// 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");
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn