using HotelPms.Share.Windows.Component; using HotelPms.Share.Windows.Report; using HotelPms.Share.Windows.Report.Member; using HotelPms.Share.Windows.Util; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace HotelPms.SourceFactory { public partial class FormReport : Form { private DataTable listData; private DataTable data; public FormReport() { InitializeComponent(); } private void FormReport_Load(object sender, EventArgs e) { GeneralSub.SetDoubleBuffered(dataGridView1); GeneralSub.InitDataGridView(dataGridView1, true); InitData(); InitReport(); CreateTestData(); dataGridView1.DataSource = listData; SetGridStyle(); } private void InitData() { data = new DataTable(); data.Columns.Add("Field0", typeof(DateTime)); data.Columns.Add("Field1", typeof(DateTime)); data.Columns.Add("Field2", typeof(int)); data.Columns.Add("Field3", typeof(int)); data.Columns.Add("Field4", typeof(int)); data.Columns.Add("Field5", typeof(int)); data.Columns.Add("Field6", typeof(int)); data.Columns.Add("Field7", typeof(int)); data.Columns.Add("Field8", typeof(int)); data.Columns.Add("Field9", typeof(string)); for (int i = 0; i < 100; i++) { DataRow row = data.NewRow(); row["Field0"] = DateTime.Now.AddDays((double)i); row["Field1"] = DateTime.Now.AddMinutes(i); row["Field2"] = i + 10000; row["Field3"] = i + 5000; row["Field4"] = i + 6000; row["Field5"] = i + 7000; row["Field6"] = i + 8000; row["Field7"] = i + 9000; row["Field8"] = i + 4000; row["Field9"] = $"備考ですよ:{i}"; } } private void InitReport() { pnlPageHeader.SetPropertyEx("ReportTag", new ReportControl() { ItemType = ReportControl.EItemType.PageHeader }); pnlGroupHeader.SetPropertyEx("ReportTag", new ReportControl() { ItemType = ReportControl.EItemType.GroupHeader }); pnlDetail.SetPropertyEx("ReportTag", new ReportControl() { ItemType = ReportControl.EItemType.Detail }); } private void SetGridStyle() { dataGridView1.ColumnHeadersDefaultCellStyle.SelectionBackColor = SystemColors.Control; dataGridView1.Columns[0].Width = 250; dataGridView1.Columns[1].Width = 100; dataGridView1.Columns[2].Width = 80; dataGridView1.Columns[2].DefaultCellStyle.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter; dataGridView1.Columns[3].Width = 200; dataGridView1.Columns[4].Width = 150; dataGridView1.Columns[5].Width = 100; dataGridView1.Columns[6].Width = 60; dataGridView1.Columns[6].DefaultCellStyle.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter; dataGridView1.Columns[7].Width = 100; dataGridView1.Columns[8].Width = 100; dataGridView1.Columns[9].Width = 150; dataGridView1.Columns[10].Width = 150; dataGridView1.Columns[10].DefaultCellStyle.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleRight; dataGridView1.Columns[11].Width = 100; dataGridView1.Columns[11].DefaultCellStyle.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleRight; dataGridView1.Columns[12].Width = 100; dataGridView1.Columns[12].DefaultCellStyle.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleRight; dataGridView1.Columns[13].Width = 100; dataGridView1.Columns[13].DefaultCellStyle.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleRight; dataGridView1.Columns[14].Width = 100; dataGridView1.Columns[14].DefaultCellStyle.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleRight; } private void CreateTestData() { listData = new DataTable(); listData.Columns.Add("予約No", typeof(string)); listData.Columns.Add("部屋タイプ", typeof(string)); listData.Columns.Add("部屋No", typeof(int)); listData.Columns.Add("氏名", typeof(string)); listData.Columns.Add("カナ", typeof(string)); listData.Columns.Add("到着日", typeof(DateTime)); listData.Columns.Add("泊数", typeof(int)); listData.Columns.Add("出発日", typeof(DateTime)); listData.Columns.Add("部屋人数", typeof(int)); listData.Columns.Add("利用合計", typeof(int)); listData.Columns.Add("入金合計", typeof(int)); listData.Columns.Add("現金", typeof(int)); listData.Columns.Add("クレジット", typeof(int)); listData.Columns.Add("クーポン", typeof(int)); listData.Columns.Add("その他", typeof(int)); for (int i = 0; i < 100; i++) { Random rd = new Random(Guid.NewGuid().GetHashCode()); DataRow row = listData.NewRow(); listData.Rows.Add(row); row["予約No"] = Guid.NewGuid().ToString(); row["部屋タイプ"] = GetType(rd.Next(1, 4)); row["部屋No"] = rd.Next(101, 3000); int nameId = rd.Next(1, 11); row["氏名"] = GetName(nameId) + " " + GetKanjiNumber(rd.Next(1, 11)) + "郎"; row["カナ"] = GetNameKana(nameId); DateTime cinDate = DateTime.Now.Date.AddDays((double)rd.Next(1, 10)); int stay = rd.Next(1, 99); DateTime coutDate = cinDate.AddDays((double)stay); row["到着日"] = cinDate; row["泊数"] = stay; row["出発日"] = coutDate; row["部屋人数"] = rd.Next(1, 11); int cash = rd.Next(0, 100000); int credit = rd.Next(0, 100000); int coupon = rd.Next(0, 100000); int other = rd.Next(0, 100000); row["利用合計"] = cash + credit + coupon + other; row["入金合計"] = cash + credit + coupon + other; row["現金"] = cash; row["クレジット"] = credit; row["クーポン"] = coupon; row["その他"] = other; } } private string GetKanjiNumber(int type) { switch (type) { case 1: return "一"; case 2: return "二"; case 3: return "三"; case 4: return "四"; case 5: return "五"; case 6: return "六"; case 7: return "七"; case 8: return "八"; case 9: return "九"; case 10: return "十"; default: return "太"; } } private string GetNameKana(int type) { switch (type) { case 1: return "クマモト"; case 2: return "フクオカ"; case 3: return "サトウ"; case 4: return "ナカタ"; case 5: return "ナカノ"; case 6: return "タムラ"; case 7: return "キムラ"; case 8: return "ヨシカワ"; case 9: return "キチセ"; case 10: return "ヨシカワ"; default: return "タナカ"; } } private string GetName(int type) { switch (type) { case 1: return "熊本"; case 2: return "福岡"; case 3: return "佐藤"; case 4: return "中田"; case 5: return "中野"; case 6: return "田村"; case 7: return "木村"; case 8: return "吉川"; case 9: return "吉瀬"; case 10: return "吉岡"; default: return "田中"; } } private string GetType(int type) { switch(type) { case 1: return "シングル"; case 2: return "ダブル"; default: return "ツイン"; } } private void button2_Click(object sender, EventArgs e) { if (dataGridView1.Rows.Count == 0) { MessageBox.Show("印刷対象を選択してください。", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information); return; } //using (Graphics g = this.CreateGraphics()) //{ // g.PageUnit = GraphicsUnit.Pixel; // using (Font font = new Font("MS UI Gothic", 9F)) // { // float w = g.MeasureString("濃", font).Width; // } //} // Truncate([numChars*7+5]/7*256)/256 using (DataTable data = (dataGridView1.DataSource as DataTable).Copy()) { GridStyle style = new GridStyle(); style.DefaultRowStyle.Padding = new Padding(10); style.DefaultCellStyle.Font = dataGridView1.DefaultCellStyle.Font; //ヘッダー情報設定 style.Header.DefaultRowStyle.Font = new Font("MS UI Gothic", 12F); style.Header.DefaultRowStyle.Padding = new Padding(5); style.Header.DefaultCellStyle.Font = new Font("MS UI Gothic", 12F); #region 一行目 PrintRow printRow = new PrintRow(); style.Header.Rows.Add(printRow); //右下⇒印刷日時+頁: CellStyle cellTyle = new CellStyle { Dock = DockStyle.Right, WidthMode = CellStyle.EWidthMode.TextWidth, Padding = new Padding(0, 3, 3, 0), Font = new Font("メイリオ", 10F), Alignment = StringAlignment.Far, LineAlignment = StringAlignment.Far, Template = "印刷日時:{0}\r\n頁:{1} / {2}" }; //cellTyle.TopBorder.Visible = cellTyle.LeftBorder.Visible = cellTyle.RightBorder.Visible = cellTyle.BottomBorder.Visible = true; Cell cell = new Cell { CellType = Cell.CellKind.TimeAndPage, Style = cellTyle }; printRow.Cells.Add(cell); //タイトル cellTyle = new CellStyle { Dock = DockStyle.Fill, WidthMode = CellStyle.EWidthMode.AutoFill, Padding = new Padding(0, 3, 3, 0), Font = new Font("メイリオ", 18F), Alignment = StringAlignment.Near, LineAlignment = StringAlignment.Center, }; //cellTyle.TopBorder.Visible = cellTyle.LeftBorder.Visible = cellTyle.RightBorder.Visible = cellTyle.BottomBorder.Visible = true; cell = new Cell { CellType = Cell.CellKind.Text, Style = cellTyle, Text = "部屋別売上一覧" }; printRow.Cells.Add(cell); #endregion #region 二行目 printRow = new PrintRow(); style.Header.Rows.Add(printRow); cellTyle = new CellStyle { LocalXMode = CellStyle.ELocalXMode.BeginOfCol, WidthMode = CellStyle.EWidthMode.EndOfCol, BeginCol = 0, EndCol = 3, Padding = new Padding(0, 3, 3, 0), Font = new Font("メイリオ", 10F), Alignment = StringAlignment.Near, LineAlignment = StringAlignment.Center, Template = "利用期間:{0}" }; //cellTyle.TopBorder.Visible = cellTyle.LeftBorder.Visible = cellTyle.RightBorder.Visible = cellTyle.BottomBorder.Visible = true; cell = new Cell { CellType = Cell.CellKind.Text, Style = cellTyle, Text = "2021/09/01~2021/09/30" }; printRow.Cells.Add(cell); cellTyle = new CellStyle { LocalXMode = CellStyle.ELocalXMode.BeginOfCol, WidthMode = CellStyle.EWidthMode.TextWidth, BeginCol = 4, Padding = new Padding(0, 3, 3, 0), Font = new Font("メイリオ", 10F), Alignment = StringAlignment.Near, LineAlignment = StringAlignment.Center, }; //cellTyle.TopBorder.Visible = cellTyle.LeftBorder.Visible = cellTyle.RightBorder.Visible = cellTyle.BottomBorder.Visible = true; cell = new Cell { CellType = Cell.CellKind.Text, Style = cellTyle, Text = "利用状態:在室、アウト" }; printRow.Cells.Add(cell); #endregion ReportBase.CreateColumnStyle(dataGridView1, style); ReportFactory.OutputGrid(data, style); } MessageBox.Show("OK"); } } }