[]
        
(Showing Draft Content)

剪切或复制形状、切片器、图表和图片

GCExcel Java可以使用户将形状、图表、切片器和图片从一个工作簿剪切或复制到另一个工作簿,以及从一个工作表剪切或复制到另一个工作表。

要执行复制操作,可以使用 IRange 接口的 copy() 方法。

要执行剪切操作,可以使用 IRange 接口的 cut() 方法。

要在GcExcel Java中剪切或复制形状、切片器、图表和图片,请参考以下示例代码。

Workbook workbook = new Workbook();
IWorksheet worksheet = workbook.getWorksheets().get(0);

// Create a shape in worksheet, shape's range is Range("A7:B7")
IShape shape = worksheet.getShapes().addShape(AutoShapeType.Rectangle, 40, 40, 100, 100);
shape.getTextFrame().getTextRange().getFont().getColor().setRGB(Color.FromArgb(0, 255, 0));
 
// Range["A1:D10"] contains Range["A7:B7"], copy a new shape to Range["C1:F7"]
worksheet.getRange("A1:D10").copy(worksheet.getRange("C1"));
worksheet.getRange("A1:D10").copy(worksheet.getRange("C1:G9"));
 
// Cross sheet copy operation - copy a new shape to worksheet2's Range["C1:F7"]
IWorksheet worksheet2 = workbook.getWorksheets().add();
worksheet.getRange("A1:D10").copy(worksheet2.getRange("C1"));
worksheet.getRange("A1:D10").copy(worksheet2.getRange("C1:G9"));
 
// Range["A1:D10"] contains Range["A7:B7"], cut a new shape to Range["C1:F7"]
worksheet.getRange("A1:D10").cut(worksheet.getRange("C1"));
worksheet.getRange("A1:D10").cut(worksheet.getRange("C1:G9"));

// Cross sheet cut operation - cut a new shape to worksheet2's Range["C1:F7"]
IWorksheet worksheet3 = workbook.getWorksheets().add();
worksheet.getRange("A1:D10").cut(worksheet3.getRange("C1"));
worksheet2.getRange("A1:D10").cut(worksheet3.getRange("C1:G9"));

要将形状复制到当前工作表,可以使用IShape 接口的方法。

要复制现有形状、切片器、图表和图片,请参考以下示例代码。

// Create Shape
IShape shape1 = worksheet.getShapes().addShape(AutoShapeType.Rectangle, 50, 50, 200, 200);
 
// Create Chart, chart's range is Range["G1:M21"]
IShape chart = chartworksheet.getShapes().addChart(ChartType.ColumnClustered, 300, 10, 300, 300);
chartworksheet.getRange("A1:D6").setValue(new Object[][]{
        {null, "S1", "S2", "S3"},
        {"Item1", 10, 25, 25},
        {"Item2", -51, -36, 27},
        {"Item3", 52, -85, -30},
        {"Item4", 22, 65, 65},
        {"Item5", 23, 69, 69}
});
chart.getChart().getSeriesCollection().add(chartworksheet.getRange("A1:D6"), RowCol.Columns, true, true);
 
// Create slicer cache for table.
ISlicerCache cache = workbook1.getSlicerCaches().add(table, "Category", "categoryCache");

// Create slicer
ISlicer slicer = cache.getSlicers().add(workbook1.getWorksheets().get("Sheet1"), "cate1", "Category", 30, 550, 100, 200);
 
// Create Picture
IShape picture = worksheet.getShapes().addPicture("C:/Pictures", 1, 1, 100, 100);
 
// Duplicate Shape
IShape newShape = shape1.duplicate();
 
// Duplicate Chart
IShape newchart = chart.duplicate();
 
// Duplicate Slicer
IShape slicerShape = slicer.getShape().duplicate();
 
// Duplicate Picture
IShape newPicture = picture.duplicate();