我想从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");
现在的问题是它复制了Sheet" Cash Bonuses"但它使Sheet名称为" Sheet111"。我想制作一张带有指定名称的表格,例如" Cash"。怎么做?将数据复制到新标签后,我想删除旧标签"现金奖金"并将新标签重命名为"现金奖励"来自" Cash"。
答案 0 :(得分:1)
请注意,为了将工作表的内容复制到另一个工作表,您需要向集合中添加一个空白工作表,然后在传递现有工作表的对象时调用其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作为开发者布道者。