仅复制范围数据和样式
Contents
[
Hide
]
仅复制范围数据解释了如何将一个单元格范围的数据复制到另一个范围。具体来说,它会为复制的单元格应用一组新样式。 Aspose.Cells for Python via .NET还可以复制带有格式的范围。本文进行了解释。
Aspose.Cells for Python via .NET提供了一系列用于处理范围的类和方法,例如,create_range(upper_left_cell, lower_right_cell)、StyleFlag和apply_style(style, flag)。
此示例:
- 创建一个工作簿。
- 在第一个工作表中填充多个单元格的数据。
- 创建一个 Range。
- 使用指定的格式属性创建一个 Style 对象。
- 将样式应用到数据范围。
- 创建第二个单元格范围。
- 将第一个范围的带有格式的数据复制到第二个范围。
This file contains hidden or 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
from aspose.cells import BackgroundType, CellBorderType, StyleFlag, Workbook | |
from aspose.pydrawing import Color | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# The path to the documents directory. | |
dataDir = RunExamples.GetDataDir(".") | |
# Instantiate a new Workbook. | |
workbook = Workbook() | |
# Get the first Worksheet Cells. | |
cells = workbook.worksheets[0].cells | |
# Fill some sample data into the cells. | |
for i in range(50): | |
for j in range(10): | |
cells.get(i, j).put_value(str(i) + "," + str(j)) | |
# Create a range (A1:D3). | |
range = cells.create_range("A1", "D3") | |
style = workbook.create_style() | |
# Specify the font attribute. | |
style.font.name = "Calibri" | |
# Specify the shading color. | |
style.foreground_color = Color.yellow | |
style.pattern = BackgroundType.SOLID | |
# Specify the border attributes. | |
style.borders.set_color(Color.blue) | |
style.borders.set_style(CellBorderType.THIN) | |
# Create the styleflag object. | |
flag1 = StyleFlag() | |
# Implement font attribute | |
flag1.font_name = True | |
# Implement the shading / fill color. | |
flag1.cell_shading = True | |
# Implment border attributes. | |
flag1.borders = True | |
# Set the Range style. | |
range.apply_style(style, flag1) | |
# Create a second range (C10:F12). | |
range2 = cells.create_range("C10", "F12") | |
# Copy the range data with formatting. | |
range2.copy(range) | |
dataDir = dataDir + "CopyRange.out.xlsx" | |
# Save the excel file. | |
workbook.save(dataDir) |