1. はじめに
- C#のコンソールアプリからExcelファイルを印刷したい
- Excelで印刷せず、VB-Reportのビューアーコントロールから使用したい
2. 動作確認
- C#
- .NET .8
- VB-Report 11
- Windows 10
- Visual Studio 2022
3. VB-Reportとは
- アドバンスソフトウェア株式会社が開発したExcel で帳票デザインを作成する帳票開発ツールです。
- ビューアーコントローを使用することでプレビュー、印刷することが可能です。
4. サンプルプログラム
- Excelファイル名、シート名、プリンタ名を固定で出力する。
Program.cs
using AdvanceSoftware.VBReport;
using System.Windows.Forms;
namespace ConsoleApp
{
internal class Program
{
/// <summary>
/// 必要なデザイナー変数です。
/// </summary>
static System.ComponentModel.IContainer components = null;
static ViewerControl viewerControl1;
static CellReport cellReport1;
static void Main(string[] args)
{
components = new System.ComponentModel.Container();
cellReport1 = new CellReport(components);
viewerControl1 = new ViewerControl();
// 帳票出力前にビューアコントロールをクリア
viewerControl1.Clear();
// Excel帳票ファイル名を指定
cellReport1.FileName = @"C:\sample.xlsx";
// 帳票ドキュメントの作成開始
cellReport1.Report.Start();
// 帳票処理の開始
cellReport1.Report.File();
// 帳票のページ処理開始(Excelシート名、ページ数)
cellReport1.Page.Start("合計請求書1", "1");
// 帳票のページ処理終了
cellReport1.Page.End();
// 帳票ドキュメントの作成終了
cellReport1.Report.End();
// 印刷ジョブ名の指定
viewerControl1.PrintDocumentName = "印刷ジョブ名";
// 印刷プリンタの設定
// プリンタ名が存在しない場合、通常使うプリンターへ出力
viewerControl1.SetPrinter(false, "プリンタ名");
// ビューアコントロールに帳票ドキュメントを設定
viewerControl1.Document = cellReport1.Document;
// ビューアコントロールからプリンタ印刷
viewerControl1.PrintOut();
// 出力が失敗した場合、ErrorNo にてエラー内容が取得できます。
if (cellReport1.ErrorNo != AdvanceSoftware.VBReport.ErrorNo.NoError)
{
MessageBox.Show(cellReport1.ErrorMessage);
return;
}
}
}
}
5. 参考文献