ページ設定および印刷オプション
開発者は、印刷プロセスを制御するためにページ設定と印刷設定を構成する必要があります。Aspose.Cellsではページ設定と印刷設定を制御するためのさまざまなオプションがサポートされています。
この記事では、Aspose.Cells APIを使用して、数行のコードでコンソールアプリケーションを作成し、ワークシートにページの設定と印刷オプションを適用する方法を紹介しています。
ページ設定および印刷設定の操作
この例では、Microsoft Excelでワークブックを作成し、Aspose.Cellsを使用してページ設定と印刷オプションを設定しました。
ページの設定オプションを設定する
まず、Microsoft Excelで簡単なワークシートを作成します。次に、ページ設定オプションを適用します。コードを実行すると、以下のスクリーンショットのようにページ設定オプションが変更されます。
出力ファイル
- Microsoft Excelのワークシートにいくつかのデータを作成します。
-
Microsoft Excelで新しいブックを開きます。
-
いくつかのデータを追加します。 以下はファイルのスクリーンショットです。
入力ファイル
-
-
ページ設定オプションを設定します。 ファイルにページ設定オプションを適用します。以下は、新しいオプションが適用される前のデフォルトオプションのスクリーンショットです。
デフォルトのページ設定オプション
- Aspose.Cellsをダウンロードしてインストールします。
- プロジェクトを作成してください。 Javaエディタ(たとえばEclipse)を使用してプロジェクトを作成するか、テキストエディタを使用して簡単なプログラムを作成してください。
- クラスパスを追加します。
- Aspose.Cells.zipからAspose.Cells.jarとdom4j_1.6.1.jarを抽出します。
- Eclipseでプロジェクトのクラスパスを設定します。
- Eclipse でプロジェクトを選択し、プロジェクト をクリックしてから プロパティ をクリックします。
- ダイアログの左側で Javaビルドパス を選択します。
- ライブラリタブを選択し、JARの追加 または 外部JARの追加 をクリックして、Aspose.Cells.jar と dom4j_1.6.1.jar を選択してビルドパスに追加します。 または、Windows のコマンドプロンプトで実行時に設定することもできます。
javac \-classpath %classpath%;e:\Aspose.Cells.jar; ClassName .javajava \-classpath %classpath%;e:\Aspose.Cells.jar; ClassName
- API を呼び出すアプリケーションを作成します。 以下は、この例でコンポーネントで使用されるコードです。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(SettingPageSetupOptions.class); | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(dataDir + "CustomerReport.xls"); | |
// Accessing the first worksheet in the Excel file | |
Worksheet sheet = workbook.getWorksheets().get(0); | |
PageSetup pageSetup = sheet.getPageSetup(); | |
// Setting the orientation to Portrait | |
pageSetup.setOrientation(PageOrientationType.PORTRAIT); | |
// Setting the scaling factor to 100 | |
// pageSetup.setZoom(100); | |
// OR Alternately you can use Fit to Page Options as under | |
// Setting the number of pages to which the length of the worksheet will be spanned | |
pageSetup.setFitToPagesTall(1); | |
// Setting the number of pages to which the width of the worksheet will be spanned | |
pageSetup.setFitToPagesWide(1); | |
// Setting the paper size to A4 | |
pageSetup.setPaperSize(PaperSizeType.PAPER_A_4); | |
// Setting the print quality of the worksheet to 1200 dpi | |
pageSetup.setPrintQuality(1200); | |
// Setting the first page number of the worksheet pages | |
pageSetup.setFirstPageNumber(2); | |
// Save the workbook | |
workbook.save(dataDir + "PageSetup.xls"); |
印刷オプションの設定
ページ設定設定には、ワークシートページの印刷方法を制御するいくつかの印刷オプション(シートオプションとも呼ばれる)も提供されます。これにより、ユーザーは次のような操作ができます。
- ワークシートの特定の印刷エリアを選択します。
- タイトルを印刷する。
- グリッド線を印刷する。
- 行/列見出しを印刷します。
- 下書き品質を実現する。
- コメントを印刷する。
- セルエラーを印刷する。
- ページ順序を定義する。
次の例では、上記の例(PageSetup.xls)で作成されたファイルに印刷オプションを適用します。以下のスクリーンショットは、新しいオプションが適用される前のデフォルトの印刷オプションを示しています。 入力ドキュメント
コードを実行すると、印刷オプションが変更されます。 出力ファイル
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(SettingPrintoptions.class); | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(dataDir + "PageSetup.xls"); | |
// Accessing the first worksheet in the Excel file | |
Worksheet sheet = workbook.getWorksheets().get(0); | |
PageSetup pageSetup = sheet.getPageSetup(); | |
// Specifying the cells range (from A1 cell to E30 cell) of the print area | |
pageSetup.setPrintArea("A1:E30"); | |
// Defining column numbers A & E as title columns | |
pageSetup.setPrintTitleColumns("$A:$E"); | |
// Defining row numbers 1 & 2 as title rows | |
pageSetup.setPrintTitleRows("$1:$2"); | |
// Allowing to print gridlines | |
pageSetup.setPrintGridlines(true); | |
// Allowing to print row/column headings | |
pageSetup.setPrintHeadings(true); | |
// Allowing to print worksheet in black & white mode | |
pageSetup.setBlackAndWhite(true); | |
// Allowing to print comments as displayed on worksheet | |
pageSetup.setPrintComments(PrintCommentsType.PRINT_IN_PLACE); | |
// Allowing to print worksheet with draft quality | |
pageSetup.setPrintDraft(true); | |
// Allowing to print cell errors as N/A | |
pageSetup.setPrintErrors(PrintErrorsType.PRINT_ERRORS_NA); | |
// Setting the printing order of the pages to over then down | |
pageSetup.setOrder(PrintOrderType.OVER_THEN_DOWN); | |
// Save the workbook | |
workbook.save(dataDir + "PageSetup_Print.xls"); |
まとめ
本文では、Aspose.Cellsを使用してページ設定およびシート印刷オプションを設定する方法について説明します。お役に立てれば幸いです。お客様のご質問、コメント、提案をApose.Cells Forumで心よりお待ちしております。きめ細やかな対応を保証いたします。
Aspose.Cellsは長年にわたる研究、設計、慎重な調整の成果を受けています。ご質問、コメント、提案を心より歓迎いたします。