ワークシートのコピーと移動
場合によっては、共通の書式設定とデータを含む多数のワークシートが必要になることがあります。たとえば、四半期ごとの予算を扱う場合、同じ列見出し、行見出し、および数式を含むシートを含むワークブックを作成できます。これを行う方法があります: 1 つのシートを作成し、それをコピーします。
Aspose.Cells は、ワークブック内またはワークブック間でのワークシートのコピーと移動をサポートしています。データ、フォーマット、テーブル、マトリックス、チャート、画像、その他のオブジェクトを備えたワークシートは、最高の精度でコピーされます。
Microsoft Excel を使用したシートの移動またはコピー
以下は、ワークブック内またはワークブック間でワークシートをコピーおよび移動するための手順です。
- シートを別のブックに移動またはコピーするには、シートを受け取るブックを開きます。
- 移動またはコピーするシートを含むブックに切り替えてから、シートを選択します。
- 上で編集メニュー、クリックシートの移動またはコピー.
- [To book] ボックスで、ワークブックをクリックしてシートを受け取ります。
- 選択したシートを新しいワークブックに移動またはコピーするには、新しい本.
- の中にシート前ボックスで、移動またはコピーしたシートを挿入する前のシートをクリックします。
- シートを移動する代わりにコピーするには、コピーを作成するチェックボックス。
ワークブック内でワークシートをコピーする
Aspose.Cells はオーバーロードされたメソッドを提供し、WorksheetCollection.addCopy()、ワークシートをコレクションに追加し、既存のワークシートからデータをコピーするために使用されます。メソッドの 1 つのバージョンは、ソース ワークシートのインデックスをパラメーターとして受け取ります。もう一方のバージョンは、ソース ワークシートの名前を取ります。
次の例は、ブック内の既存のワークシートをコピーする方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
String dataDir = Utils.getSharedDataDir(AddingPageBreaks.class) + "worksheets/"; | |
// Create a new Workbook by excel file path | |
Workbook wb = new Workbook(dataDir + "book1.xls"); | |
// Create a Worksheets object with reference to the sheets of the Workbook. | |
WorksheetCollection sheets = wb.getWorksheets(); | |
// Copy data to a new sheet from an existing sheet within the Workbook. | |
sheets.addCopy("Sheet1"); | |
// Save the excel file. | |
wb.save(dataDir + "CopyWithinWorkbook_out.xls"); |
ワークブック間でワークシートをコピーする
Aspose.Cells はメソッドを提供し、Worksheet.copy()、ソース ワークシートからワークブック内またはワークブック間でデータと書式設定を別のワークシートにコピーするために使用されます。このメソッドは、ソース ワークシート オブジェクトをパラメーターとして受け取ります。
次の例は、あるブックから別のブックにワークシートをコピーする方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
String dataDir = Utils.getSharedDataDir(AddingPageBreaks.class) + "worksheets/"; | |
// Create a Workbook. | |
Workbook excelWorkbook0 = new Workbook(dataDir + "book1.xls"); | |
// Create another Workbook. | |
Workbook excelWorkbook1 = new Workbook(); | |
// Copy the first sheet of the first book into second book. | |
excelWorkbook1.getWorksheets().get(0).copy(excelWorkbook0.getWorksheets().get(0)); | |
// Save the file. | |
excelWorkbook1.save(dataDir + "CWBetweenWorkbooks_out.xls", FileFormatType.EXCEL_97_TO_2003); |
次の例は、あるブックから別のブックにワークシートをコピーする方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
String dataDir = Utils.getSharedDataDir(AddingPageBreaks.class) + "worksheets/"; | |
// Create a new Workbook. | |
Workbook excelWorkbook0 = new Workbook(); | |
// Get the first worksheet in the book. | |
Worksheet ws0 = excelWorkbook0.getWorksheets().get(0); | |
// Put some data into header rows (A1:A4) | |
for (int i = 0; i < 5; i++) { | |
ws0.getCells().get(i, 0).setValue("Header Row " + i); | |
} | |
// Put some detail data (A5:A999) | |
for (int i = 5; i < 1000; i++) { | |
ws0.getCells().get(i, 0).setValue("Detail Row " + i); | |
} | |
// Define a pagesetup object based on the first worksheet. | |
PageSetup pagesetup = ws0.getPageSetup(); | |
// The first five rows are repeated in each page... It can be seen in print preview. | |
pagesetup.setPrintTitleRows("$1:$5"); | |
// Create another Workbook. | |
Workbook excelWorkbook1 = new Workbook(); | |
// Get the first worksheet in the book. | |
Worksheet ws1 = excelWorkbook1.getWorksheets().get(0); | |
// Name the worksheet. | |
ws1.setName("Sheet1"); | |
// 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", FileFormatType.EXCEL_97_TO_2003); |
ワークブック内でワークシートを移動する
Aspose.Cells はメソッドを提供し、Worksheet.moveTo()、ワークシートを同じスプレッドシート内の別の場所に移動するために使用されます。
次の例は、ワークシートをブック内の別の場所に移動する方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
String dataDir = Utils.getSharedDataDir(MoveWorksheet.class) + "worksheets/"; | |
// Create a new Workbook. | |
Workbook wb = new Workbook(dataDir + "BkFinance.xls"); | |
// Get the first worksheet in the book. | |
Worksheet sheet = wb.getWorksheets().get(0); | |
// Move the first sheet to the third position in the workbook. | |
sheet.moveTo(2); | |
// Save the Excel file. | |
wb.save(dataDir + "MoveWorksheet_out.xls"); |