在工作表之间复制形状
Contents
[
Hide
]
有时,您需要在工作表之间复制元素,例如图片、图表和其他绘图对象。Aspose.Cells支持此功能。可以以最高精度复制图表、图片和其他对象。
本文详细介绍了如何在工作表之间复制形状。
从一个工作表复制图片到另一个工作表
要将图片从一个工作表复制到另一个,请使用以下示例代码中显示的 Worksheet.Pictures.Add 方法。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Open the template file | |
Workbook workbook = new Workbook(dataDir + "sample.xlsx"); | |
// Get the Picture from the "Picture" worksheet. | |
Aspose.Cells.Drawing.Picture picturesource = workbook.Worksheets["Picture"].Pictures[0]; | |
// Save Picture to Memory Stream | |
MemoryStream ms = new MemoryStream(picturesource.Data); | |
// Copy the picture to the Result Worksheet | |
workbook.Worksheets["Result"].Pictures.Add(picturesource.UpperLeftRow, picturesource.UpperLeftColumn, ms, picturesource.WidthScale, picturesource.HeightScale); | |
// Save the Worksheet | |
workbook.Save(dataDir + "PictureCopied_out.xlsx"); |
将图表从一个工作表复制到另一个
以下代码演示了使用 Worksheet.Shapes.AddCopy 方法将图表从一个工作表复制到另一个。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Open the template file | |
Workbook workbook = new Workbook(dataDir + "sample.xlsx"); | |
// Get the Chart from the "Chart" worksheet. | |
Aspose.Cells.Charts.Chart chartsource = workbook.Worksheets["Chart"].Charts[0]; | |
Aspose.Cells.Drawing.ChartShape cshape = chartsource.ChartObject; | |
// Copy the Chart to the Result Worksheet | |
workbook.Worksheets["Result"].Shapes.AddCopy(cshape, 20, 0, 2, 0); | |
// Save the Worksheet | |
workbook.Save(dataDir + "ChartCopied_out.xlsx"); |
从一个工作表复制控件和其他绘图对象到另一个
要复制控件和其他绘图对象,请使用以下示例中显示的 Worksheet.Shapes.AddCopy 方法。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Open the template file | |
Workbook workbook = new Workbook(dataDir + "sample2.xlsx"); | |
// Get the Shapes from the "Control" worksheet. | |
Aspose.Cells.Drawing.ShapeCollection shape = workbook.Worksheets["Control"].Shapes; | |
// Copy the Textbox to the Result Worksheet | |
workbook.Worksheets["Result"].Shapes.AddCopy(shape[0], 5, 0, 2, 0); | |
// Copy the Oval Shape to the Result Worksheet | |
workbook.Worksheets["Result"].Shapes.AddCopy(shape[1], 10, 0, 2, 0); | |
// Save the Worksheet | |
workbook.Save(dataDir + "ControlsCopied_out.xlsx"); |