ページ設定および印刷オプション
開発者は、印刷プロセスを制御するためにページ設定と印刷設定を構成する必要があります。Aspose.Cellsではページ設定と印刷設定を制御するためのさまざまなオプションがサポートされています。
この記事では、Visual Studio.Netでコンソールアプリケーションを作成し、Aspose.Cells APIを使用してワークシートに簡単なコード行を適用してページ設定と印刷オプションを適用する方法を示します。
ページ設定および印刷設定の操作
この例では、Microsoft Excelでワークブックを作成し、Aspose.Cellsを使用してページ設定と印刷オプションを設定しました。
Aspose.Cellsを使用してページ設定オプションを設定する
まず、Microsoft Excelで簡単なワークシートを作成します。次に、ページ設定オプションを適用します。コードを実行すると、以下のスクリーンショットのようにページ設定オプションが変更されます。
出力ファイル。 |
---|
![]() |
- Microsoft Excelのワークシートにいくつかのデータを作成します。
- Microsoft Excelで新しいブックを開きます。
- いくつかのデータを追加します。
- ページ設定オプションを設定します。 ファイルにページ設定オプションを適用します。以下は、新しいオプションが適用される前のデフォルトオプションのスクリーンショットです。
デフォルトのページ設定オプション。 |
---|
![]() |
- Aspose.Cellsをダウンロードしてインストールします。
- Aspose.Cells for .Net をダウンロードします。
- 開発コンピュータにインストールします。 すべてのAsposeのコンポーネントは、インストールされると評価モードで動作します。評価モードには時間制限はなく、生成された文書にウォーターマークを注入するだけです。
- プロジェクトを作成します。
- Visual Studioを起動します。
- 新しいコンソールアプリケーションを作成します。 この例ではC#コンソールアプリケーションを示しますが、VB.NETも使用できます。
- 参照を追加します。
- この例ではAspose.Cellsを使用するため、プロジェクトにそのコンポーネントへの参照を追加します。例: …\Program Files\Aspose\Aspose.Cells\Bin\Net1.0\Aspose.Cells.dll
- APIを呼び出すアプリケーションを記述します。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Open the template workbook | |
Workbook workbook = new Workbook(dataDir + "CustomerReport.xlsx"); | |
// Accessing the first worksheet in the Excel file | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Setting the orientation to Portrait | |
worksheet.PageSetup.Orientation = PageOrientationType.Portrait; | |
// Setting the scaling factor to 100 | |
// worksheet.PageSetup.Zoom = 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 | |
worksheet.PageSetup.FitToPagesTall = 1; | |
// Setting the number of pages to which the width of the worksheet will be spanned | |
worksheet.PageSetup.FitToPagesWide = 1; | |
// Setting the paper size to A4 | |
worksheet.PageSetup.PaperSize = PaperSizeType.PaperA4; | |
// Setting the print quality of the worksheet to 1200 dpi | |
worksheet.PageSetup.PrintQuality = 1200; | |
//Setting the first page number of the worksheet pages | |
worksheet.PageSetup.FirstPageNumber = 2; | |
// Save the workbook | |
workbook.Save(dataDir + "PageSetup_out.xlsx"); |
印刷オプションの設定
ページ設定設定には、ワークシートページの印刷方法を制御するいくつかの印刷オプション(シートオプションとも呼ばれる)も提供されます。これにより、ユーザーは次のような操作ができます。
- ワークシートの特定の印刷エリアを選択します。
- タイトルを印刷する。
- グリッド線を印刷する。
- 行/列見出しを印刷します。
- 下書き品質を実現する。
- コメントを印刷する。
- セルエラーを印刷する。
- ページ順序を定義する。
次の例では、上記の例(PageSetup.xls)で作成されたファイルに印刷オプションを適用します。以下のスクリーンショットは、新しいオプションが適用される前のデフォルトの印刷オプションを示しています。
入力ドキュメント |
---|
![]() |
コードを実行すると、印刷オプションが変更されます。 |
出力ファイル |
---|
![]() |
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Open the template workbook | |
Workbook workbook = new Workbook(dataDir + "PageSetup.xlsx"); | |
// Accessing the first worksheet in the Excel file | |
Worksheet worksheet = workbook.Worksheets[0]; | |
PageSetup pageSetup = worksheet.PageSetup; | |
// Specifying the cells range (from A1 cell to E30 cell) of the print area | |
pageSetup.PrintArea = "A1:E30"; | |
// Defining column numbers A & E as title columns | |
pageSetup.PrintTitleColumns = "$A:$E"; | |
// Defining row numbers 1 as title rows | |
pageSetup.PrintTitleRows = "$1:$2"; | |
// Allowing to print gridlines | |
pageSetup.PrintGridlines = true; | |
// Allowing to print row/column headings | |
pageSetup.PrintHeadings = true; | |
// Allowing to print worksheet in black & white mode | |
pageSetup.BlackAndWhite = true; | |
// Allowing to print comments as displayed on worksheet | |
pageSetup.PrintComments = PrintCommentsType.PrintInPlace; | |
// Allowing to print worksheet with draft quality | |
pageSetup.PrintDraft = true; | |
// Allowing to print cell errors as N/A | |
pageSetup.PrintErrors = PrintErrorsType.PrintErrorsNA; | |
// Setting the printing order of the pages to over then down | |
pageSetup.Order = PrintOrderType.OverThenDown; | |
// Save the workbook | |
workbook.Save(dataDir + "PageSetup_Print_out.xlsx"); |