ワークシートのコピーと移動
時には、共通のフォーマットとデータを持つワークシートの数が必要です。たとえば、四半期予算で作業する場合、同じ列見出し、行見出し、数式を含むシートを持つワークブックを作成したいと思うかもしれません。これを行う方法があります:1つのシートを作成してからコピーすることです。
Aspose.Cells for Python via .NETは、ワークブック内またはワークブック間でワークシートをコピーおよび移動する機能をサポートします。データ、書式設定、表、行列、グラフ、画像などのオブジェクトを含むワークシートは、最高の精度でコピーされます。
Microsoft Excelを使用してシートを移動またはコピーする方法
Microsoft Excelでワークブック内またはワークブック間でワークシートをコピーおよび移動する手順は次のとおりです。
- 別のワークブックにシートを移動またはコピーするには、シートを受け取るワークブックを開きます。
- 移動またはコピーしたいシートを含むワークブックに切り替え、そのシートを選択します。
- 編集メニューでシートの移動またはコピーをクリックします。
- ブックへダイアログボックスで、シートを受け取るワークブックをクリックします。
- 選択したシートを新しいワークブックに移動またはコピーするには、新しいブックをクリックします。
- 前のシートボックスで、移動またはコピーされたシートが挿入される前のシートをクリックします。
- 移動ではなくシートをコピーする場合は、コピーを作成チェックボックスを選択します。
Aspose.Cells for Python Excelライブラリを使用してワークブック内でワークシートをコピーする方法
Aspose.Cells for Python via .NETは、コレクションにワークシートを追加し、既存のワークシートからデータをコピーするために使用されるオーバーロードされたメソッドAspose.Cells.WorksheetCollection.add_copy()を提供します。このメソッドの1つのバージョンは、ソースワークシートのインデックスをパラメータとして取ります。他のバージョンは、ソースワークシートの名前を取ります。
次の例は、ブック内で既存のワークシートをコピーする方法を示しています。
from aspose.cells import Workbook | |
# 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(".") | |
InputPath = dataDir + "book1.xls" | |
# Open an existing Excel file. | |
wb = Workbook(InputPath) | |
# Create a Worksheets object with reference to | |
# the sheets of the Workbook. | |
sheets = wb.worksheets | |
# Copy data to a new sheet from an existing | |
# sheet within the Workbook. | |
sheets.add_copy("Sheet1") | |
# Save the Excel file. | |
wb.save(dataDir + "CopyWithinWorkbook_out.xls") |
ワークブック間でワークシートをコピーする方法
Aspose.Cells for Python via .NETでは、ソースワークシートから別のワークシートにデータと書式をコピーするために使用されるAspose.Cells.Worksheet.copy()メソッドが提供されます。
次の例は、ワークブックから別のワークブックにワークシートをコピーする方法を示しています。
from aspose.cells import Workbook | |
# 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(".") | |
InputPath = dataDir + "book1.xls" | |
# Create a Workbook. | |
# Open a file into the first book. | |
excelWorkbook0 = Workbook(InputPath) | |
# Create another Workbook. | |
excelWorkbook1 = Workbook() | |
# Copy the first sheet of the first book into second book. | |
excelWorkbook1.worksheets[0].copy(excelWorkbook0.worksheets[0]) | |
# Save the file. | |
excelWorkbook1.save(dataDir + "CopyWorksheetsBetweenWorkbooks_out.xls") |
次の例は、ワークブックから別のワークブックにワークシートをコピーする方法を示しています。
from aspose.cells import Workbook | |
# 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 a new Workbook. | |
excelWorkbook0 = Workbook() | |
# Get the first worksheet in the book. | |
ws0 = excelWorkbook0.worksheets[0] | |
# Put some data into header rows (A1:A4) | |
for i in range(5): | |
headerRow = "Header Row " + str(i) | |
ws0.cells.get(i, 0).put_value(headerRow) | |
# Put some detail data (A5:A999) | |
for i in range(5, 1000): | |
detailRow = "Detail Row " + str(i) | |
ws0.cells.get(i, 0).put_value(detailRow) | |
# Define a pagesetup object based on the first worksheet. | |
pagesetup = ws0.page_setup | |
# The first five rows are repeated in each page... | |
# It can be seen in print preview. | |
pagesetup.print_title_rows = "$1:$5" | |
# Create another Workbook. | |
excelWorkbook1 = Workbook() | |
# Get the first worksheet in the book. | |
ws1 = excelWorkbook1.worksheets[0] | |
# Name the worksheet. | |
ws1.name = "MySheet" | |
# Copy data from the first worksheet of the first workbook into the | |
# first worksheet of the second workbook. | |
ws1.copy(ws0) | |
# Save the excel file. | |
excelWorkbook1.save(dataDir + "CopyWorksheetFromWorkbookToOther_out.xls") |
ワークブック内でワークシートを移動する方法
Aspose.Cells for Python via .NETは、同じスプレッドシート内の別の場所にワークシートを移動するために使用されるAspose.Cells.Worksheet.move_to()メソッドを提供します。
次の例は、ワークブック内でワークシートを別の場所に移動する方法を示しています。
from aspose.cells import Workbook | |
# 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(".") | |
InputPath = dataDir + "book1.xls" | |
# Open an existing excel file. | |
wb = Workbook(InputPath) | |
# Create a Worksheets object with reference to | |
# the sheets of the Workbook. | |
sheets = wb.worksheets | |
# Get the first worksheet. | |
worksheet = sheets[0] | |
# Move the first sheet to the third position in the workbook. | |
worksheet.move_to(2) | |
# Save the excel file. | |
wb.save(dataDir + "MoveWorksheet_out.xls") |