![](/img/trans.png)
[英]Is there a way to format an Aspose.Cells generated excel sheet without calling Workbook.Save()?
[英]Aspose workbook copy/rename sheet
我想從Excel復制工作表,創建具有特定名稱的工作表副本。
Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook(excelFilePath);
//Create a Worksheets object with reference to the sheets of the Workbook.
WorksheetCollection sheets = workbook.Worksheets;
sheets.AddCopy("Cash Bonuses");
現在的問題是,它復制工作表“現金紅利”的數據,但使工作表名稱為“ Sheet111”。 我想使用諸如“現金”之類的指定名稱制作此工作表。該怎么做? 數據復制到新選項卡后,我想刪除舊選項卡“現金獎金”,並將新選項卡從“現金”重命名為“現金獎金”。
請注意,為了將工作表的內容復制到另一個工作表,您需要向集合中添加一個空白工作表,然后在傳遞現有工作表的對象(需要復制的對象)的同時調用其Copy方法,否則您將丟失目標工作表上的數據。
請嘗試以下代碼,因為它會嘗試滿足您的所有要求。 希望這些評論可以幫助您理解聲明的含義。
var workbook = new Aspose.Cells.Workbook(excelFilePath);
var sheets = workbook.Worksheets;
//Access 1st worksheet from the collection
//You may also pass the worksheet name to access a particular worksheet
var sheet0 = sheets[0];
//Add a new worksheet to the collection and name it as desired
var sheet1 = sheets[sheets.Add()];
sheet1.Name = "Cash";
//Copy the contents of 1st worksheet onto the new worksheet
sheet1.Copy(sheet0);
//Delete 1st worksheet
sheets.RemoveAt(sheet0.Index);
//Rename newly added worksheet to 'Cash bonuses'
sheet1.Name = "Cash bonuses";
//Save result
workbook.Save(dir + "output.xlsx");
注意:我與Aspose一起擔任開發人員推廣人員。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.