翻譯|使用教程|編輯:李顯亮|2020-04-28 09:45:18.733|閱讀 423 次
概述:Aspose.Cells for .NET是Excel電子表格編程API,可加快電子表格管理和處理任務(wù),支持構(gòu)建具有生成,修改,轉(zhuǎn)換,呈現(xiàn)和打印電子表格功能的跨平臺應(yīng)用程序。 本文重點(diǎn)介紹如何在Microsoft Excel文件中添加和刪除工作表。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
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ù)制和移動(dòng)工作表。
>>Aspose.Cells for .NET已經(jīng)更新至v20.4,支持多個(gè)單元作為范圍的并集,添加用于更新PowerQueryFormulaItems的源字段的選項(xiàng),支持ODS的數(shù)據(jù)欄,色標(biāo)和圖標(biāo)集條件格式,修復(fù)諸多Bug,點(diǎn)擊下載體驗(yàn)
Aspose.Cells提供了一個(gè) 代表Excel文件的Workbook類。該Workbook類包含一個(gè)工作表 集合允許訪問在Excel文件中的每個(gè)工作表
將工作表添加到新的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)已知工作表的名稱時(shí),按名稱刪除工作表效果很好。如果您不知道工作表的名稱,請使用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");
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn