範囲のデータのみをコピーします。
Contents
[
Hide
]
場合によっては、データをセル範囲から別の範囲にコピーする際、書式をコピーせずにデータのみをコピーする必要があります。 Aspose.Cells for Python via .NETではこの機能が提供されます。
この記事では、Aspose.Cells for Python via .NETを使用してデータ範囲をコピーするサンプルコードを提供します。
この例では、次のことができます:
- ワークブックを作成する。
- 最初のワークシートのセルにデータを追加する。
- Rangeを作成します。
- 指定した書式属性でStyleを作成します。
- 範囲にスタイルを適用します。
- 別のセルの範囲を作成します。
- 最初の範囲のデータを2番目の範囲にコピーします。
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
from aspose.cells import BackgroundType, CellBorderType, StyleFlag, Workbook | |
from aspose.pydrawing import Color | |
from os import os, path | |
# 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(".") | |
# Create directory if it is not already present. | |
IsExists = path.isdir(dataDir) | |
if notIsExists: | |
os.makedirs(dataDir) | |
# 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 only. | |
range2.copy_data(range) | |
dataDir = dataDir + "CopyRangeData.out.xlsx" | |
# Save the excel file. | |
workbook.save(dataDir) |