using System; using System.Collections.Generic; using System.Text; using System.Drawing; using System.Drawing.Printing; using System.Data; using System.Windows.Forms; using System.Diagnostics; using System.Collections; using System.Text.RegularExpressions; using System.IO; namespace NetLibrary.ReportPrint { #region 报表打印_数据分页打印 public class PrintReport { private MyReport m_Report; public MyReport Report { get { return m_Report; } set { m_Report = value; SetPageRecordNumber(Convert.ToInt32(value.Width), Convert.ToInt32(value.Height)); } } private PrintDocument m_Document; public PrintDocument Document { get { return m_Document; } set { m_Document = value; } } private Kind m_Kind; public Kind Kind { get { return m_Kind; } set { m_Kind = value; } } private Int16 m_Copies; /// /// 份数 /// public Int16 Copies { get { return m_Copies; } set { m_Copies = value; } } private bool m_IsFixHeaderFooter = false; /// /// 是否固定页眉、尾 /// public virtual bool IsFixHeaderFooter { get { return m_IsFixHeaderFooter; } set { m_IsFixHeaderFooter = value; } } private bool m_Collate = true; public virtual bool Collate { get { return m_Collate; } set { m_Collate = value; } } private float m_ColumnWidthRectify = 0; /// /// 微调列宽 /// public virtual float ColumnWidthRectify { get { return m_ColumnWidthRectify; } set { m_ColumnWidthRectify = value; } } private int m_PageCount = 1; /// /// 一共所需打印页数 /// public int PageCount { get { return m_PageCount; } set { m_PageCount = value; if (m_PageCount == 0) m_PageCount = 1; } } int CurrentPageNumber = 0; //当前页号 public int StartPageNumber = 0; //结束页号 public int StopPageNumber = 0; //结束页号 private bool m_IsFillBlank = false; /// /// 是否填充空行 /// public virtual bool IsFillBlank { get { return m_IsFillBlank; } set { m_IsFillBlank = value; } } private int m_Index; public int Index { get { return m_Index; } set { m_Index = value; } } public event EventHandler EndPrint; int startIndex = 0; //数据源行号 public PrintReport(MyReport report) { Copies = 1; m_Document = new PrintDocument(); m_Document.PrintPage += new PrintPageEventHandler(pd_PrintPage); m_Document.BeginPrint += new PrintEventHandler(m_Document_BeginPrint); m_Document.QueryPageSettings += new QueryPageSettingsEventHandler(m_Document_QueryPageSettings); m_Document.EndPrint += new PrintEventHandler(m_Document_EndPrint); this.Report = report; this.Kind = this.Report.PagerKind[0]; } public PrintReport() { Copies = 1; m_Document = new PrintDocument(); m_Document.PrintPage += new PrintPageEventHandler(pd_PrintPage); m_Document.BeginPrint += new PrintEventHandler(m_Document_BeginPrint); m_Document.QueryPageSettings += new QueryPageSettingsEventHandler(m_Document_QueryPageSettings); m_Document.EndPrint += new PrintEventHandler(m_Document_EndPrint); } private void MathRowHeight(Table tb) { if (tb.Rows.Count == 0) return; Font CurrentFont = this.Report.Font; foreach (TableRow row in tb.Rows) { if (row.Height > MeasureString("测试高度", CurrentFont).Height) continue; int RowNumber = 1; float WidthCount = 0; foreach (TableColumn col in tb.Columns) { int index = tb.Columns.IndexOf(col); if (row.Cells[index].Font != null) CurrentFont = row.Cells[index].Font; string value1 = Regex.Replace(row.Cells[index].Text, "", "", RegexOptions.IgnoreCase); value1 = Regex.Replace(value1, "", "", RegexOptions.IgnoreCase); value1 = Regex.Replace(value1, "", "", RegexOptions.IgnoreCase); value1 = Regex.Replace(value1, "", "", RegexOptions.IgnoreCase); if (Regex.IsMatch(value1, "
", RegexOptions.IgnoreCase) == false) { WidthCount = MeasureString(value1, CurrentFont).Width; int CurrentRowNumber = 1; try { CurrentRowNumber = Convert.ToInt32(Math.Ceiling(WidthCount / (col.Width * row.Cells[index].ColumnSpan))); } catch { } if (CurrentRowNumber == 0) CurrentRowNumber = 1; if (CurrentRowNumber > RowNumber) RowNumber = CurrentRowNumber; } else { int MaxRowNumber = 0; string[] ss = Regex.Split(value1, "
", RegexOptions.IgnoreCase); foreach (string s in ss) { WidthCount = MeasureString(s, CurrentFont).Width; int CurrentRowNumber = 1; try { CurrentRowNumber = Convert.ToInt32(Math.Ceiling(WidthCount / col.Width * row.Cells[index].ColumnSpan)); } catch { } if (CurrentRowNumber == 0) CurrentRowNumber = 1; MaxRowNumber = MaxRowNumber + CurrentRowNumber; } if (MaxRowNumber > RowNumber) RowNumber = MaxRowNumber; } } row.RowSpan = RowNumber; row.Height = MeasureString("测试高度", CurrentFont).Height; } } private void MathRowHeight(PageRows Rows) { Font CurrentFont = this.Report.Font; float WidthCount = 0; foreach (PageRow row in Rows) { if (row.Font != null) CurrentFont = row.Font; row.Height = 0; float Height = Convert.ToSingle(MeasureString(row.Text, CurrentFont).Height); float charHeight = Convert.ToSingle(MeasureString("测试高度", CurrentFont).Height); if (Height == 0) { row.Height = charHeight; continue; } float cellWidth = Convert.ToSingle(this.Report.Width - Report.Left - Report.Right); string value1 = Regex.Replace(row.Text, "", "", RegexOptions.IgnoreCase); value1 = Regex.Replace(value1, "", "", RegexOptions.IgnoreCase); value1 = Regex.Replace(value1, "", "", RegexOptions.IgnoreCase); value1 = Regex.Replace(value1, "", "", RegexOptions.IgnoreCase); if (Regex.IsMatch(value1, "
", RegexOptions.IgnoreCase) == false) { WidthCount = MeasureString(value1, CurrentFont).Width; int CurrentRowNumber = 1; try { CurrentRowNumber = Convert.ToInt32(Math.Ceiling(WidthCount / cellWidth)); } catch { } if (CurrentRowNumber == 0) CurrentRowNumber = 1; row.Height = Height * CurrentRowNumber; } else { string[] ss = Regex.Split(value1, "
", RegexOptions.IgnoreCase); foreach (string s in ss) { WidthCount = MeasureString(s, CurrentFont).Width; int CurrentRowNumber = 1; try { CurrentRowNumber = Convert.ToInt32(Math.Ceiling(WidthCount / cellWidth)); } catch { } if (CurrentRowNumber == 0) CurrentRowNumber = 1; row.Height = row.Height + Height * CurrentRowNumber; } } } } public SizeF MeasureString(string Text, Font font) { Bitmap bmp = new Bitmap(100, 100); Graphics g = Graphics.FromImage(bmp); g.PageUnit = font.Unit; SizeF s = g.MeasureString(Text, font); return s; } public SizeF MeasureString(string Text, Font font, GraphicsUnit unit) { Bitmap bmp = new Bitmap(100, 100); Graphics g = Graphics.FromImage(bmp); g.PageUnit = unit; SizeF s = g.MeasureString(Text, font); return s; } public void SetPageRecordNumber(int width, int height) { if (Report.AutoMathPageRecordNumber == false) { if (this.ColumnWidthRectify != 0) { foreach (TableColumn col in Report.Group.LeftItem.Columns) { col.Width = col.Width + this.ColumnWidthRectify; } foreach (TableColumn col in Report.Group.Header.Columns) { col.Width = col.Width + this.ColumnWidthRectify; } foreach (TableColumn col in Report.Group.Footer.Columns) { col.Width = col.Width + this.ColumnWidthRectify; } foreach (TableColumn col in Report.Group.DataItem.Columns) { col.Width = col.Width + this.ColumnWidthRectify; } foreach (TableColumn col in Report.Group.RightItem.Columns) { col.Width = col.Width + this.ColumnWidthRectify; } } MathRowHeight(Report.Header.TableItem); MathRowHeight(Report.Footer.TableItem); MathRowHeight(Report.Group.Footer); MathRowHeight(Report.Group.Header); MathRowHeight(Report.Group.LeftItem); MathRowHeight(Report.Group.RightItem); MathRowHeight(Report.Group.DataItem); MathRowHeight(Report.Header.Rows); MathRowHeight(Report.Footer.Rows); return; } //重设高度 // float Original Report.Group.Height = Convert.ToSingle(height) - Report.Header.Height - Report.Footer.Height - Report.Top - Report.Bottom; Report.Group.Width = Convert.ToSingle(width - Report.Left - Report.Right);//重设宽度 float DataWidth = 0; foreach (TableColumn col in Report.Group.Header.Columns) { DataWidth = DataWidth + col.Width; } float w = (Report.Group.Width - DataWidth - this.Report.Group.LeftItem.Width - this.Report.Group.RightItem.Width) / Convert.ToSingle(Report.Group.Header.Columns.Count); if (w > 0) { foreach (TableColumn col in Report.Group.Header.Columns) { col.Width = col.Width + w; } foreach (TableColumn col in Report.Group.DataItem.Columns) { col.Width = col.Width + w; } foreach (TableColumn col in Report.Group.Footer.Columns) { col.Width = col.Width + w; } } if (this.ColumnWidthRectify != 0) { foreach (TableColumn col in Report.Group.LeftItem.Columns) { col.Width = col.Width + this.ColumnWidthRectify; } foreach (TableColumn col in Report.Group.DataItem.Columns) { col.Width = col.Width + this.ColumnWidthRectify; } foreach (TableColumn col in Report.Group.Header.Columns) { col.Width = col.Width + this.ColumnWidthRectify; } foreach (TableColumn col in Report.Group.Footer.Columns) { col.Width = col.Width + this.ColumnWidthRectify; } foreach (TableColumn col in Report.Group.RightItem.Columns) { col.Width = col.Width + this.ColumnWidthRectify; } } MathRowHeight(Report.Header.TableItem); MathRowHeight(Report.Footer.TableItem); MathRowHeight(Report.Group.Footer); MathRowHeight(Report.Group.Header); MathRowHeight(Report.Group.LeftItem); MathRowHeight(Report.Group.RightItem); MathRowHeight(Report.Group.DataItem); MathRowHeight(Report.Header.Rows); MathRowHeight(Report.Footer.Rows); } public int GetPagerCount() { return this.PageCount; } private void m_Document_QueryPageSettings(object sender, QueryPageSettingsEventArgs e) { e.PageSettings.Margins = new Margins(0, 0, 0, 0); //e.PageSettings.PrinterSettings.DefaultPageSettings.Margins = e.PageSettings.Margins; e.PageSettings.PrinterSettings.Copies = this.Copies; e.PageSettings.PrinterSettings.Collate = this.Collate; if (this.Report.RotatePoint > 0) { e.PageSettings.Landscape = true; } else { e.PageSettings.Landscape = this.Kind.Landscape; } //e.PageSettings.PrinterSettings.DefaultPageSettings.Landscape = e.PageSettings.Landscape; PaperSize CustomPaperSize = null; PaperSize PaperSizeItem = null; foreach (PaperSize item in e.PageSettings.PrinterSettings.PaperSizes) { if (item.PaperName == this.Kind.Name) { PaperSizeItem = item; break; } if (item.Kind == PaperKind.Custom) CustomPaperSize = item; } if (PaperSizeItem != null) { e.PageSettings.PaperSize = PaperSizeItem; } else { if (CustomPaperSize != null) { e.PageSettings.PaperSize = new PaperSize(CustomPaperSize.PaperName, System.Drawing.Printing.PrinterUnitConvert.Convert(this.Kind.Width * 10, PrinterUnit.TenthsOfAMillimeter, PrinterUnit.Display), System.Drawing.Printing.PrinterUnitConvert.Convert(this.Kind.Height * 10, PrinterUnit.TenthsOfAMillimeter, PrinterUnit.Display)); e.PageSettings.PaperSize.RawKind = CustomPaperSize.RawKind; } } //e.PageSettings.PrinterSettings.DefaultPageSettings.PaperSize = e.PageSettings.PaperSize; if (Report.PageLayout == PageLayoutType.GridLayout) e.PageSettings.PrinterSettings.PrintRange = PrintRange.SomePages; e.PageSettings.PrinterSettings.FromPage = this.CurrentPageNumber; e.PageSettings.PrinterSettings.ToPage = this.StopPageNumber; } public void MathPageCount() { if (Report.Group.DataItem == null || Report.Group.DataItem.Rows.Count == 0) { this.PageCount = 1; return; } if (this.Report.AutoMathPageRecordNumber == false) { this.PageCount = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(Report.Group.DataItem.Rows.Count) / Convert.ToDouble(Report.PageRecordNumber))); //一共所需打印页数 } else { this.PageCount = 1; float currentHeigt = 0; Font CurrentFont = this.Report.Font; //计算页眉高度 float y = this.Report.Top; foreach (TableRow row in this.Report.Group.Header.Rows) { y = y + row.Height * row.RowSpan + Report.TableRowHeightReviseNumber; } foreach (TableRow row in this.Report.Group.Footer.Rows) { y = y + row.Height * row.RowSpan + Report.TableRowHeightReviseNumber; } //计算页数 foreach (TableRow row in this.Report.Group.DataItem.Rows) { //计算行最大高度 int RowIndex = this.Report.Group.DataItem.Rows.IndexOf(row); float maxRowHeight = row.Height * row.RowSpan + Report.TableRowHeightReviseNumber; currentHeigt = currentHeigt + maxRowHeight; if (currentHeigt + y + this.Report.Header.Height + this.Report.Group.Footer.Height > this.Report.Group.Height + this.Report.Header.Height + this.Report.Top) { this.PageCount++; currentHeigt = maxRowHeight; }//如果已画高度+当前所需画的高度超出预计,那么页数+1 } } } private void m_Document_BeginPrint(object sender, PrintEventArgs e) { if (this.Kind.Landscape == false) { SetPageRecordNumber(this.Kind.Width, this.Kind.Height); } else { SetPageRecordNumber(this.Kind.Height, this.Kind.Width); } m_Document.DocumentName = Convert.ToString(this.StopPageNumber - this.StartPageNumber); if (this.Report.PageRecordNumber > 0 || this.StartPageNumber == 1) return; this.CurrentPageNumber = this.StartPageNumber; this.PageCount = 1; float currentHeigt = 0; Font CurrentFont = this.Report.Font; //计算页眉高度 float y = this.Report.Top; foreach (TableRow row in this.Report.Group.Header.Rows) { y = y + row.Height * row.RowSpan + Report.TableRowHeightReviseNumber; } foreach (TableRow row in this.Report.Group.Footer.Rows) { y = y + row.Height * row.RowSpan + Report.TableRowHeightReviseNumber; } //计算页数 foreach (TableRow row in this.Report.Group.DataItem.Rows) { //计算行最大高度 int RowIndex = this.Report.Group.DataItem.Rows.IndexOf(row); float maxRowHeight = row.Height * row.RowSpan + Report.TableRowHeightReviseNumber; currentHeigt = currentHeigt + maxRowHeight; if (currentHeigt + y + this.Report.Header.Height + this.Report.Group.Footer.Height > this.Report.Group.Height + this.Report.Header.Height + this.Report.Top) {//如果已画高度+当前所需画的高度超出预计,那么页数+1 this.PageCount++; currentHeigt = maxRowHeight; if (this.PageCount == this.CurrentPageNumber + 1) this.startIndex = RowIndex; } } } private void pd_PrintPage(object sender, PrintPageEventArgs e) { Graphics g = e.Graphics; //RectangleF picRect = new RectangleF(10, 10, e.MarginBounds.Height, e.MarginBounds.Width); //float y=picRect.Y + picRect.Height / 2; //float x = picRect.X + picRect.Width / 2; //g.TranslateTransform(y / 2, y - picRect.Height / 2); ////旋转的角度 //g.RotateTransform(90.0F); if (this.Report.RotatePoint > 0) { //旋转的角度 if (this.Report.RotateX == 0) this.Report.RotateX = e.MarginBounds.Width / 4; g.TranslateTransform(this.Report.RotateX, this.Report.RotateY); g.RotateTransform(this.Report.RotatePoint); } g.PageUnit = GraphicsUnit.Millimeter; DrawStringObject(g); DrawLines(g); DrawRectangles(g); DrawImages(g); if (this.Report.PageRecordNumber != 0) startIndex = Report.PageRecordNumber * this.CurrentPageNumber; DrawHeader(g, Report.Left, Report.Top); float dpiy = DrawGroups(g, startIndex, Report.Left, Report.Header.Height + this.Report.Top); DrawFooter(g, Report.Left, dpiy + 2); //startIndex += Report.PageRecordNumber; CurrentPageNumber++; if (CurrentPageNumber < this.StopPageNumber) { e.HasMorePages = true; } else { e.HasMorePages = false; startIndex = 0; CurrentPageNumber = 0; } } void DrawStringObject(Graphics g) { foreach (StringObject item in Report.PageStringObjects) { Font CurrentFont = this.Report.Font; if (item.Font != null) CurrentFont = item.Font; if (item.TextAlign == "Left") { g.DrawString(item.Text, CurrentFont, Brushes.Black, item.Left, item.Top); } if (item.TextAlign == "Center") { if (item.Height == 0) item.Height = MeasureString(item.Text, CurrentFont, GraphicsUnit.Millimeter).Height; StringFormat sf = new StringFormat(); sf.Alignment = StringAlignment.Center; sf.LineAlignment = StringAlignment.Center; RectangleF rf = new RectangleF(item.Left, item.Top, item.Width, item.Height); g.DrawString(item.Text, CurrentFont, Brushes.Black, rf, sf); } if (item.TextAlign == "Right") { StringFormat sf = new StringFormat(); sf.Alignment = StringAlignment.Near; sf.LineAlignment = StringAlignment.Center; RectangleF rf = new RectangleF(item.Left, item.Top, item.Width, item.Height); g.DrawString(item.Text, CurrentFont, Brushes.Black, rf, sf); } } } void DrawLines(Graphics g) { g.PageUnit = GraphicsUnit.Point; foreach (Line lin in Report.PageLines) { g.DrawLine(new Pen(Brushes.Black, lin.BrushWidth), lin.Pt1, lin.Pt2); } g.PageUnit = GraphicsUnit.Millimeter; } void DrawImages(Graphics g) { //g.PageUnit = GraphicsUnit.Pixel; foreach (ImageObject img in Report.PageImageObjects) { if (img.IsZoom == false) { g.DrawImage(img.MemoryImage, img.Left, img.Top); } else { g.DrawImage(img.MemoryImage, img.Left, img.Top, img.Width, img.Height); } } //g.PageUnit = GraphicsUnit.Millimeter; } void DrawRectangles(Graphics g) { g.PageUnit = GraphicsUnit.Point; foreach (Rectangle gx in Report.PageRectangles) { g.DrawRectangle(new Pen(Brushes.Black, gx.BrushWidth), gx.X, gx.Y, gx.Width, gx.Height); } g.PageUnit = GraphicsUnit.Millimeter; } float DrawHeader(Graphics g, float dpix, float dpiy) { float x = dpix; float y = dpiy; StringFormat SF = null; Font CurrentFont = this.Report.Font; foreach (PageRow row in Report.Header.Rows) { if (row.Text == "") { y = y + row.Height; continue; } CurrentFont = this.Report.Font; if (row.Font != null) CurrentFont = row.Font; SF = new StringFormat(); SF.LineAlignment = row.LineAlignment; SF.Alignment = row.Alignment; SF.FormatFlags = row.FormatFlags; if (this.Kind.Landscape == false) { // g.DrawString(row.Text, CurrentFont, Brushes.Black, new RectangleF(x, y, Convert.ToSingle(this.Kind.Width-Report.Left-Report.Right), Convert.ToSingle(RowHeight)), SF); float cellWidth = Convert.ToSingle(this.Kind.Width - Report.Left - Report.Right); float cellHeight = row.Height; this.DrawString(g, CurrentFont, x, y, cellWidth, cellHeight, SF, row.Text, cellWidth, row.Height); } else { // g.DrawString(row.Text, CurrentFont, Brushes.Black, new RectangleF(x, y, Convert.ToSingle(this.Kind.Height-Report.Left-Report.Right), Convert.ToSingle(RowHeight)), SF); float cellWidth = Convert.ToSingle(this.Kind.Height - Report.Left - Report.Right); float cellHeight = row.Height; this.DrawString(g, CurrentFont, x, y, cellWidth, cellHeight, SF, row.Text, cellWidth, row.Height); } y = y + row.Height; } if (Report.Header.TableItem.Rows.Count > 0) y = y + Report.TableRowHeightReviseNumber; foreach (TableRow row in Report.Header.TableItem.Rows) { int rowIndex = Report.Header.TableItem.Rows.IndexOf(row); for (int i = 0; i < row.Cells.Count; i++) { TableCell cell = row.Cells[i]; if (row.Cells[i].Visible == true) { float cellWidth = 0, cellHeight = 0; if (cell.RowSpan > 1) { for (int k = 0; k < cell.RowSpan; k++) { cellHeight = cellHeight + Report.Header.TableItem.Rows[rowIndex + k].Height * Report.Header.TableItem.Rows[rowIndex + k].RowSpan + Report.TableRowHeightReviseNumber; } } else { cellHeight = row.Height * row.RowSpan + Report.TableRowHeightReviseNumber; } if (cell.ColumnSpan > 1) { for (int k = 0; k < cell.ColumnSpan; k++) { cellWidth = cellWidth + Report.Header.TableItem.Columns[i + k].Width; } } else { cellWidth = Report.Header.TableItem.Columns[i].Width; } if (Report.Header.TableItem.ShowLine == true) g.DrawRectangle(new Pen(Color.Black, cell.BorderWidth), x, y, cellWidth, cellHeight); SF = new StringFormat(); SF.LineAlignment = cell.LineAlignment; SF.Alignment = cell.Alignment; SF.FormatFlags = cell.FormatFlags; CurrentFont = this.Report.Font; if (cell.Font != null) CurrentFont = cell.Font; // g.DrawString(cell.Text, CurrentFont, Brushes.Black, new RectangleF(x, y, cellWidth, cellHeight), SF); this.DrawString(g, CurrentFont, x, y, cellWidth, cellHeight, SF, cell.Text, cellWidth, row.Height); } x = x + Convert.ToSingle(Report.Header.TableItem.Columns[i].Width); } y = y + row.Height * row.RowSpan + Report.TableRowHeightReviseNumber; x = dpix; } return y; } void DrawFooter(Graphics g, float dpix, float dpiy) { float x = dpix; float y = dpiy; StringFormat SF = null; Font CurrentFont = this.Report.Font; foreach (TableRow row in Report.Footer.TableItem.Rows) { int rowIndex = Report.Footer.TableItem.Rows.IndexOf(row); for (int i = 0; i < row.Cells.Count; i++) { TableCell cell = row.Cells[i]; if (row.Cells[i].Visible == true) { float cellWidth = 0, cellHeight = 0; if (cell.RowSpan > 1) { for (int k = 0; k < cell.RowSpan; k++) { cellHeight = cellHeight + Report.Footer.TableItem.Rows[rowIndex + k].Height * Report.Footer.TableItem.Rows[rowIndex + k].RowSpan + Report.TableRowHeightReviseNumber; } } else { cellHeight = row.Height * row.RowSpan + Report.TableRowHeightReviseNumber; } if (cell.ColumnSpan > 1) { for (int k = 0; k < cell.ColumnSpan; k++) { cellWidth = cellWidth + Report.Footer.TableItem.Columns[i + k].Width; } } else { cellWidth = Report.Footer.TableItem.Columns[i].Width; } if (Report.Footer.TableItem.ShowLine == true) g.DrawRectangle(new Pen(Color.Black, cell.BorderWidth), x, y, cellWidth, cellHeight); SF = new StringFormat(); SF.LineAlignment = cell.LineAlignment; SF.Alignment = cell.Alignment; SF.FormatFlags = cell.FormatFlags; CurrentFont = this.Report.Font; if (cell.Font != null) CurrentFont = cell.Font; // g.DrawString(cell.Text, CurrentFont, Brushes.Black, new RectangleF(x, y, cellWidth, cellHeight), SF); this.DrawString(g, CurrentFont, x, y, cellWidth, cellHeight, SF, cell.Text, cellWidth, row.Height); } x = x + Convert.ToSingle(Report.Footer.TableItem.Columns[i].Width); } y = y + row.Height * row.RowSpan + Report.TableRowHeightReviseNumber; x = dpix; } if (Report.Footer.TableItem.Rows.Count > 0) y = y + Report.TableRowHeightReviseNumber; foreach (PageRow row in Report.Footer.Rows) { if (row.Text == "") { y = y + row.Height; continue; } CurrentFont = this.Report.Font; if (row.Font != null) CurrentFont = row.Font; SF = new StringFormat(); SF.LineAlignment = row.LineAlignment; SF.Alignment = row.Alignment; SF.FormatFlags = row.FormatFlags; if (this.Kind.Landscape == false) { // g.DrawString(row.Text, CurrentFont, Brushes.Black, new RectangleF(x, y, Convert.ToSingle(this.Kind.Width-Report.Left-Report.Right), Convert.ToSingle(RowHeight)),SF); float cellWidth = Convert.ToSingle(this.Kind.Width - Report.Left - Report.Right); float cellHeight = row.Height; this.DrawString(g, CurrentFont, x, y, cellWidth, cellHeight, SF, row.Text, cellWidth, row.Height); } else { // g.DrawString(row.Text, CurrentFont, Brushes.Black, new RectangleF(x, y, Convert.ToSingle(this.Kind.Height-Report.Left-Report.Right), Convert.ToSingle(RowHeight)),SF); float cellWidth = Convert.ToSingle(this.Kind.Height - Report.Left - Report.Right); float cellHeight = row.Height; this.DrawString(g, CurrentFont, x, y, cellWidth, cellHeight, SF, row.Text, cellWidth, row.Height); } y = y + row.Height; } if (this.Report.ShowPagerNumber == true) { SF = new StringFormat(); SF.LineAlignment = StringAlignment.Center; SF.Alignment = StringAlignment.Center; string PagerNumberString = "第" + Convert.ToString(this.CurrentPageNumber + 1) + "页,共" + this.PageCount + "页"; float PagerNumberHeight = Report.MeasureString(PagerNumberString, this.Report.Font).Height; if (this.Kind.Landscape == false) { g.DrawString(PagerNumberString, this.Report.Font, Brushes.Black, new RectangleF(x, y, Convert.ToSingle(this.Kind.Width - Report.Left - Report.Right), PagerNumberHeight), SF); } else { g.DrawString(PagerNumberString, this.Report.Font, Brushes.Black, new RectangleF(x, y, Convert.ToSingle(this.Kind.Height - Report.Left - Report.Right), PagerNumberHeight), SF); } } } float DrawGroups(Graphics g, int startIndex, float dpix, float dpiy) { float x = dpix; float y = dpiy; StringFormat SF = null; Font CurrentFont = this.Report.Font; //左表 foreach (TableRow row in Report.Group.LeftItem.Rows) { int rowIndex = Report.Group.LeftItem.Rows.IndexOf(row); for (int i = 0; i < row.Cells.Count; i++) { TableCell cell = row.Cells[i]; if (row.Cells[i].Visible == true) { float cellWidth = 0, cellHeight = 0; if (cell.RowSpan > 1) { for (int k = 0; k < cell.RowSpan; k++) { cellHeight = cellHeight + Report.Group.LeftItem.Rows[rowIndex + k].Height * Report.Group.LeftItem.Rows[rowIndex + k].RowSpan + Report.TableRowHeightReviseNumber; } } else { cellHeight = row.Height * row.RowSpan + Report.TableRowHeightReviseNumber; } if (cell.ColumnSpan > 1) { for (int k = 0; k < cell.ColumnSpan; k++) { cellWidth = cellWidth + Report.Group.LeftItem.Columns[i + k].Width; } } else { cellWidth = Report.Group.LeftItem.Columns[i].Width; } if (Report.Group.LeftItem.ShowLine == true) g.DrawRectangle(new Pen(Color.Black, cell.BorderWidth), x, y, cellWidth, cellHeight); SF = new StringFormat(); SF.LineAlignment = cell.LineAlignment; SF.Alignment = cell.Alignment; SF.FormatFlags = cell.FormatFlags; CurrentFont = this.Report.Font; if (cell.Font != null) CurrentFont = cell.Font; // g.DrawString(cell.Text, CurrentFont, Brushes.Black, new RectangleF(x, y, cellWidth, cellHeight), SF); this.DrawString(g, CurrentFont, x, y, cellWidth, cellHeight, SF, cell.Text, cellWidth, row.Height); } x = x + Convert.ToSingle(Report.Group.LeftItem.Columns[i].Width); } y = y + row.Height * row.RowSpan + Report.TableRowHeightReviseNumber; x = dpix; } if (Report.Group.Header.Rows.Count > 0 || Report.Group.DataItem.Rows.Count > 0 || Report.Group.Footer.Rows.Count > 0) { x = dpix + Report.Group.LeftItem.Width; y = dpiy; } //多列表头 foreach (TableRow row in Report.Group.Header.Rows) { int rowIndex = Report.Group.Header.Rows.IndexOf(row); for (int i = 0; i < row.Cells.Count; i++) { TableCell cell = row.Cells[i]; if (row.Cells[i].Visible == true) { float cellWidth = 0, cellHeight = 0; if (cell.RowSpan > 1) { for (int k = 0; k < cell.RowSpan; k++) { cellHeight = cellHeight + Report.Group.Header.Rows[rowIndex + k].Height * Report.Group.Header.Rows[rowIndex + k].RowSpan + this.Report.TableRowHeightReviseNumber; } } else { cellHeight = row.Height * row.RowSpan + this.Report.TableRowHeightReviseNumber; } if (cell.ColumnSpan > 1) { for (int k = 0; k < cell.ColumnSpan; k++) { cellWidth = cellWidth + Report.Group.Header.Columns[i + k].Width; } } else { cellWidth = Report.Group.Header.Columns[i].Width; } if (Report.Group.Header.ShowLine == true) g.DrawRectangle(new Pen(Color.Black, cell.BorderWidth), x, y, cellWidth, cellHeight); SF = new StringFormat(); SF.LineAlignment = cell.LineAlignment; SF.Alignment = cell.Alignment; SF.FormatFlags = cell.FormatFlags; CurrentFont = this.Report.Font; if (cell.Font != null) CurrentFont = cell.Font; // g.DrawString(cell.Text, CurrentFont, Brushes.Black, new RectangleF(x, y, cellWidth, cellHeight), SF); this.DrawString(g, CurrentFont, x, y, cellWidth, cellHeight, SF, cell.Text, cellWidth, row.Height); } x = x + Convert.ToSingle(Report.Group.Header.Columns[i].Width); } y = y + row.Height * row.RowSpan + Report.TableRowHeightReviseNumber; x = dpix + Report.Group.LeftItem.Width; } //表项目 if (Report.Group.DataItem.Rows.Count > 0) { if (this.Report.AutoMathPageRecordNumber == true) { for (int j = startIndex; j < Report.Group.DataItem.Rows.Count; j++) { //计算行最大高度 float maxRowHeight = Report.Group.DataItem.Rows[j].Height * Report.Group.DataItem.Rows[j].RowSpan + this.Report.TableRowHeightReviseNumber; for (int i = 0; i < Report.Group.DataItem.Columns.Count; i++) { // TableCell cell = Report.Group.DataItem.Rows[j].Cells[i]; if (Report.Group.DataItem.Rows[j].Cells[i].Visible == false) continue; float cellRowHeight = 0; if (Report.Group.DataItem.Rows[j].Cells[i].RowSpan > 1) { for (int k = 0; k < Report.Group.DataItem.Rows[j].Cells[i].RowSpan; k++) { // cellRowHeight = cellRowHeight + (Report.Group.DataItem.Rows[j + k].Height+Report.TableRowHeightReviseNumber)*Report.Group.DataItem.Rows[j + k].RowSpan; cellRowHeight = cellRowHeight + Report.Group.DataItem.Rows[j + k].Height * Report.Group.DataItem.Rows[j + k].RowSpan + this.Report.TableRowHeightReviseNumber; } } if (cellRowHeight > maxRowHeight) maxRowHeight = cellRowHeight; } if (y + maxRowHeight + this.Report.Group.Footer.Height > this.Report.Group.Height + this.Report.Header.Height + this.Report.Top) { this.startIndex = j; break; }//如果已画高度+当前所需画的高度超出预计,那么退出此循环 for (int i = 0; i < Report.Group.DataItem.Columns.Count; i++) { TableCell cell = Report.Group.DataItem.Rows[j].Cells[i]; if (Report.Group.DataItem.Rows[j].Cells[i].Visible == true) { float cellWidth = 0, cellHeight = 0; if (cell.RowSpan > 1) { for (int k = 0; k < cell.RowSpan; k++) { //cellHeight = cellHeight + (Report.Group.DataItem.Rows[j + k].Height+Report.TableRowHeightReviseNumber)*Report.Group.DataItem.Rows[j + k].RowSpan; cellHeight = cellHeight + Report.Group.DataItem.Rows[j + k].Height * Report.Group.DataItem.Rows[j + k].RowSpan + this.Report.TableRowHeightReviseNumber; } } else { cellHeight = Report.Group.DataItem.Rows[j].Height * Report.Group.DataItem.Rows[j].RowSpan + this.Report.TableRowHeightReviseNumber; } if (cell.ColumnSpan > 1) { for (int k = 0; k < cell.ColumnSpan; k++) { cellWidth = Report.Group.DataItem.Columns[i + k].Width; } } else { cellWidth = Report.Group.DataItem.Columns[i].Width; } if (Report.Group.DataItem.ShowLine == true) g.DrawRectangle(new Pen(Color.Black, cell.BorderWidth), x, y, cellWidth, cellHeight); SF = new StringFormat(); SF.LineAlignment = cell.LineAlignment; SF.Alignment = cell.Alignment; SF.FormatFlags = cell.FormatFlags; CurrentFont = this.Report.Font; if (cell.Font != null) CurrentFont = cell.Font; // g.DrawString(cell.Text, CurrentFont, Brushes.Black, new RectangleF(x, y, cellWidth, cellHeight), SF); this.DrawString(g, CurrentFont, x, y, cellWidth, cellHeight, SF, cell.Text, cellWidth, Report.Group.DataItem.Rows[j].Height); } x = x + Report.Group.DataItem.Columns[i].Width; } y = y + Report.Group.DataItem.Rows[j].Height * Report.Group.DataItem.Rows[j].RowSpan + this.Report.TableRowHeightReviseNumber; x = dpix + Report.Group.LeftItem.Width; //填充空行 if (j == this.Report.Group.DataItem.Rows.Count - 1) { this.startIndex = j; //填充空行 if (this.IsFillBlank == true) { while (true) { if (y + this.Report.Group.Footer.Height > this.Report.Group.Height + this.Report.Header.Height + this.Report.Top) break;//如果已画高度+当前所需画的高度超出预计,那么退出此循环 for (int i = 0; i < Report.Group.DataItem.Columns.Count; i++) { g.DrawRectangle(new Pen(Color.Black, Report.Group.DataItem.Rows[j].Cells[i].BorderWidth), x, y, Report.Group.DataItem.Columns[i].Width, Report.Group.DataItem.Rows[j].Height + Report.TableRowHeightReviseNumber); x = x + Report.Group.DataItem.Columns[i].Width; } y = y + Report.Group.DataItem.Rows[j].Height + Report.TableRowHeightReviseNumber; x = dpix + Report.Group.LeftItem.Width; } } } } } else { //按设定行数循环 if (startIndex + Report.PageRecordNumber > this.Report.Group.DataItem.Rows.Count && this.IsFillBlank == true) { int modSl = Report.Group.DataItem.Rows.Count % Report.PageRecordNumber; //取得余数 TableRow row = null; if (modSl != 0) { for (int ii = 0; ii < Report.PageRecordNumber - modSl; ii++) { row = Report.Group.DataItem.NewRow(); row.Height = Report.Group.DataItem.Rows[Report.Group.DataItem.Rows.Count - 1].Height; Report.Group.DataItem.Rows.Add(row); } } } for (int j = startIndex; j < startIndex + Report.PageRecordNumber; j++) { for (int i = 0; i < Report.Group.DataItem.Columns.Count; i++) { TableCell cell = Report.Group.DataItem.Rows[j].Cells[i]; if (Report.Group.DataItem.Rows[j].Cells[i].Visible == true) { float cellWidth = 0, cellHeight = 0; if (cell.RowSpan > 1) { for (int k = 0; k < cell.RowSpan; k++) { cellHeight = cellHeight + Report.Group.DataItem.Rows[j + k].Height * Report.Group.DataItem.Rows[j + k].RowSpan + Report.TableRowHeightReviseNumber; } } else { cellHeight = Report.Group.DataItem.Rows[j].Height * Report.Group.DataItem.Rows[j].RowSpan + Report.TableRowHeightReviseNumber; } if (cell.ColumnSpan > 1) { for (int k = 0; k < cell.ColumnSpan; k++) { cellWidth = Report.Group.DataItem.Columns[i + k].Width; } } else { cellWidth = Report.Group.DataItem.Columns[i].Width; } if (Report.Group.DataItem.ShowLine == true) g.DrawRectangle(new Pen(Color.Black, cell.BorderWidth), x, y, cellWidth, cellHeight); SF = new StringFormat(); SF.LineAlignment = cell.LineAlignment; SF.Alignment = cell.Alignment; SF.FormatFlags = cell.FormatFlags; CurrentFont = this.Report.Font; if (cell.Font != null) CurrentFont = cell.Font; // g.DrawString(cell.Text, CurrentFont, Brushes.Black, new RectangleF(x, y, cellWidth, cellHeight), SF); this.DrawString(g, CurrentFont, x, y, cellWidth, cellHeight, SF, cell.Text, cellWidth, Report.Group.DataItem.Rows[j].Height); } x = x + Report.Group.DataItem.Columns[i].Width; } y = y + Report.Group.DataItem.Rows[j].Height * Report.Group.DataItem.Rows[j].RowSpan + Report.TableRowHeightReviseNumber; x = dpix + Report.Group.LeftItem.Width; } } } //多列表尾 foreach (TableRow row in Report.Group.Footer.Rows) { int rowIndex = Report.Group.Footer.Rows.IndexOf(row); for (int i = 0; i < row.Cells.Count; i++) { TableCell cell = row.Cells[i]; if (row.Cells[i].Visible == true) { float cellWidth = 0, cellHeight = 0; if (cell.RowSpan > 1) { for (int k = 0; k < cell.RowSpan; k++) { cellHeight = cellHeight + Report.Group.Footer.Rows[rowIndex + k].Height * Report.Group.Footer.Rows[rowIndex + k].RowSpan + Report.TableRowHeightReviseNumber; } } else { cellHeight = Report.Group.Footer.Rows[rowIndex].Height * Report.Group.Footer.Rows[rowIndex].RowSpan + Report.TableRowHeightReviseNumber; } if (cell.ColumnSpan > 1) { for (int k = 0; k < cell.ColumnSpan; k++) { cellWidth = cellWidth + Report.Group.Header.Columns[i + k].Width; } } else { cellWidth = Report.Group.Header.Columns[i].Width; } if (Report.Group.Footer.ShowLine == true) g.DrawRectangle(new Pen(Color.Black, cell.BorderWidth), x, y, cellWidth, cellHeight); SF = new StringFormat(); SF.LineAlignment = cell.LineAlignment; SF.Alignment = cell.Alignment; SF.FormatFlags = cell.FormatFlags; CurrentFont = this.Report.Font; if (cell.Font != null) CurrentFont = cell.Font; // g.DrawString(cell.Text, CurrentFont, Brushes.Black, new RectangleF(x, y, cellWidth, cellHeight),SF); this.DrawString(g, CurrentFont, x, y, cellWidth, cellHeight, SF, cell.Text, cellWidth, row.Height); } x = x + Convert.ToSingle(Report.Group.Header.Columns[i].Width); } y = y + row.Height * row.RowSpan + Report.TableRowHeightReviseNumber; x = dpix + Report.Group.LeftItem.Width; } #region 右表 //右表 if (Report.Group.RightItem.Rows.Count > 0) { x = dpix + Report.Group.LeftItem.Width + (Report.Group.Width - Report.Group.LeftItem.Width - Report.Group.RightItem.Width); y = dpiy; } foreach (TableRow row in Report.Group.RightItem.Rows) { int rowIndex = Report.Group.RightItem.Rows.IndexOf(row); for (int i = 0; i < row.Cells.Count; i++) { TableCell cell = row.Cells[i]; if (row.Cells[i].Visible == true) { float cellWidth = 0, cellHeight = 0; if (cell.RowSpan > 1) { for (int k = 0; k < cell.RowSpan; k++) { cellHeight = cellHeight + Report.Group.RightItem.Rows[rowIndex + k].Height * Report.Group.RightItem.Rows[rowIndex + k].RowSpan + Report.TableRowHeightReviseNumber; } } else { cellHeight = row.Height * row.RowSpan + Report.TableRowHeightReviseNumber; } if (cell.ColumnSpan > 1) { for (int k = 0; k < cell.ColumnSpan; k++) { cellWidth = cellWidth + Report.Group.RightItem.Columns[i + k].Width; } } else { cellWidth = Report.Group.RightItem.Columns[i].Width; } if (Report.Group.RightItem.ShowLine == true) g.DrawRectangle(new Pen(Color.Black, cell.BorderWidth), x, y, cellWidth, cellHeight); SF = new StringFormat(); SF.LineAlignment = cell.LineAlignment; SF.Alignment = cell.Alignment; SF.FormatFlags = cell.FormatFlags; CurrentFont = this.Report.Font; if (cell.Font != null) CurrentFont = cell.Font; g.DrawString(cell.Text, CurrentFont, Brushes.Black, new RectangleF(x, y, cellWidth, cellHeight), SF); } x = x + Convert.ToSingle(Report.Group.RightItem.Columns[i].Width); } y = y + row.Height * row.RowSpan + Report.TableRowHeightReviseNumber; x = dpix + Report.Group.LeftItem.Width + (Report.Group.Width - Report.Group.LeftItem.Width - Report.Group.RightItem.Width); } #endregion return y; } private void DrawString(Graphics g, Font CurrentFont, float dpix, float dpiy, float width, float height, StringFormat sf, string value, float maxwidth, float RowHeight) { if (Regex.IsMatch(value, "||
", RegexOptions.IgnoreCase) == false) { g.DrawString(value, CurrentFont, Brushes.Black, new RectangleF(dpix, dpiy, width, height), sf); } else { float CurrentWidth = 0, x = dpix, y = dpiy + 0.5f; value = Regex.Replace(value, "", "♂↓", RegexOptions.IgnoreCase); value = Regex.Replace(value, "", "♂↑", RegexOptions.IgnoreCase); value = Regex.Replace(value, "", "♂", RegexOptions.IgnoreCase); value = Regex.Replace(value, "
", "♂", RegexOptions.IgnoreCase); value = Regex.Replace(value, "
", "〓", RegexOptions.IgnoreCase); string[] ss = value.Split(Convert.ToChar("♂")); foreach (string s in ss) { if (s.Length == 0) continue; string flag = s.Substring(0, 1); switch (flag) { case "↑": foreach (char c in s) { if (c.ToString() == "↑") continue; Font f = new Font("宋体", CurrentFont.Size * 0.5f, GraphicsUnit.Millimeter); g.DrawString(c.ToString(), f, Brushes.Black, x, y); float charWidth = this.MeasureString(c.ToString(), f).Width; //float charWidth2 = this.MeasureString(c.ToString() + c.ToString(), CurrentFont).Width; //charWidth = charWidth - (charWidth * 2 - charWidth2); x = x + charWidth; CurrentWidth = CurrentWidth + charWidth; if (CurrentWidth >= maxwidth - charWidth) { CurrentWidth = 0; x = dpix; y = y + RowHeight; } } continue; case "↓": foreach (char c in s) { if (c.ToString() == "↓") continue; Font f = new Font("宋体", CurrentFont.Size * 0.5f, GraphicsUnit.Millimeter); g.DrawString(c.ToString(), f, Brushes.Black, x, y + RowHeight * 0.5f); float charWidth = this.MeasureString(c.ToString(), f).Width; //float charWidth2 = this.MeasureString(c.ToString() + c.ToString(), CurrentFont).Width; //charWidth = charWidth - (charWidth * 2 - charWidth2); x = x + charWidth; CurrentWidth = CurrentWidth + charWidth; if (CurrentWidth >= maxwidth - charWidth) { CurrentWidth = 0; x = dpix; y = y + RowHeight; } } continue; default: float charHeight = this.MeasureString("测试高度", CurrentFont).Height; foreach (char c in s) { if (c.ToString() == "〓") { CurrentWidth = 0; x = dpix; y = y + charHeight; continue; } g.DrawString(c.ToString(), CurrentFont, Brushes.Black, x, y); float charWidth = this.MeasureString(c.ToString(), CurrentFont).Width; float charWidth2 = this.MeasureString(c.ToString() + c.ToString(), CurrentFont).Width; if (c.ToString().Trim() != "") charWidth = charWidth - (charWidth * 2 - charWidth2); x = x + charWidth; CurrentWidth = CurrentWidth + charWidth; if (CurrentWidth >= maxwidth) { CurrentWidth = 0; x = dpix; y = y + charHeight; } } break; } } } } public void Print() { if (m_Document.PrinterSettings.IsValid == false) { MessageBox.Show("打印机没有安装,请先准备好打印机"); return; } m_Document.Print(); } private void m_Document_EndPrint(object sender, PrintEventArgs e) { if (this.EndPrint != null) this.EndPrint(this, null); } } #endregion #region 报表打印_多页打印 public class PrintMuchReport { public List Report { get; set; } public PrintDocument Document { get; set; } public Kind Kind { get; set; } public Int16 Copies { get; set; } //逐份打印 public bool Collate { get; set; } int CurrentPageNumber = 0; //当前页号 int StopPageNumber = 0; //结束页号 public int GetPagerCount() { return StopPageNumber; } public event EventHandler EndPrint; public PrintMuchReport(List report) { Copies = 1; Document = new PrintDocument(); Document.DocumentName = report.Count.ToString(); Document.PrintPage += new PrintPageEventHandler(pd_PrintPage); Document.BeginPrint += new PrintEventHandler(m_Document_BeginPrint); Document.QueryPageSettings += new QueryPageSettingsEventHandler(m_Document_QueryPageSettings); Document.EndPrint += new PrintEventHandler(m_Document_EndPrint); this.Report = report; this.Kind = this.Report[0].PagerKind[0]; this.StopPageNumber = report.Count; //SetPageRecordNumber(Convert.ToInt32(value.Width), Convert.ToInt32(value.Height)); } private void MathRowHeight(Table tb) { if (tb.Rows.Count == 0) return; Font CurrentFont = this.Report[0].Font; foreach (TableRow row in tb.Rows) { if (row.Height > MeasureString("测试高度", CurrentFont).Height) continue; int RowNumber = 1; float WidthCount = 0; foreach (TableColumn col in tb.Columns) { int index = tb.Columns.IndexOf(col); if (row.Cells[index].Font != null) CurrentFont = row.Cells[index].Font; string value1 = Regex.Replace(row.Cells[index].Text, "", "", RegexOptions.IgnoreCase); value1 = Regex.Replace(value1, "", "", RegexOptions.IgnoreCase); value1 = Regex.Replace(value1, "", "", RegexOptions.IgnoreCase); value1 = Regex.Replace(value1, "", "", RegexOptions.IgnoreCase); if (Regex.IsMatch(value1, "
", RegexOptions.IgnoreCase) == false) { WidthCount = MeasureString(value1, CurrentFont).Width; int CurrentRowNumber = 1; try { CurrentRowNumber = Convert.ToInt32(Math.Ceiling(WidthCount / (col.Width * row.Cells[index].ColumnSpan))); } catch { } if (CurrentRowNumber == 0) CurrentRowNumber = 1; if (CurrentRowNumber > RowNumber) RowNumber = CurrentRowNumber; } else { int MaxRowNumber = 0; string[] ss = Regex.Split(value1, "
", RegexOptions.IgnoreCase); foreach (string s in ss) { WidthCount = MeasureString(s, CurrentFont).Width; int CurrentRowNumber = 1; try { CurrentRowNumber = Convert.ToInt32(Math.Ceiling(WidthCount / col.Width * row.Cells[index].ColumnSpan)); } catch { } if (CurrentRowNumber == 0) CurrentRowNumber = 1; MaxRowNumber = MaxRowNumber + CurrentRowNumber; } if (MaxRowNumber > RowNumber) RowNumber = MaxRowNumber; } } row.RowSpan = RowNumber; row.Height = MeasureString("测试高度", CurrentFont).Height; } } private void MathRowHeight(PageRows Rows) { Font CurrentFont = this.Report[0].Font; float WidthCount = 0; foreach (PageRow row in Rows) { if (row.Font != null) CurrentFont = row.Font; row.Height = 0; float Height = Convert.ToSingle(MeasureString(row.Text, CurrentFont).Height); float charHeight = Convert.ToSingle(MeasureString("测试高度", CurrentFont).Height); if (Height == 0) { row.Height = charHeight; continue; } float cellWidth = Convert.ToSingle(this.Report[0].Width - Report[0].Left - Report[0].Right); string value1 = Regex.Replace(row.Text, "", "", RegexOptions.IgnoreCase); value1 = Regex.Replace(value1, "", "", RegexOptions.IgnoreCase); value1 = Regex.Replace(value1, "", "", RegexOptions.IgnoreCase); value1 = Regex.Replace(value1, "", "", RegexOptions.IgnoreCase); if (Regex.IsMatch(value1, "
", RegexOptions.IgnoreCase) == false) { WidthCount = MeasureString(value1, CurrentFont).Width; int CurrentRowNumber = 1; try { CurrentRowNumber = Convert.ToInt32(Math.Ceiling(WidthCount / cellWidth)); } catch { } if (CurrentRowNumber == 0) CurrentRowNumber = 1; row.Height = Height * CurrentRowNumber; } else { string[] ss = Regex.Split(value1, "
", RegexOptions.IgnoreCase); foreach (string s in ss) { WidthCount = MeasureString(s, CurrentFont).Width; int CurrentRowNumber = 1; try { CurrentRowNumber = Convert.ToInt32(Math.Ceiling(WidthCount / cellWidth)); } catch { } if (CurrentRowNumber == 0) CurrentRowNumber = 1; row.Height = row.Height + Height * CurrentRowNumber; } } } } public SizeF MeasureString(string Text, Font font) { Bitmap bmp = new Bitmap(100, 100); Graphics g = Graphics.FromImage(bmp); g.PageUnit = font.Unit; SizeF s = g.MeasureString(Text, font); return s; } public SizeF MeasureString(string Text, Font font, GraphicsUnit unit) { Bitmap bmp = new Bitmap(100, 100); Graphics g = Graphics.FromImage(bmp); g.PageUnit = unit; SizeF s = g.MeasureString(Text, font); return s; } private void m_Document_QueryPageSettings(object sender, QueryPageSettingsEventArgs e) { e.PageSettings.Margins = new Margins(0, 0, 0, 0); //e.PageSettings.PrinterSettings.DefaultPageSettings.Margins = e.PageSettings.Margins; e.PageSettings.PrinterSettings.Copies = this.Copies; e.PageSettings.PrinterSettings.Collate = this.Collate; if (this.Report[0].RotatePoint > 0) { e.PageSettings.Landscape = true; } else { e.PageSettings.Landscape = this.Kind.Landscape; } //e.PageSettings.PrinterSettings.DefaultPageSettings.Landscape = e.PageSettings.Landscape; PaperSize CustomPaperSize = null; PaperSize PaperSizeItem = null; foreach (PaperSize item in e.PageSettings.PrinterSettings.PaperSizes) { if (item.PaperName == this.Kind.Name && item.Kind != PaperKind.Custom) { PaperSizeItem = item; break; } if (item.Kind == PaperKind.Custom) CustomPaperSize = item; } if (PaperSizeItem != null) { e.PageSettings.PaperSize = PaperSizeItem; } else { if (CustomPaperSize != null) { e.PageSettings.PaperSize = new PaperSize(CustomPaperSize.PaperName, System.Drawing.Printing.PrinterUnitConvert.Convert(this.Kind.Width * 10, PrinterUnit.TenthsOfAMillimeter, PrinterUnit.Display), System.Drawing.Printing.PrinterUnitConvert.Convert(this.Kind.Height * 10, PrinterUnit.TenthsOfAMillimeter, PrinterUnit.Display)); e.PageSettings.PaperSize.RawKind = CustomPaperSize.RawKind; } } //e.PageSettings.PrinterSettings.DefaultPageSettings.PaperSize = e.PageSettings.PaperSize; if (Report[0].PageLayout == PageLayoutType.GridLayout) e.PageSettings.PrinterSettings.PrintRange = PrintRange.SomePages; e.PageSettings.PrinterSettings.FromPage = this.CurrentPageNumber; e.PageSettings.PrinterSettings.ToPage = this.StopPageNumber; } private void m_Document_BeginPrint(object sender, PrintEventArgs e) { this.CurrentPageNumber = 0; } private void pd_PrintPage(object sender, PrintPageEventArgs e) { Graphics g = e.Graphics; //RectangleF picRect = new RectangleF(10, 10, e.MarginBounds.Height, e.MarginBounds.Width); //float y=picRect.Y + picRect.Height / 2; //float x = picRect.X + picRect.Width / 2; //g.TranslateTransform(y / 2, y - picRect.Height / 2); ////旋转的角度 //g.RotateTransform(90.0F); MyReport report = this.Report[this.CurrentPageNumber]; if (report.RotatePoint > 0) { //旋转的角度 if (report.RotateX == 0) report.RotateX = e.MarginBounds.Width / 4; g.TranslateTransform(report.RotateX, report.RotateY); g.RotateTransform(report.RotatePoint); } g.PageUnit = GraphicsUnit.Millimeter; DrawStringObject(report, g); DrawLines(report, g); DrawRectangles(report, g); DrawImages(report, g); DrawHeader(report, g, report.Left, report.Top); CurrentPageNumber++; if (CurrentPageNumber < this.StopPageNumber) { e.HasMorePages = true; } else { e.HasMorePages = false; } } void DrawStringObject(MyReport report, Graphics g) { foreach (StringObject item in report.PageStringObjects) { Font CurrentFont = report.Font; if (item.Font != null) CurrentFont = item.Font; if (item.TextAlign == "Left") { g.DrawString(item.Text, CurrentFont, Brushes.Black, item.Left, item.Top); } if (item.TextAlign == "Center") { if (item.Height == 0) item.Height = MeasureString(item.Text, CurrentFont, GraphicsUnit.Millimeter).Height; StringFormat sf = new StringFormat(); sf.Alignment = StringAlignment.Center; sf.LineAlignment = StringAlignment.Center; RectangleF rf = new RectangleF(item.Left, item.Top, item.Width, item.Height); g.DrawString(item.Text, CurrentFont, Brushes.Black, rf, sf); } if (item.TextAlign == "Right") { StringFormat sf = new StringFormat(); sf.Alignment = StringAlignment.Near; sf.LineAlignment = StringAlignment.Center; RectangleF rf = new RectangleF(item.Left, item.Top, item.Width, item.Height); g.DrawString(item.Text, CurrentFont, Brushes.Black, rf, sf); } } } void DrawLines(MyReport report, Graphics g) { g.PageUnit = GraphicsUnit.Point; foreach (Line lin in report.PageLines) { g.DrawLine(new Pen(Brushes.Black, lin.BrushWidth), lin.Pt1, lin.Pt2); } g.PageUnit = GraphicsUnit.Millimeter; } void DrawImages(MyReport report, Graphics g) { //g.PageUnit = GraphicsUnit.Pixel; foreach (ImageObject img in report.PageImageObjects) { if (img.IsZoom == false) { g.DrawImage(img.MemoryImage, img.Left, img.Top); } else { g.DrawImage(img.MemoryImage, img.Left, img.Top, img.Width, img.Height); } } //g.PageUnit = GraphicsUnit.Millimeter; } void DrawRectangles(MyReport report, Graphics g) { g.PageUnit = GraphicsUnit.Point; foreach (Rectangle gx in report.PageRectangles) { g.DrawRectangle(new Pen(Brushes.Black, gx.BrushWidth), gx.X, gx.Y, gx.Width, gx.Height); } g.PageUnit = GraphicsUnit.Millimeter; } float DrawHeader(MyReport report, Graphics g, float dpix, float dpiy) { float x = dpix; float y = dpiy; StringFormat SF = null; Font CurrentFont = report.Font; foreach (PageRow row in report.Header.Rows) { if (row.Text == "") { y = y + row.Height; continue; } CurrentFont = report.Font; if (row.Font != null) CurrentFont = row.Font; SF = new StringFormat(); SF.LineAlignment = row.LineAlignment; SF.Alignment = row.Alignment; SF.FormatFlags = row.FormatFlags; if (this.Kind.Landscape == false) { // g.DrawString(row.Text, CurrentFont, Brushes.Black, new RectangleF(x, y, Convert.ToSingle(this.Kind.Width-Report.Left-Report.Right), Convert.ToSingle(RowHeight)), SF); float cellWidth = Convert.ToSingle(this.Kind.Width - report.Left - report.Right); float cellHeight = row.Height; this.DrawString(g, CurrentFont, x, y, cellWidth, cellHeight, SF, row.Text, cellWidth, row.Height); } else { // g.DrawString(row.Text, CurrentFont, Brushes.Black, new RectangleF(x, y, Convert.ToSingle(this.Kind.Height-Report.Left-Report.Right), Convert.ToSingle(RowHeight)), SF); float cellWidth = Convert.ToSingle(this.Kind.Height - report.Left - report.Right); float cellHeight = row.Height; this.DrawString(g, CurrentFont, x, y, cellWidth, cellHeight, SF, row.Text, cellWidth, row.Height); } y = y + row.Height; } if (report.Header.TableItem.Rows.Count > 0) y = y + report.TableRowHeightReviseNumber; foreach (TableRow row in report.Header.TableItem.Rows) { int rowIndex = report.Header.TableItem.Rows.IndexOf(row); for (int i = 0; i < row.Cells.Count; i++) { TableCell cell = row.Cells[i]; if (row.Cells[i].Visible == true) { float cellWidth = 0, cellHeight = 0; if (cell.RowSpan > 1) { for (int k = 0; k < cell.RowSpan; k++) { cellHeight = cellHeight + report.Header.TableItem.Rows[rowIndex + k].Height * report.Header.TableItem.Rows[rowIndex + k].RowSpan + report.TableRowHeightReviseNumber; } } else { cellHeight = row.Height * row.RowSpan + report.TableRowHeightReviseNumber; } if (cell.ColumnSpan > 1) { for (int k = 0; k < cell.ColumnSpan; k++) { cellWidth = cellWidth + report.Header.TableItem.Columns[i + k].Width; } } else { cellWidth = report.Header.TableItem.Columns[i].Width; } if (report.Header.TableItem.ShowLine == true) g.DrawRectangle(new Pen(Color.Black, cell.BorderWidth), x, y, cellWidth, cellHeight); SF = new StringFormat(); SF.LineAlignment = cell.LineAlignment; SF.Alignment = cell.Alignment; SF.FormatFlags = cell.FormatFlags; CurrentFont = report.Font; if (cell.Font != null) CurrentFont = cell.Font; // g.DrawString(cell.Text, CurrentFont, Brushes.Black, new RectangleF(x, y, cellWidth, cellHeight), SF); this.DrawString(g, CurrentFont, x, y, cellWidth, cellHeight, SF, cell.Text, cellWidth, row.Height); } x = x + Convert.ToSingle(report.Header.TableItem.Columns[i].Width); } y = y + row.Height * row.RowSpan + report.TableRowHeightReviseNumber; x = dpix; } return y; } private void DrawString(Graphics g, Font CurrentFont, float dpix, float dpiy, float width, float height, StringFormat sf, string value, float maxwidth, float RowHeight) { if (value == null) value = ""; if (Regex.IsMatch(value, "||
", RegexOptions.IgnoreCase) == false) { g.DrawString(value, CurrentFont, Brushes.Black, new RectangleF(dpix, dpiy, width, height), sf); } else { float CurrentWidth = 0, x = dpix, y = dpiy + 0.5f; value = Regex.Replace(value, "", "♂↓", RegexOptions.IgnoreCase); value = Regex.Replace(value, "", "♂↑", RegexOptions.IgnoreCase); value = Regex.Replace(value, "", "♂", RegexOptions.IgnoreCase); value = Regex.Replace(value, "
", "♂", RegexOptions.IgnoreCase); value = Regex.Replace(value, "
", "〓", RegexOptions.IgnoreCase); string[] ss = value.Split(Convert.ToChar("♂")); foreach (string s in ss) { if (s.Length == 0) continue; string flag = s.Substring(0, 1); switch (flag) { case "↑": foreach (char c in s) { if (c.ToString() == "↑") continue; Font f = new Font("宋体", CurrentFont.Size * 0.5f, GraphicsUnit.Millimeter); g.DrawString(c.ToString(), f, Brushes.Black, x, y); float charWidth = this.MeasureString(c.ToString(), f).Width; //float charWidth2 = this.MeasureString(c.ToString() + c.ToString(), CurrentFont).Width; //charWidth = charWidth - (charWidth * 2 - charWidth2); x = x + charWidth; CurrentWidth = CurrentWidth + charWidth; if (CurrentWidth >= maxwidth - charWidth) { CurrentWidth = 0; x = dpix; y = y + RowHeight; } } continue; case "↓": foreach (char c in s) { if (c.ToString() == "↓") continue; Font f = new Font("宋体", CurrentFont.Size * 0.5f, GraphicsUnit.Millimeter); g.DrawString(c.ToString(), f, Brushes.Black, x, y + RowHeight * 0.5f); float charWidth = this.MeasureString(c.ToString(), f).Width; //float charWidth2 = this.MeasureString(c.ToString() + c.ToString(), CurrentFont).Width; //charWidth = charWidth - (charWidth * 2 - charWidth2); x = x + charWidth; CurrentWidth = CurrentWidth + charWidth; if (CurrentWidth >= maxwidth - charWidth) { CurrentWidth = 0; x = dpix; y = y + RowHeight; } } continue; default: float charHeight = this.MeasureString("测试高度", CurrentFont).Height; foreach (char c in s) { if (c.ToString() == "〓") { CurrentWidth = 0; x = dpix; y = y + charHeight; continue; } g.DrawString(c.ToString(), CurrentFont, Brushes.Black, x, y); float charWidth = this.MeasureString(c.ToString(), CurrentFont).Width; float charWidth2 = this.MeasureString(c.ToString() + c.ToString(), CurrentFont).Width; if (c.ToString().Trim() != "") charWidth = charWidth - (charWidth * 2 - charWidth2); x = x + charWidth; CurrentWidth = CurrentWidth + charWidth; if (CurrentWidth >= maxwidth) { CurrentWidth = 0; x = dpix; y = y + charHeight; } } break; } } } } public void Print() { if (Document.PrinterSettings.IsValid == false) { MessageBox.Show("打印机没有安装,请先准备好打印机"); return; } Document.Print(); } private void m_Document_EndPrint(object sender, PrintEventArgs e) { if (this.EndPrint != null) this.EndPrint(this, null); } } #endregion #region 报表类 [Serializable] public class MyReport { public PageHeader Header; public PageFooter Footer; public PageGroup Group; public Lines PageLines; public ImageObjects PageImageObjects; public StringObjects PageStringObjects; public float Width; //页宽(单位毫米) public float Height; //页高(单位毫米) public float Top = 0; public float Left = 10F; public float Right = 10F; public float Bottom = 0; public float RotatePoint = 0; //旋转度数 public float RotateX = 0; //旋转X坐标转换 public float RotateY = 0; //旋转Y坐标转换 public Rectangles PageRectangles; /// /// 每页分页行数 /// public int PageRecordNumber = 0; /// /// 是否显示页码 /// public bool ShowPagerNumber = true; /// /// 支持纸类 /// public Kinds PagerKind = null; /// /// 是否自动计算每页数据行数 /// public bool AutoMathPageRecordNumber = false; /// /// 设表格行高修正值(文字上边线与下边线的空隙) /// public float TableRowHeightReviseNumber = 1.4F; private bool m_IsFillBlank = false; /// /// 是否填充空行 /// public virtual bool IsFillBlank { get { return m_IsFillBlank; } set { m_IsFillBlank = value; } } private PageLayoutType m_PageLayout = PageLayoutType.GridLayout; /// /// 布局绘制模式 /// public virtual PageLayoutType PageLayout { get { return m_PageLayout; } set { m_PageLayout = value; } } private Font m_Font; public Font Font { get { return this.m_Font; } set { m_Font = value; this.Group.Font = value; this.Header.TableItem.Font = value; this.Footer.TableItem.Font = value; this.Group.LeftItem.Font = value; this.Group.RightItem.Font = value; this.Group.DataItem.Font = value; this.Group.Header.Font = value; this.Group.Footer.Font = value; } } public MyReport() { Header = new PageHeader(); Footer = new PageFooter(); Group = new PageGroup(); PageLines = new Lines(); PageRectangles = new Rectangles(); PageImageObjects = new ImageObjects(); PageStringObjects = new StringObjects(); PagerKind = new Kinds(); this.Font = new System.Drawing.Font("宋体", 3, System.Drawing.FontStyle.Regular, GraphicsUnit.Millimeter); } public MyReport(Kind kind) { Header = new PageHeader(); Footer = new PageFooter(); Group = new PageGroup(); PageLines = new Lines(); PageRectangles = new Rectangles(); PageImageObjects = new ImageObjects(); PageStringObjects = new StringObjects(); PagerKind = new Kinds(); this.Font = new System.Drawing.Font("宋体", 3, System.Drawing.FontStyle.Regular, GraphicsUnit.Millimeter); if (kind.Landscape == false) { this.Height = kind.Height; this.Width = kind.Width; } else { this.Height = kind.Width; this.Width = kind.Height; } PagerKind.Add(kind); this.Group.Width = this.Width - this.Left - this.Right; } public MyReport(string name, float width, float height, bool landscape) { Header = new PageHeader(); Footer = new PageFooter(); Group = new PageGroup(); PageLines = new Lines(); PageRectangles = new Rectangles(); PageImageObjects = new ImageObjects(); PageStringObjects = new StringObjects(); PagerKind = new Kinds(); this.Font = new System.Drawing.Font("宋体", 3, System.Drawing.FontStyle.Regular, GraphicsUnit.Millimeter); PagerKind.Add(new Kind(name, Convert.ToInt32(width), Convert.ToInt32(height), landscape)); if (landscape == false) { this.Width = width; this.Height = height; } else { this.Width = height; this.Height = width; } this.Group.Width = this.Width - this.Left - this.Right; } public MyReport(float width, float height, bool landscape) { Header = new PageHeader(); Footer = new PageFooter(); Group = new PageGroup(); PageLines = new Lines(); PageRectangles = new Rectangles(); PageImageObjects = new ImageObjects(); PageStringObjects = new StringObjects(); PagerKind = new Kinds(); this.Font = new System.Drawing.Font("宋体", 3, System.Drawing.FontStyle.Regular, GraphicsUnit.Millimeter); PagerKind.Add(new Kind("自定义大小", Convert.ToInt32(width), Convert.ToInt32(height), landscape)); if (landscape == false) { this.Width = width; this.Height = height; } else { this.Width = height; this.Height = width; } this.Group.Width = this.Width - this.Left - this.Right; } public MyReport(float width, float height) { Header = new PageHeader(); Footer = new PageFooter(); Group = new PageGroup(); PageLines = new Lines(); PageRectangles = new Rectangles(); PageImageObjects = new ImageObjects(); PageStringObjects = new StringObjects(); PagerKind = new Kinds(); this.Font = new System.Drawing.Font("宋体", 3, System.Drawing.FontStyle.Regular, GraphicsUnit.Millimeter); if (width < height) { PagerKind.Add(new Kind("自定义大小", Convert.ToInt32(width), Convert.ToInt32(height), false)); } else { PagerKind.Add(new Kind("自定义大小", Convert.ToInt32(width), Convert.ToInt32(height), true)); } this.Width = width; this.Height = height; this.Group.Width = this.Width - this.Left - this.Right; } #region 把对象转换成DataSet public DataSet ConvertDataSet() { DataSet ds = new DataSet(); DataRow row = null; //报表对象 DataTable ReportTable = new DataTable("MyReport"); ReportTable.Columns.Add("PageRecordNumber", typeof(int)); //分页行数 ReportTable.Columns.Add("Width", typeof(float)); //报表宽 ReportTable.Columns.Add("Height", typeof(float)); //报表高度 ReportTable.Columns.Add("ShowPagerNumber", typeof(bool)); //是否显示页码 ReportTable.Columns.Add("AutoMathPageRecordNumber", typeof(bool)); //是否自动计算每页数据行数 ReportTable.Columns.Add("TableRowHeightReviseNumber", typeof(float)); //设表格行高修正值(文字上边线与下边线的空隙) ReportTable.Columns.Add("HeaderHeight", typeof(float)); //报表页眉高度 ReportTable.Columns.Add("GroupWidth", typeof(float)); //报表数据区域宽度 ReportTable.Columns.Add("GroupHeight", typeof(float)); //报表数据区域高度 ReportTable.Columns.Add("FooterHeight", typeof(float)); //报表页尾高度 ReportTable.Columns.Add("Top", typeof(float)); //报表上边距 ReportTable.Columns.Add("Left", typeof(float)); //报表左边距 ReportTable.Columns.Add("Right", typeof(float)); //报表右边距 ReportTable.Columns.Add("Bottom", typeof(float)); //报表下边距 ReportTable.Columns.Add("IsFillBlank", typeof(bool)); //是否打印空行 ReportTable.Columns.Add("familyName", typeof(string)); //字体名称 ReportTable.Columns.Add("emSize", typeof(float)); //字体大小 ReportTable.Columns.Add("FontStyle", typeof(int)); //字体式样 ReportTable.Columns.Add("GraphicsUnit", typeof(int)); //字体单位 ReportTable.Columns.Add("PageLayout", typeof(int)); //页面布局 ReportTable.Columns.Add("RotatePoint", typeof(float)); //旋转度数 ReportTable.Columns.Add("RotateX", typeof(float)); //旋转X坐标转换 ReportTable.Columns.Add("RotateY", typeof(float)); //旋转Y坐标转换 ds.Tables.Add(ReportTable); row = ds.Tables["MyReport"].NewRow(); row["Width"] = this.Width; row["Height"] = this.Height; row["PageRecordNumber"] = this.PageRecordNumber; row["ShowPagerNumber"] = this.ShowPagerNumber; row["AutoMathPageRecordNumber"] = this.AutoMathPageRecordNumber; row["TableRowHeightReviseNumber"] = this.TableRowHeightReviseNumber; row["HeaderHeight"] = this.Header.Height; row["GroupWidth"] = this.Group.Width; row["GroupHeight"] = this.Group.Height; row["FooterHeight"] = this.Footer.Height; row["Top"] = this.Top; row["Left"] = this.Left; row["Right"] = this.Right; row["Bottom"] = this.Bottom; row["IsFillBlank"] = this.IsFillBlank; row["familyName"] = this.Font.Name; row["emSize"] = this.Font.Size; row["FontStyle"] = (int)this.Font.Style; row["GraphicsUnit"] = (int)this.Font.Unit; row["PageLayout"] = this.PageLayout; row["RotatePoint"] = this.RotatePoint; row["RotateX"] = this.RotateX; row["RotateY"] = this.RotateY; ds.Tables["MyReport"].Rows.Add(row); DataTable KindTable = new DataTable("PagerKind"); KindTable.Columns.Add("Name", typeof(string)); //纸张名称 KindTable.Columns.Add("Height", typeof(int)); //高度 KindTable.Columns.Add("Width", typeof(int)); //宽度 KindTable.Columns.Add("KindType", typeof(int)); //纸张类型 KindTable.Columns.Add("Landscape", typeof(bool)); //方向 ds.Tables.Add(KindTable); foreach (Kind kd in this.PagerKind) { row = ds.Tables["PagerKind"].NewRow(); row["Name"] = kd.Name; row["Height"] = kd.Height; row["Width"] = kd.Width; row["KindType"] = kd.KindType; row["Landscape"] = kd.Landscape; ds.Tables["PagerKind"].Rows.Add(row); } //线对象 DataTable LinesTable = new DataTable("Lines"); LinesTable.Columns.Add("BrushWidth", typeof(float)); LinesTable.Columns.Add("x1", typeof(float)); LinesTable.Columns.Add("y1", typeof(float)); LinesTable.Columns.Add("x2", typeof(float)); LinesTable.Columns.Add("y2", typeof(float)); ds.Tables.Add(LinesTable); foreach (Line l in this.PageLines) { row = ds.Tables["Lines"].NewRow(); row["BrushWidth"] = l.BrushWidth; row["x1"] = l.Pt1.X; row["y1"] = l.Pt1.Y; row["x2"] = l.Pt2.X; row["y2"] = l.Pt2.Y; ds.Tables["Lines"].Rows.Add(row); } //图片对象 DataTable ImageObjectsTable = new DataTable("ImageObjects"); ImageObjectsTable.Columns.Add("IsZoom", typeof(bool)); ImageObjectsTable.Columns.Add("Height", typeof(int)); ImageObjectsTable.Columns.Add("Width", typeof(int)); ImageObjectsTable.Columns.Add("Top", typeof(int)); ImageObjectsTable.Columns.Add("Left", typeof(int)); ImageObjectsTable.Columns.Add("MemoryByte", typeof(byte[])); ds.Tables.Add(ImageObjectsTable); foreach (ImageObject img in this.PageImageObjects) { row = ds.Tables["ImageObjects"].NewRow(); row["IsZoom"] = img.IsZoom; row["Height"] = img.Height; row["Width"] = img.Width; row["Top"] = img.Top; row["Left"] = img.Left; row["MemoryByte"] = img.MemoryByte; ds.Tables["ImageObjects"].Rows.Add(row); } //矩形对象 DataTable RectanglesTable = new DataTable("Rectangles"); RectanglesTable.Columns.Add("BrushWidth", typeof(float)); RectanglesTable.Columns.Add("X", typeof(float)); RectanglesTable.Columns.Add("Y", typeof(float)); RectanglesTable.Columns.Add("Width", typeof(float)); RectanglesTable.Columns.Add("Height", typeof(float)); ds.Tables.Add(RectanglesTable); foreach (Rectangle l in this.PageRectangles) { row = ds.Tables["Rectangles"].NewRow(); row["BrushWidth"] = l.BrushWidth; row["X"] = l.X; row["Y"] = l.Y; row["Width"] = l.Width; row["Height"] = l.Height; ds.Tables["Rectangles"].Rows.Add(row); } DataTable PageRowsTable = new DataTable("PageRows"); PageRowsTable.Columns.Add("ReportSign", typeof(int)); PageRowsTable.Columns.Add("Alignment", typeof(int)); PageRowsTable.Columns.Add("familyName", typeof(string)); PageRowsTable.Columns.Add("emSize", typeof(float)); PageRowsTable.Columns.Add("FontStyle", typeof(int)); PageRowsTable.Columns.Add("GraphicsUnit", typeof(int)); PageRowsTable.Columns.Add("FormatFlags", typeof(int)); PageRowsTable.Columns.Add("Height", typeof(float)); PageRowsTable.Columns.Add("LineAlignment", typeof(int)); PageRowsTable.Columns.Add("Text", typeof(string)); ds.Tables.Add(PageRowsTable); foreach (PageRow prow in this.Header.Rows) { row = ds.Tables["PageRows"].NewRow(); row["ReportSign"] = 1; row["Alignment"] = (int)prow.Alignment; if (prow.Font != null) { row["familyName"] = prow.Font.Name; row["emSize"] = prow.Font.Size; row["FontStyle"] = (int)prow.Font.Style; row["GraphicsUnit"] = (int)prow.Font.Unit; } row["FormatFlags"] = (int)prow.FormatFlags; row["Height"] = prow.Height; row["LineAlignment"] = (int)prow.LineAlignment; row["Text"] = prow.Text; ds.Tables["PageRows"].Rows.Add(row); } foreach (PageRow prow in this.Footer.Rows) { row = ds.Tables["PageRows"].NewRow(); row["ReportSign"] = 2; row["Alignment"] = (int)prow.Alignment; if (prow.Font != null) { row["familyName"] = prow.Font.Name; row["emSize"] = prow.Font.Size; row["FontStyle"] = (int)prow.Font.Style; row["GraphicsUnit"] = (int)prow.Font.Unit; } row["FormatFlags"] = (int)prow.FormatFlags; row["Height"] = prow.Height; row["LineAlignment"] = (int)prow.LineAlignment; row["Text"] = prow.Text; ds.Tables["PageRows"].Rows.Add(row); } //表对象 DataTable tb = new DataTable("Table"); tb.Columns.Add("TableID", typeof(int)); tb.Columns.Add("Height", typeof(float)); tb.Columns.Add("ShowLine", typeof(bool)); tb.Columns.Add("Width", typeof(float)); ds.Tables.Add(tb); //表的列对象 DataTable ColumnsTable = new DataTable("TableColumns"); ColumnsTable.Columns.Add("TableID", typeof(int)); ColumnsTable.Columns.Add("MinWidth", typeof(float)); ColumnsTable.Columns.Add("Width", typeof(float)); ds.Tables.Add(ColumnsTable); //表的行对象 DataTable RowsTable = new DataTable("TableRows"); RowsTable.Columns.Add("TableID", typeof(int)); RowsTable.Columns.Add("RowID", typeof(int)).AutoIncrement = true; RowsTable.Columns["RowID"].AutoIncrementSeed = 1; RowsTable.Columns["RowID"].AutoIncrementStep = 1; RowsTable.Columns.Add("Height", typeof(float)); RowsTable.Columns.Add("RowSpan", typeof(int)); ds.Tables.Add(RowsTable); //行对象中的单元格 DataTable CellTable = new DataTable("TableCells"); CellTable.Columns.Add("RowID", typeof(int)); CellTable.Columns.Add("Alignment", typeof(int)); CellTable.Columns.Add("BorderWidth", typeof(float)); CellTable.Columns.Add("ColumnSpan", typeof(int)); CellTable.Columns.Add("familyName", typeof(string)); CellTable.Columns.Add("emSize", typeof(float)); CellTable.Columns.Add("FontStyle", typeof(int)); CellTable.Columns.Add("GraphicsUnit", typeof(int)); CellTable.Columns.Add("FormatFlags", typeof(int)); CellTable.Columns.Add("Height", typeof(float)); CellTable.Columns.Add("LineAlignment", typeof(int)); CellTable.Columns.Add("RowSpan", typeof(int)); CellTable.Columns.Add("Text", typeof(string)); CellTable.Columns.Add("Visible", typeof(bool)); CellTable.Columns.Add("VisibleState", typeof(bool)); CellTable.Columns.Add("Width", typeof(float)); ds.Tables.Add(CellTable); FillTable(ds, 1, this.Header.TableItem); FillTable(ds, 2, this.Group.Header); FillTable(ds, 3, this.Group.DataItem); FillTable(ds, 4, this.Group.Footer); FillTable(ds, 5, this.Footer.TableItem); FillTable(ds, 6, this.Group.LeftItem); FillTable(ds, 7, this.Group.RightItem); return ds; } private void FillTable(DataSet ds, int TableID, Table table) { DataRow row = null; row = ds.Tables["Table"].NewRow(); row["TableID"] = TableID; row["Height"] = table.Height; row["ShowLine"] = table.ShowLine; row["Width"] = table.Width; ds.Tables["Table"].Rows.Add(row); foreach (TableColumn col in table.Columns) { row = ds.Tables["TableColumns"].NewRow(); row["TableID"] = TableID; row["MinWidth"] = col.MinWidth; row["Width"] = col.Width; ds.Tables["TableColumns"].Rows.Add(row); } foreach (TableRow trow in table.Rows) { row = ds.Tables["TableRows"].NewRow(); row["TableID"] = TableID; row["Height"] = trow.Height; row["RowSpan"] = trow.RowSpan; ds.Tables["TableRows"].Rows.Add(row); int rowid = Convert.ToInt32(row["RowID"]); foreach (TableCell cell in trow.Cells) { row = ds.Tables["TableCells"].NewRow(); row["RowID"] = rowid; row["Alignment"] = (int)cell.Alignment; row["BorderWidth"] = cell.BorderWidth; row["ColumnSpan"] = cell.ColumnSpan; if (cell.Font != null) { row["familyName"] = cell.Font.Name; row["emSize"] = cell.Font.Size; row["FontStyle"] = (int)cell.Font.Style; row["GraphicsUnit"] = (int)cell.Font.Unit; } row["FormatFlags"] = cell.FormatFlags; row["Height"] = cell.Height; row["LineAlignment"] = (int)cell.LineAlignment; row["RowSpan"] = cell.RowSpan; row["Text"] = cell.Text; row["Visible"] = cell.Visible; row["VisibleState"] = cell.VisibleState; row["Width"] = cell.Width; ds.Tables["TableCells"].Rows.Add(row); } } } #endregion #region 用已填充了报表数据的DataSet对象初始化报表 /// /// 用已填充了报表数据的DataSet对象初始化报表 /// /// public void InitReport(DataSet ds) { this.Height = Convert.ToSingle(ds.Tables["MyReport"].Rows[0]["Height"]); this.Width = Convert.ToSingle(ds.Tables["MyReport"].Rows[0]["Width"]); this.PageRecordNumber = Convert.ToInt32(ds.Tables["MyReport"].Rows[0]["PageRecordNumber"]); this.ShowPagerNumber = Convert.ToBoolean(ds.Tables["MyReport"].Rows[0]["ShowPagerNumber"]); this.AutoMathPageRecordNumber = Convert.ToBoolean(ds.Tables["MyReport"].Rows[0]["AutoMathPageRecordNumber"]); this.TableRowHeightReviseNumber = Convert.ToSingle(ds.Tables["MyReport"].Rows[0]["TableRowHeightReviseNumber"]); this.Header.Height = Convert.ToSingle(ds.Tables["MyReport"].Rows[0]["HeaderHeight"]); this.Group.Height = Convert.ToSingle(ds.Tables["MyReport"].Rows[0]["GroupHeight"]); this.Group.Width = Convert.ToSingle(ds.Tables["MyReport"].Rows[0]["GroupWidth"]); this.Footer.Height = Convert.ToSingle(ds.Tables["MyReport"].Rows[0]["FooterHeight"]); this.Left = Convert.ToSingle(ds.Tables["MyReport"].Rows[0]["Left"]); this.Bottom = Convert.ToSingle(ds.Tables["MyReport"].Rows[0]["Bottom"]); this.Top = Convert.ToSingle(ds.Tables["MyReport"].Rows[0]["Top"]); this.Right = Convert.ToSingle(ds.Tables["MyReport"].Rows[0]["Right"]); this.IsFillBlank = Convert.ToBoolean(ds.Tables["MyReport"].Rows[0]["IsFillBlank"]); this.RotatePoint = Convert.ToSingle(ds.Tables["MyReport"].Rows[0]["RotatePoint"]); this.RotateX = Convert.ToSingle(ds.Tables["MyReport"].Rows[0]["RotateX"]); this.RotateY = Convert.ToSingle(ds.Tables["MyReport"].Rows[0]["RotateY"]); this.Font = new Font(ds.Tables["MyReport"].Rows[0]["familyName"].ToString(), Convert.ToSingle(ds.Tables["MyReport"].Rows[0]["emSize"]), (FontStyle)ds.Tables["MyReport"].Rows[0]["FontStyle"], (GraphicsUnit)ds.Tables["MyReport"].Rows[0]["GraphicsUnit"]); this.PageLayout = (PageLayoutType)ds.Tables["MyReport"].Rows[0]["PageLayout"]; Kind kd = null; Line lin = null; //线条 Rectangle rect = null; //矩形 ImageObject img = null; //图片 PageRow prow = null; //报表行 TableColumn col; //表的列 TableRow trow = null; //表的行 TableCell cell = null; //表的单元格 DataRow[] drow = null; foreach (DataRow row in ds.Tables["PagerKind"].Rows) { kd = new Kind(); kd.Height = Convert.ToInt32(row["Height"]); kd.Width = Convert.ToInt32(row["Width"]); kd.Name = Convert.ToString(row["Name"]); kd.KindType = (KindType)row["KindType"]; kd.Landscape = (bool)row["Landscape"]; this.PagerKind.Add(kd); } foreach (DataRow row in ds.Tables["Lines"].Rows) { lin = new Line(); lin.BrushWidth = Convert.ToSingle(row["BrushWidth"]); lin.Pt1 = new PointF(Convert.ToSingle(row["x1"]), Convert.ToSingle(row["y1"])); lin.Pt2 = new PointF(Convert.ToSingle(row["x2"]), Convert.ToSingle(row["y2"])); this.PageLines.Add(lin); } foreach (DataRow row in ds.Tables["ImageObjects"].Rows) { img = new ImageObject(); img.IsZoom = Convert.ToBoolean(row["IsZoom"]); img.Height = Convert.ToInt32(row["Height"]); img.Width = Convert.ToInt32(row["Width"]); img.Left = Convert.ToInt32(row["Left"]); img.Top = Convert.ToInt32(row["Top"]); img.MemoryByte = (byte[])row["MemoryByte"]; this.PageImageObjects.Add(img); } foreach (DataRow row in ds.Tables["Rectangles"].Rows) { rect = new Rectangle(); rect.BrushWidth = Convert.ToSingle(row["BrushWidth"]); rect.Height = Convert.ToSingle(row["Height"]); rect.Width = Convert.ToSingle(row["Width"]); rect.X = Convert.ToSingle(row["X"]); rect.Y = Convert.ToSingle(row["Y"]); this.PageRectangles.Add(rect); } foreach (DataRow row in ds.Tables["PageRows"].Rows) { prow = new PageRow(); prow.Alignment = (StringAlignment)row["Alignment"]; if (row.IsNull("familyName") == false) { prow.Font = new Font(row["familyName"].ToString(), Convert.ToSingle(row["emSize"]), (FontStyle)row["FontStyle"], (GraphicsUnit)row["GraphicsUnit"]); } prow.FormatFlags = (StringFormatFlags)row["FormatFlags"]; prow.Height = Convert.ToSingle(row["Height"]); prow.LineAlignment = (StringAlignment)row["LineAlignment"]; prow.Text = row["Text"].ToString(); if (row["ReportSign"].ToString() == "1") { this.Header.Rows.Add(prow); } else { this.Footer.Rows.Add(prow); } } foreach (DataRow row in ds.Tables["Table"].Rows) { int TableID = Convert.ToInt32(row["TableID"]); switch (TableID) { case 1: this.Header.TableItem.Height = Convert.ToSingle(row["Height"]); this.Header.TableItem.ShowLine = Convert.ToBoolean(row["ShowLine"]); this.Header.TableItem.Width = Convert.ToSingle(row["Width"]); break; case 2: this.Group.Header.Height = Convert.ToSingle(row["Height"]); this.Group.Header.ShowLine = Convert.ToBoolean(row["ShowLine"]); this.Group.Header.Width = Convert.ToSingle(row["Width"]); break; case 3: this.Group.DataItem.Height = Convert.ToSingle(row["Height"]); this.Group.DataItem.ShowLine = Convert.ToBoolean(row["ShowLine"]); this.Group.DataItem.Width = Convert.ToSingle(row["Width"]); break; case 4: this.Group.Footer.Height = Convert.ToSingle(row["Height"]); this.Group.Footer.ShowLine = Convert.ToBoolean(row["ShowLine"]); this.Group.Footer.Width = Convert.ToSingle(row["Width"]); break; case 5: this.Footer.TableItem.Height = Convert.ToSingle(row["Height"]); this.Footer.TableItem.ShowLine = Convert.ToBoolean(row["ShowLine"]); this.Footer.TableItem.Width = Convert.ToSingle(row["Width"]); break; case 6: this.Group.LeftItem.Height = Convert.ToSingle(row["Height"]); this.Group.LeftItem.ShowLine = Convert.ToBoolean(row["ShowLine"]); this.Group.LeftItem.Width = Convert.ToSingle(row["Width"]); break; case 7: this.Group.RightItem.Height = Convert.ToSingle(row["Height"]); this.Group.RightItem.ShowLine = Convert.ToBoolean(row["ShowLine"]); this.Group.RightItem.Width = Convert.ToSingle(row["Width"]); break; default: break; } } foreach (DataRow row in ds.Tables["TableColumns"].Rows) { int TableID = Convert.ToInt32(row["TableID"]); col = new TableColumn(); col.MinWidth = Convert.ToSingle(row["MinWidth"]); col.Width = Convert.ToSingle(row["Width"]); switch (TableID) { case 1: this.Header.TableItem.Columns.Add(col); break; case 2: this.Group.Header.Columns.Add(col); break; case 3: this.Group.DataItem.Columns.Add(col); break; case 4: this.Group.Footer.Columns.Add(col); break; case 5: this.Footer.TableItem.Columns.Add(col); break; case 6: this.Group.LeftItem.Columns.Add(col); break; case 7: this.Group.RightItem.Columns.Add(col); break; default: break; } } foreach (DataRow row in ds.Tables["TableRows"].Rows) { int TableID = Convert.ToInt32(row["TableID"]); string rowID = Convert.ToString(row["RowID"]); trow = new TableRow(); trow.Height = Convert.ToSingle(row["Height"]); trow.RowSpan = Convert.ToInt32(row["RowSpan"]); //填充单元格 drow = ds.Tables["TableCells"].Select("RowID=" + rowID); foreach (DataRow arow in drow) { cell = new TableCell(); cell.Alignment = (StringAlignment)arow["Alignment"]; cell.BorderWidth = Convert.ToSingle(arow["BorderWidth"]); cell.ColumnSpan = Convert.ToInt32(arow["ColumnSpan"]); if (arow.IsNull("familyName") == false) { cell.Font = new Font(arow["familyName"].ToString(), Convert.ToSingle(arow["emSize"]), (FontStyle)arow["FontStyle"], (GraphicsUnit)arow["GraphicsUnit"]); } cell.FormatFlags = (StringFormatFlags)arow["FormatFlags"]; cell.Height = Convert.ToSingle(arow["Height"]); cell.LineAlignment = (StringAlignment)arow["LineAlignment"]; cell.RowSpan = Convert.ToInt32(arow["RowSpan"]); cell.Text = Convert.ToString(arow["Text"]); cell.Visible = Convert.ToBoolean(arow["Visible"]); cell.VisibleState = Convert.ToBoolean(arow["VisibleState"]); cell.Width = Convert.ToSingle(arow["Width"]); trow.Cells.Add(cell); } switch (TableID) { case 1: this.Header.TableItem.Rows.Add(trow); break; case 2: this.Group.Header.Rows.Add(trow); break; case 3: this.Group.DataItem.Rows.Add(trow); break; case 4: this.Group.Footer.Rows.Add(trow); break; case 5: this.Footer.TableItem.Rows.Add(trow); break; case 6: this.Group.LeftItem.Rows.Add(trow); break; case 7: this.Group.RightItem.Rows.Add(trow); break; default: break; } } } #endregion public SizeF MeasureString(string Text, Font font) { if (PagerKind.Count == 0) throw new Exception("请先添加能打印的纸张类型对象PagerKind"); System.Drawing.Font ffont = font; if (font == null) ffont = this.Font; Bitmap bmp = new Bitmap(PagerKind[0].Width, PagerKind[0].Height); Graphics g = Graphics.FromImage(bmp); g.PageUnit = GraphicsUnit.Millimeter; SizeF s = g.MeasureString(Text, ffont); return s; } public void InitReport() { //格式化表格 if (this.Header.TableItem.Rows.Count > 0) FormatGroups(this.Header.TableItem); //页眉 if (this.Footer.TableItem.Rows.Count > 0) FormatGroups(this.Footer.TableItem); //页眉 if (this.Group.Header.Rows.Count > 0) FormatGroups(this.Group.Header); if (this.Group.DataItem.Rows.Count > 0) FormatGroups(this.Group.DataItem); if (this.Group.Footer.Rows.Count > 0) FormatGroups(this.Group.Footer); if (this.Group.LeftItem.Rows.Count > 0) FormatGroups(this.Group.LeftItem); if (this.Group.RightItem.Rows.Count > 0) FormatGroups(this.Group.RightItem); } void FormatGroups(Table table) { float tableWidth = 0; Font CurrentFont = this.Font; foreach (TableColumn col in table.Columns) { if (col.Width != 0) { tableWidth = tableWidth + col.Width; continue; } int index = table.Columns.IndexOf(col); float colWidth = 0; for (int i = 0; i < table.Rows.Count; i++) { //计算列宽 TableCell cell = table.Rows[i].Cells[index]; if (cell.Font != null) CurrentFont = cell.Font; if (cell.FormatFlags == StringFormatFlags.DirectionVertical) { continue; } string value1 = Regex.Replace(cell.Text, "", "", RegexOptions.IgnoreCase); value1 = Regex.Replace(value1, "", "", RegexOptions.IgnoreCase); value1 = Regex.Replace(value1, "", "", RegexOptions.IgnoreCase); value1 = Regex.Replace(value1, "", "", RegexOptions.IgnoreCase); if (Regex.IsMatch(value1, "
", RegexOptions.IgnoreCase) == false) { float colw = this.MeasureString(value1, CurrentFont).Width; if (colw > colWidth) { colWidth = colw; } } else { string[] ss = Regex.Split(value1, "
", RegexOptions.IgnoreCase); foreach (string s in ss) { float colw = this.MeasureString(s, CurrentFont).Width; if (colw > colWidth) { colWidth = colw; } } } } col.Width = colWidth; colWidth = 0; tableWidth = tableWidth + col.Width; } table.Width = tableWidth; float tableHeight = 0; foreach (TableRow row in table.Rows) { for (int i = 0; i < table.Columns.Count; i++) { TableCell cell = row.Cells[i]; if (cell.RowSpan > 1) {//如果跨行数等于>1,那么添加,计算本单元格所需高度 for (int ii = 1; ii < cell.RowSpan; ii++) { table.Rows[table.Rows.IndexOf(row) + ii].Cells[i].Visible = false; table.Rows[table.Rows.IndexOf(row) + ii].Cells[i].VisibleState = false; } } if (cell.ColumnSpan > 1) {//如果跨列数大于1,那么计算本单元格所需宽度 for (int ii = 1; ii < cell.ColumnSpan; ii++) { row.Cells[i + ii].Visible = false; row.Cells[i + ii].VisibleState = true; } } //如果不是第一行,并且左边与上边的单元格属性为隐藏,那么隐藏单元格 int rowIndex = table.Rows.IndexOf(row); if (rowIndex != 0 && i > 0) { if (row.Cells[i - 1].Visible == false && row.Cells[i - 1].VisibleState == false && table.Rows[rowIndex - 1].Cells[i].Visible == false && table.Rows[rowIndex - 1].Cells[i].VisibleState == true) { cell.Visible = false; } } } tableHeight = tableHeight + (row.Height + this.TableRowHeightReviseNumber) * row.RowSpan; } table.Height = tableHeight; } } #endregion #region 报表页眉 /// /// 页眉 /// [Serializable] public class PageHeader { private float m_Height; public float Height { get { return m_Height; } set { m_Height = value; } } private PageRows m_Rows; public PageRows Rows { get { return m_Rows; } set { m_Rows = value; } } private Table m_TableItem; /// /// 表格 /// public Table TableItem { get { return m_TableItem; } set { m_TableItem = value; } } public PageHeader() { m_Rows = new PageRows(); m_TableItem = new Table(); m_TableItem.ShowLine = false; } } #endregion #region 报表页脚 /// /// 页脚 /// [Serializable] public class PageFooter { private float m_Height; public float Height { get { return m_Height; } set { m_Height = value; } } private PageRows m_Rows; public PageRows Rows { get { return m_Rows; } set { m_Rows = value; } } private Table m_TableItem; /// /// 表格 /// public Table TableItem { get { return m_TableItem; } set { m_TableItem = value; } } public PageFooter() { m_Rows = new PageRows(); m_TableItem = new Table(); m_TableItem.ShowLine = false; } } #endregion #region 报表数据组 [Serializable] public class PageGroup { private float m_Height; public float Height { get { return m_Height; } set { m_Height = value; } } private Font m_Font; public Font Font { get { return this.m_Font; } set { m_Font = value; } } public PageGroup() { this.Header = new Table(); this.DataItem = new Table(); this.Footer = new Table(); this.LeftItem = new Table(); this.RightItem = new Table(); this.Font = new System.Drawing.Font("宋体", 3, System.Drawing.FontStyle.Regular, GraphicsUnit.Millimeter); } #region 初始化 /// /// 用DataTable初始化数据区域) /// /// public void InitGroup(DataTable tb) { Header.Rows.Clear(); DataItem.Rows.Clear(); TableRow row; // int RowNumber = 1; //行高倍率 // float WidthCount = 0; //行中单元格文本最大宽度 float colWidthCount = 0; if (Header.Columns.Count == 0) {//如果列没有设置 for (int i = 0; i < tb.Columns.Count; i++) { string value1 = tb.Columns[i].Caption.Replace("", "♂↓"); value1 = value1.Replace("", "♂↑"); value1 = value1.Replace("", "♂"); value1 = value1.Replace("", "♂"); tb.Columns[i].ExtendedProperties.Add("Width", this.MeasureString(value1, this.Font).Width + MeasureString("A", this.Font).Width); } foreach (DataColumn col in tb.Columns) { colWidthCount = colWidthCount + Convert.ToSingle(col.ExtendedProperties["Width"]); } float SurplusWidth = this.Width - this.LeftItem.Width - this.RightItem.Width - colWidthCount; float addWidth = 0; if (SurplusWidth > 0) addWidth = SurplusWidth / Convert.ToSingle(tb.Columns.Count); foreach (DataColumn col in tb.Columns) { col.ExtendedProperties["Width"] = Convert.ToSingle(col.ExtendedProperties["Width"]) + addWidth; } for (int i = 0; i < tb.Columns.Count; i++) { Header.Columns.Add(new TableColumn(Convert.ToSingle(tb.Columns[i].ExtendedProperties["Width"]))); DataItem.Columns.Add(new TableColumn(Convert.ToSingle(tb.Columns[i].ExtendedProperties["Width"]))); } row = Header.NewRow(); for (int i = 0; i < tb.Columns.Count; i++) {//列标题 row.Cells[i].Text = tb.Columns[i].Caption; row.Cells[i].Width = Header.Columns[i].Width; Header.Columns[i].Name = tb.Columns[i].ColumnName; } // if (this.Header.Height == 0) row.Height = MeasureString("测试高度", this.Font).Height; // if (this.Header.Height != 0) row.Height = this.Header.Height / Convert.ToSingle(tb.Rows.Count + 1); Header.Rows.Add(row); } else { foreach (TableColumn col in Header.Columns) { if (tb.Columns[col.Name].ExtendedProperties.Contains("Width") == false) tb.Columns[col.Name].ExtendedProperties.Add("Width", 0); if (col.Width == 0) { string value1 = col.Caption.Replace("", "♂↓"); value1 = value1.Replace("", "♂↑"); value1 = value1.Replace("", "♂"); value1 = value1.Replace("", "♂"); tb.Columns[col.Name].ExtendedProperties["Width"] = this.MeasureString(value1, this.Font).Width + MeasureString("A", this.Font).Width; } else { tb.Columns[col.Name].ExtendedProperties["Width"] = col.Width; } this.DataItem.Columns.Add(col.Name, col.Caption, col.DataType, col.Format); } foreach (DataRow crow in tb.Rows) { foreach (TableColumn col in Header.Columns) { if (col.Width != 0) continue; string value = crow[col.Name].ToString(); if (col.Format != "") { if (col.DataType == DbType.DateTime) { value = DataConvert.Format(value, TypeCode.DateTime, col.Format); } else if (col.DataType == DbType.Decimal || col.DataType == DbType.Single || col.DataType == DbType.Double) { value = DataConvert.Format(value, TypeCode.Decimal, col.Format); } else { value = crow[col.Name].ToString(); } } string value1 = value.Replace("", "♂↓"); value1 = value1.Replace("", "♂↑"); value1 = value1.Replace("", "♂"); value1 = value1.Replace("", "♂"); float width = this.MeasureString(value1, this.Font).Width + this.MeasureString("A", this.Font).Width; float maxWidth = Convert.ToSingle(tb.Columns[col.Name].ExtendedProperties["Width"]); if (width > maxWidth) tb.Columns[col.Name].ExtendedProperties["Width"] = width; } } foreach (DataColumn col in tb.Columns) { if (col.ExtendedProperties.ContainsKey("Width") == false) continue; colWidthCount = colWidthCount + Convert.ToSingle(col.ExtendedProperties["Width"]); } float SurplusWidth = this.Width - this.LeftItem.Width - this.RightItem.Width - colWidthCount; float addWidth = 0; if (SurplusWidth > 0) {//如果总宽小于数据层宽,那么把剩余的宽度平均分配各列,否则分配至比最小列宽大的列中(按递减方式分配) addWidth = SurplusWidth / Convert.ToSingle(Header.Columns.Count); foreach (DataColumn col in tb.Columns) { if (col.ExtendedProperties.ContainsKey("Width") == false) continue; col.ExtendedProperties["Width"] = Convert.ToSingle(col.ExtendedProperties["Width"]) + addWidth; } } for (int i = 0; i < Header.Columns.Count; i++) { this.Header.Columns[i].Width = Convert.ToSingle(tb.Columns[Header.Columns[i].Name].ExtendedProperties["Width"]); this.DataItem.Columns[i].Width = Convert.ToSingle(tb.Columns[Header.Columns[i].Name].ExtendedProperties["Width"]); } if (SurplusWidth < 0) { float ColWidthCount = 0; //超出最小列宽的列总宽 foreach (TableColumn col in this.Header.Columns) { if (col.Width <= col.MinWidth) continue; ColWidthCount = ColWidthCount + col.Width; } for (int i = 0; i < Header.Columns.Count; i++) { if (Header.Columns[i].Width <= Header.Columns[i].MinWidth) continue; Header.Columns[i].Width = Header.Columns[i].Width + Header.Columns[i].Width / ColWidthCount * SurplusWidth; //列宽除以总宽,取得列宽点用百分比,乘以缺少总宽度 this.DataItem.Columns[i].Width = Header.Columns[i].Width; } } row = Header.NewRow(); for (int i = 0; i < Header.Columns.Count; i++) { row.Cells[i].Text = Header.Columns[i].Caption; row.Cells[i].Width = Header.Columns[i].Width; row.Cells[i].Alignment = Header.Columns[i].StringFormat.Alignment; row.Cells[i].LineAlignment = Header.Columns[i].StringFormat.LineAlignment; row.Cells[i].FormatFlags = Header.Columns[i].StringFormat.FormatFlags; // WidthCount = MeasureString(row.Cells[i].Text, this.Font).Width; // int CurrentRowNumber = Convert.ToInt32(Math.Ceiling(WidthCount / (Header.Columns[i].Width))); // if (CurrentRowNumber > RowNumber) RowNumber = CurrentRowNumber; } row.RowSpan = 1; // row.Height = MeasureString("测试高度", this.Font).Height; Header.Rows.Add(row); } foreach (DataRow row1 in tb.Rows) { row = Header.NewRow(); foreach (TableColumn col in Header.Columns) { int index = Header.Columns.IndexOf(col); if (col.Format != "") { if (col.DataType == DbType.DateTime) { row.Cells[index].Text = DataConvert.Format(row1[col.Name], TypeCode.DateTime, col.Format); } else if (col.DataType == DbType.Decimal || col.DataType == DbType.Single || col.DataType == DbType.Double) { row.Cells[index].Text = DataConvert.Format(row1[col.Name], TypeCode.Decimal, col.Format); } else { row.Cells[index].Text = row1[col.Name].ToString(); } } else { row.Cells[index].Text = row1[col.Name].ToString(); } row.Cells[index].Width = col.Width; row.Cells[index].Alignment = col.StringFormat.Alignment; row.Cells[index].LineAlignment = col.StringFormat.LineAlignment; row.Cells[index].FormatFlags = col.StringFormat.FormatFlags; // WidthCount = MeasureString(row.Cells[index].Text, this.Font).Width; //int CurrentRowNumber = Convert.ToInt32(Math.Ceiling(WidthCount / (col.Width - MeasureString("A", this.Font).Width))); // int CurrentRowNumber = Convert.ToInt32(Math.Ceiling(WidthCount / (col.Width))); // if (CurrentRowNumber > RowNumber) RowNumber = CurrentRowNumber; } row.RowSpan = 1; // row.Height = MeasureString("测试高度", this.Font).Height; DataItem.Rows.Add(row); } } #endregion public SizeF MeasureString(string Text, Font font) { System.Drawing.Font ffont = font; if (font == null) ffont = this.Font; Bitmap bmp = new Bitmap(100, 100); Graphics g = Graphics.FromImage(bmp); g.PageUnit = font.Unit; SizeF s = g.MeasureString(Text, ffont); return s; } private float m_Width; public float Width { get { return m_Width; } set { m_Width = value; } } private Table m_Header; /// /// 多列表头 /// public Table Header { get { return m_Header; } set { m_Header = value; } } private Table m_Footer; /// /// 多列表尾 /// public Table Footer { get { return m_Footer; } set { m_Footer = value; } } private Table m_DataItem; /// /// 数据行 /// public Table DataItem { get { return m_DataItem; } set { m_DataItem = value; } } private Table m_LeftItem; /// /// 左数据表 /// public Table LeftItem { get { return m_LeftItem; } set { m_LeftItem = value; } } private Table m_RightItem; /// /// 右数据表 /// public Table RightItem { get { return m_RightItem; } set { m_RightItem = value; } } } #endregion #region 表示报表文档中的一行 /// /// 表示报表文档中的一行 /// [Serializable] public class PageRow { /// /// 要显示的字符串 /// private string m_Text; public string Text { get { return m_Text; } set { m_Text = value; } } private float m_Height = 0; public float Height { get { return m_Height; } set { m_Height = value; } } private System.Drawing.Font m_Font; public System.Drawing.Font Font { get { return m_Font; } set { m_Font = value; } } private StringFormatFlags m_FormatFlags; /// /// 文本显示方式 /// public StringFormatFlags FormatFlags { get { return m_FormatFlags; } set { m_FormatFlags = value; } } private StringAlignment m_LineAlignment; /// /// 行对齐方式 /// public StringAlignment LineAlignment { get { return m_LineAlignment; } set { m_LineAlignment = value; } } private StringAlignment m_Alignment; /// /// 文本对齐方式 /// public StringAlignment Alignment { get { return m_Alignment; } set { m_Alignment = value; } } public PageRow() { this.LineAlignment = StringAlignment.Near; this.Alignment = StringAlignment.Near; this.FormatFlags = StringFormatFlags.DisplayFormatControl; //this.Font = new System.Drawing.Font("宋体", 3, System.Drawing.FontStyle.Regular,GraphicsUnit.Millimeter); } public PageRow(string Text) { this.LineAlignment = StringAlignment.Near; this.Alignment = StringAlignment.Near; this.FormatFlags = StringFormatFlags.DisplayFormatControl; //this.Font = new System.Drawing.Font("宋体", 3, System.Drawing.FontStyle.Regular,GraphicsUnit.Millimeter); this.Text = Text; } public PageRow(string Text, float Height) { this.LineAlignment = StringAlignment.Near; this.Alignment = StringAlignment.Near; this.FormatFlags = StringFormatFlags.DisplayFormatControl; //this.Font = new System.Drawing.Font("宋体", 3, System.Drawing.FontStyle.Regular,GraphicsUnit.Millimeter); this.Text = Text; this.Height = Height; } } #endregion #region 报表中行的集合 [Serializable] public class PageRows { private ArrayList Controls; public int Count { get { return this.Controls.Count; } set { int a = value; } } public PageRows() { Controls = new ArrayList(); } public int IndexOf(PageRow item) { return Controls.IndexOf(item); } public void Insert(int index, PageRow item) { Controls.Insert(index, item); } public void RemoveAt(int index) { Controls.RemoveAt(index); } public PageRow this[int index] { get { return (PageRow)Controls[index]; } set { Controls[index] = value; } } public PageRow GetRow(int index) { return (PageRow)Controls[index]; } #region ICollection 成员 public void Add(PageRow item) { Controls.Add(item); } public void Add(string Text, StringAlignment Alignment, Font font) { PageRow row = new PageRow(Text); row.Height = MeasureString("测试高度", font).Height; row.Alignment = Alignment; row.Font = font; Controls.Add(row); } public void Add(string Text, Font font) { PageRow row = new PageRow(Text); row.Height = MeasureString("测试高度", font).Height; row.Font = font; Controls.Add(row); } public bool Contains(PageRow item) { return Controls.Contains(item); } public void CopyTo(PageRow[] array, int arrayIndex) { Controls.CopyTo(array, arrayIndex); } public void Clear() { Controls.Clear(); } public bool IsReadOnly { get { return false; } } public void Remove(PageRow item) { Controls.Remove(item); } #endregion #region IEnumerable 成员 public System.Collections.IEnumerator GetEnumerator() { return Controls.GetEnumerator(); } #endregion private SizeF MeasureString(string Text, Font font) { Bitmap bmp = new Bitmap(100, 100); Graphics g = Graphics.FromImage(bmp); g.PageUnit = font.Unit; SizeF s = g.MeasureString(Text, font); return s; } } #endregion #region 表格的列 /// /// 表格的列 /// [Serializable] public class TableColumn { private float m_Width = 0; public float Width { get { return m_Width; } set { this.m_Width = value; } } private float m_MinWidth = 0; /// /// 最小宽度 /// public float MinWidth { get { return m_MinWidth; } set { this.m_MinWidth = value; } } private string m_Caption; /// /// 列标题 /// public string Caption { get { return m_Caption; } set { m_Caption = value; } } private string m_Name; /// /// 名称 /// public string Name { get { return m_Name; } set { this.m_Name = value; } } private string m_Format; /// /// 格式化 /// public string Format { get { return m_Format; } set { this.m_Format = value; } } private DbType m_DataType = DbType.String; /// /// 数据类型 /// public DbType DataType { get { return m_DataType; } set { this.m_DataType = value; } } private string m_Percent; /// /// 列宽占用百分比 /// public string Percent { get { return m_Percent; } set { this.m_Percent = value; } } private StringFormat m_StringFormat; /// /// 格式化 /// public StringFormat StringFormat { get { return m_StringFormat; } set { this.m_StringFormat = value; } } public Dictionary KeyValueFormat { get; set; } public TableColumn() { StringFormat = new StringFormat(); StringFormat.Alignment = StringAlignment.Center; StringFormat.LineAlignment = StringAlignment.Center; StringFormat.FormatFlags = StringFormatFlags.DisplayFormatControl; KeyValueFormat = new Dictionary(); } public TableColumn(float Width) { m_Width = Width; StringFormat = new StringFormat(); StringFormat.Alignment = StringAlignment.Center; StringFormat.LineAlignment = StringAlignment.Center; StringFormat.FormatFlags = StringFormatFlags.DisplayFormatControl; KeyValueFormat = new Dictionary(); } public TableColumn(float Width, StringAlignment Alignment) { m_Width = Width; StringFormat = new StringFormat(); StringFormat.Alignment = Alignment; StringFormat.LineAlignment = StringAlignment.Center; StringFormat.FormatFlags = StringFormatFlags.DisplayFormatControl; KeyValueFormat = new Dictionary(); } public TableColumn(float Width, StringAlignment Alignment, StringAlignment LineAlignment) { m_Width = Width; StringFormat = new StringFormat(); StringFormat.Alignment = Alignment; StringFormat.LineAlignment = LineAlignment; StringFormat.FormatFlags = StringFormatFlags.DisplayFormatControl; KeyValueFormat = new Dictionary(); } } #endregion #region 表格中的行 /// /// 表格中的行 /// [Serializable] public class TableRow { private TableCellCollection TableCells; private float m_Height = 0; public float Height { get { return m_Height; } set { m_Height = value; } } private int m_RowSpan = 1; /// /// 获取或设置表中跨越的行数 /// public int RowSpan { get { return this.m_RowSpan; } set { m_RowSpan = value; } } public TableRow() { TableCells = new TableCellCollection(); } public TableRow(float height) { TableCells = new TableCellCollection(); this.Height = height; } public void AddCell(string Text, int ColumnSpan, int RowSpan) { TableCell cell = new TableCell(); cell.Text = Text; cell.ColumnSpan = ColumnSpan; cell.RowSpan = RowSpan; this.Cells.Add(cell); } public TableCellCollection Cells { get { return TableCells; } } } #endregion #region 表格中的单元格 /// /// 表格中的单元格 /// [Serializable] public class TableCell { private float m_Width; private float m_Height; private Single m_BorderWidth; private int m_ColumnSpan; private Font m_Font; private int m_RowSpan; private string m_Text; private StringFormatFlags m_FormatFlags; /// /// 文本显示方式 /// public StringFormatFlags FormatFlags { get { return m_FormatFlags; } set { m_FormatFlags = value; } } private StringAlignment m_LineAlignment; /// /// 行对齐方式 /// public StringAlignment LineAlignment { get { return m_LineAlignment; } set { m_LineAlignment = value; } } private StringAlignment m_Alignment; /// /// 文本对齐方式 /// public StringAlignment Alignment { get { return m_Alignment; } set { m_Alignment = value; } } public TableCell() { this.LineAlignment = StringAlignment.Center; this.Alignment = StringAlignment.Center; this.FormatFlags = StringFormatFlags.DisplayFormatControl; //this.Font = new System.Drawing.Font("宋体", 3, System.Drawing.FontStyle.Regular,GraphicsUnit.Millimeter); Width = 30; BorderWidth = Convert.ToSingle(0.1); ColumnSpan = 1; RowSpan = 1; Text = " "; } public TableCell(string Text) { this.LineAlignment = StringAlignment.Center; this.Alignment = StringAlignment.Center; this.FormatFlags = StringFormatFlags.DisplayFormatControl; //this.Font = new System.Drawing.Font("宋体", 3, System.Drawing.FontStyle.Regular,GraphicsUnit.Millimeter); Width = 30; BorderWidth = Convert.ToSingle(0.1); ColumnSpan = 1; RowSpan = 1; this.Text = Text; } /// /// 宽度 /// public float Width { get { return this.m_Width; } set { m_Width = value; } } /// /// 高度 /// public float Height { get { return this.m_Height; } set { m_Height = value; } } /// /// 边框宽度 /// public float BorderWidth { get { return this.m_BorderWidth; } set { m_BorderWidth = value; } } /// /// 获取或设置单元格跨越的列数 /// public int ColumnSpan { get { return this.m_ColumnSpan; } set { m_ColumnSpan = value; } } /// /// 单元格中内容的字体 /// public Font Font { get { return this.m_Font; } set { m_Font = value; } } /// /// 获取或设置 表中单元格跨越的行数 /// public int RowSpan { get { return this.m_RowSpan; } set { m_RowSpan = value; } } /// /// 要显示的文本 /// public string Text { get { return this.m_Text; } set { m_Text = value; } } private bool m_Visible = true; public bool Visible { get { return this.m_Visible; } set { m_Visible = value; } } private bool m_VisibleState = true; /// /// true为列隐藏,false为行隐藏 /// public bool VisibleState { get { return this.m_VisibleState; } set { m_VisibleState = value; } } } #endregion #region 行的单元格集合 /// /// 单元格集合 /// [Serializable] public sealed class TableCellCollection { private ArrayList Controls; public TableCellCollection() { Controls = new ArrayList(); } #region IList 成员 public int IndexOf(TableCell item) { return Controls.IndexOf(item); } public void Insert(int index, TableCell item) { Controls.Insert(index, item); } public TableCell this[int index] { get { return (TableCell)this.Controls[index]; } set { this.Controls[index] = value; } } #endregion #region ICollection 成员 public bool Contains(TableCell item) { return Controls.Contains(item); } public void CopyTo(TableCell[] array, int arrayIndex) { Controls.CopyTo(array, arrayIndex); } public int Count { get { return Controls.Count; } set { int a = value; } } public bool IsReadOnly { get { return false; } } public void Remove(TableCell item) { Controls.Remove(item); } #endregion #region IEnumerable 成员 public System.Collections.IEnumerator GetEnumerator() { return Controls.GetEnumerator(); } #endregion #region IList 成员 public void RemoveAt(int index) { Controls.RemoveAt(index); ; } #endregion #region ICollection 成员 public void Clear() { Controls.Clear(); } #endregion public void Add(TableCell cell) { Controls.Add(cell); } public void Add(object item) { Controls.Add(item); } } #endregion #region 表的行集合 /// /// 表的行集合 /// [Serializable] public class TableRowCollection { private ArrayList Controls; public TableRowCollection() { Controls = new ArrayList(); } #region IList 成员 public int IndexOf(TableRow item) { return Controls.IndexOf(item); } public void Insert(int index, TableRow item) { Controls.Insert(index, item); } public void RemoveAt(int index) { Controls.RemoveAt(index); } public TableRow this[int index] { get { return (TableRow)Controls[index]; } set { Controls[index] = value; } } #endregion #region ICollection 成员 public void Add(TableRow item) { Controls.Add(item); } public void Add(TableRow item, Font font) { item.Height = MeasureString("测试高度", font).Height; Controls.Add(item); } public bool Contains(TableRow item) { return Controls.Contains(item); } public void CopyTo(TableRow[] array, int arrayIndex) { Controls.CopyTo(array, arrayIndex); } public int Count { get { return Controls.Count; } set { int a = value; } } public bool IsReadOnly { get { return false; } } public void Remove(TableRow item) { Controls.Remove(item); } #endregion #region IEnumerable 成员 public System.Collections.IEnumerator GetEnumerator() { return Controls.GetEnumerator(); } #endregion #region ICollection 成员 public void Clear() { Controls.Clear(); } #endregion private SizeF MeasureString(string Text, Font font) { Bitmap bmp = new Bitmap(100, 100); Graphics g = Graphics.FromImage(bmp); g.PageUnit = font.Unit; SizeF s = g.MeasureString(Text, font); return s; } } #endregion #region 表的列集合 /// /// 表的列集合 /// [Serializable] public class TableColumnCollection { private ArrayList Controls; public TableColumnCollection() { Controls = new ArrayList(); } #region IList 成员 public int IndexOf(TableColumn item) { return Controls.IndexOf(item); } public void Insert(int index, TableColumn item) { Controls.Insert(index, item); } public void RemoveAt(int index) { Controls.RemoveAt(index); } public TableColumn this[int index] { get { return (TableColumn)Controls[index]; } set { Controls[index] = value; } } #endregion #region ICollection 成员 public bool Contains(TableColumn item) { return Controls.Contains(item); } public bool Contains(string Name) { foreach (TableColumn col in this.Controls) { if (col.Name == Name) return true; } return false; } public void CopyTo(TableColumn[] array, int arrayIndex) { Controls.CopyTo(array, arrayIndex); } public int Count { get { return Controls.Count; } set { int a = value; } } public bool IsReadOnly { get { return false; } } public void Remove(TableColumn item) { Controls.Remove(item); } public TableColumn GetTableColumn(string Name) { TableColumn col = null; foreach (TableColumn item in Controls) { if (item.Name == Name) { col = item; break; } } return col; } #endregion public System.Collections.IEnumerator GetEnumerator() { return Controls.GetEnumerator(); } public void Clear() { Controls.Clear(); } public void Add(TableColumn item) { Controls.Add(item); } public void Add(string Name, string Caption, float MinWidth) { TableColumn col = new TableColumn(); col.MinWidth = MinWidth; col.Caption = Caption; col.Name = Name; col.DataType = DbType.String; col.Format = ""; Controls.Add(col); } public void Add(string Name, string Caption, float MinWidth, StringFormat sf) { TableColumn col = new TableColumn(); col.MinWidth = MinWidth; col.Caption = Caption; col.Name = Name; col.DataType = DbType.String; col.Format = ""; col.StringFormat = sf; Controls.Add(col); } public void Add(string Name, string Caption, DbType DataType, string Format, StringFormat sf) { TableColumn col = new TableColumn(); col.MinWidth = float.MaxValue; col.Caption = Caption; col.Name = Name; col.DataType = DataType; col.Format = Format; col.StringFormat = sf; Controls.Add(col); } public void Add(string Name, string Caption, DbType DataType, string Format) { TableColumn col = new TableColumn(); col.MinWidth = float.MaxValue; col.Caption = Caption; col.Name = Name; col.DataType = DataType; col.Format = Format; Controls.Add(col); } public void Add(string Name, string Caption, DbType DataType, string Format, float Width, StringFormat sf) { TableColumn col = new TableColumn(); col.MinWidth = float.MaxValue; col.Caption = Caption; col.Name = Name; col.DataType = DataType; col.Format = Format; col.Width = Width; col.StringFormat = sf; Controls.Add(col); } public void Add(string Name, string Caption, DbType DataType, string Format, float Width) { TableColumn col = new TableColumn(); col.MinWidth = float.MaxValue; col.Caption = Caption; col.Name = Name; col.DataType = DataType; col.Format = Format; col.Width = Width; Controls.Add(col); } public void Add(string Name, string Caption, DbType DataType, Dictionary Format) { TableColumn col = new TableColumn(); col.MinWidth = float.MaxValue; col.Caption = Caption; col.Name = Name; col.DataType = DataType; col.Format = ""; col.KeyValueFormat = Format; Controls.Add(col); } } #endregion #region 表对象 /// /// 表对象 /// [Serializable] public class Table { private TableRowCollection TableRows; private TableColumnCollection TableColumns; private float m_Width; public float Width { get { return m_Width; } set { m_Width = value; } } private float m_Height; public float Height { get { return m_Height; } set { m_Height = value; } } private bool m_ShowLine = true; /// /// 是否显示线条 /// public bool ShowLine { get { return m_ShowLine; } set { m_ShowLine = value; } } private Font m_Font; public Font Font { get { return this.m_Font; } set { m_Font = value; } } public Table() { TableRows = new TableRowCollection(); TableColumns = new TableColumnCollection(); } public TableRowCollection Rows { get { return this.TableRows; } } public TableColumnCollection Columns { get { return this.TableColumns; } } public TableRow NewRow() { TableRow row = new TableRow(); TableCell cell = null; for (int i = 0; i < this.Columns.Count; i++) { cell = new TableCell(); cell.Width = this.Columns[i].Width; cell.Alignment = this.Columns[i].StringFormat.Alignment; cell.FormatFlags = this.Columns[i].StringFormat.FormatFlags; cell.LineAlignment = this.Columns[i].StringFormat.LineAlignment; row.Cells.Add(cell); } row.Height = this.MeasureString("测试宽度:", this.Font).Height; return row; } public TableRow NewRow(StringAlignment TextAlignment) { TableRow row = new TableRow(); TableCell cell = null; for (int i = 0; i < this.Columns.Count; i++) { cell = new TableCell(); cell.Width = this.Columns[i].Width; cell.Alignment = TextAlignment; row.Cells.Add(cell); } row.Height = this.MeasureString("测试宽度:", this.Font).Height; return row; } public TableRow NewRow(string[] list) { TableRow row = new TableRow(); TableCell cell = null; for (int i = 0; i < this.Columns.Count; i++) { cell = new TableCell(); cell.Width = this.Columns[i].Width; cell.Alignment = this.Columns[i].StringFormat.Alignment; cell.FormatFlags = this.Columns[i].StringFormat.FormatFlags; cell.LineAlignment = this.Columns[i].StringFormat.LineAlignment; if (i < list.Length) cell.Text = list[i]; row.Cells.Add(cell); } row.Height = this.MeasureString("测试宽度:", this.Font).Height; return row; } public TableRow NewRow(string[] list, StringAlignment TextAlignment) { TableRow row = new TableRow(); TableCell cell = null; for (int i = 0; i < this.Columns.Count; i++) { cell = new TableCell(); cell.Width = this.Columns[i].Width; if (i < list.Length) cell.Text = list[i]; cell.Alignment = TextAlignment; row.Cells.Add(cell); } row.Height = this.MeasureString("测试宽度:", this.Font).Height; return row; } private SizeF MeasureString(string Text, Font font) { Bitmap bmp = new Bitmap(100, 100); Graphics g = Graphics.FromImage(bmp); g.PageUnit = GraphicsUnit.Millimeter; SizeF s = g.MeasureString(Text, font); return s; } public void SetFont(Font font) { foreach (TableRow row in this.Rows) { foreach (TableCell cell in row.Cells) { cell.Font = font; } } } } #endregion #region 线对象 /// /// 线对象 /// [Serializable] public class Line { private float m_BrushWidth = 0.1F; /// /// 线条粗细 /// public float BrushWidth { get { return m_BrushWidth; } set { m_BrushWidth = value; } } private PointF m_Pt1; public PointF Pt1 { get { return m_Pt1; } set { m_Pt1 = value; } } private PointF m_Pt2; public PointF Pt2 { get { return m_Pt2; } set { m_Pt2 = value; } } public Line() { } public Line(PointF pt1, PointF pt2) { this.m_Pt1 = pt1; this.m_Pt2 = pt2; } } #endregion #region 线的集合 [Serializable] public class Lines { private ArrayList Controls; public int Count { get { return this.Controls.Count; } set { int a = value; } } public Lines() { Controls = new ArrayList(); } #region IList 成员 public int IndexOf(Line item) { return Controls.IndexOf(item); } public void Insert(int index, Line item) { Controls.Insert(index, item); } public void RemoveAt(int index) { Controls.RemoveAt(index); } public Line this[int index] { get { return (Line)Controls[index]; } set { Controls[index] = value; } } #endregion #region ICollection 成员 public void Add(Line item) { Controls.Add(item); } public bool Contains(Line item) { return Controls.Contains(item); } public void CopyTo(Line[] array, int arrayIndex) { Controls.CopyTo(array, arrayIndex); } public void Clear() { Controls.Clear(); } public bool IsReadOnly { get { return false; } } public void Remove(Line item) { Controls.Remove(item); } #endregion #region IEnumerable 成员 public System.Collections.IEnumerator GetEnumerator() { return Controls.GetEnumerator(); } #endregion } #endregion #region 矩形对象 /// /// 矩形对象 /// [Serializable] public class Rectangle { private float m_BrushWidth = 0.1F; /// /// 线条粗细 /// public float BrushWidth { get { return m_BrushWidth; } set { m_BrushWidth = value; } } private float m_x; public float X { get { return m_x; } set { m_x = value; } } private float m_y; public float Y { get { return m_y; } set { m_y = value; } } private float m_Width; public float Width { get { return m_Width; } set { m_Width = value; } } private float m_Height; public float Height { get { return m_Height; } set { m_Height = value; } } public Rectangle() { } public Rectangle(float x, float y, float width, float height) { this.m_x = x; this.m_y = y; this.Width = width; this.Height = height; } } #endregion #region 矩形的集合 [Serializable] public class Rectangles { private ArrayList Controls; public int Count { get { return this.Controls.Count; } set { int a = value; } } public Rectangles() { Controls = new ArrayList(); } #region IList 成员 public int IndexOf(Rectangle item) { return Controls.IndexOf(item); } public void Insert(int index, Rectangle item) { Controls.Insert(index, item); } public void RemoveAt(int index) { Controls.RemoveAt(index); } public Rectangle this[int index] { get { return (Rectangle)Controls[index]; } set { Controls[index] = value; } } #endregion #region ICollection 成员 public void Add(Rectangle item) { Controls.Add(item); } public bool Contains(Rectangle item) { return Controls.Contains(item); } public void CopyTo(Rectangle[] array, int arrayIndex) { Controls.CopyTo(array, arrayIndex); } public void Clear() { Controls.Clear(); } public bool IsReadOnly { get { return false; } } public void Remove(Rectangle item) { Controls.Remove(item); } #endregion #region IEnumerable 成员 public System.Collections.IEnumerator GetEnumerator() { return Controls.GetEnumerator(); } #endregion } #endregion #region 纸张 /// /// 纸张 /// public class Kind { private string m_Name; /// /// 纸张名称 /// public string Name { get { return m_Name; } set { m_Name = value; } } private int m_Width; /// /// 宽度 /// public int Width { get { return m_Width; } set { m_Width = value; } } private int m_Height; /// /// 高度 /// public int Height { get { return this.m_Height; } set { m_Height = value; } } private KindType m_KindType; /// /// 纸张类型 /// public KindType KindType { get { return this.m_KindType; } set { m_KindType = value; } } private bool m_Landscape; /// /// 纵向还是横向,默认false纵向true横向 /// public bool Landscape { get { return this.m_Landscape; } set { m_Landscape = value; } } public Kind() { } public Kind(KindType kind, bool landscape) { this.Landscape = landscape; KindType = kind; switch (kind) { case KindType.A3: this.Name = "A3"; this.Width = 297; this.Height = 420; break; case KindType.A4: this.Name = "A4"; this.Width = 210; this.Height = 297; break; case KindType.A5: this.Name = "A5"; this.Width = 148; this.Height = 210; break; case KindType.B4: this.Name = "B4"; this.Width = 250; this.Height = 353; break; case KindType.B5: this.Name = "B5"; this.Width = 176; this.Height = 250; break; case KindType.Cascade2: this.Name = "层叠1/2"; this.Width = 210; this.Height = 139; break; case KindType.Cascade3: this.Name = "层叠1/3"; this.Width = 210; this.Height = 93; break; case KindType.Cascade4: this.Name = "层叠1/4"; this.Width = 210; this.Height = 69; break; default: this.Width = 0; this.Height = 0; break; } } public Kind(string Name, int width, int height, bool landscape) { this.Name = Name; this.Width = width; this.Height = height; this.KindType = KindType.Custom; this.Landscape = landscape; } public Kind(int width, int height, bool landscape) { this.Name = "自定义大小"; this.Width = width; this.Height = height; this.KindType = KindType.Custom; this.Landscape = landscape; } } #endregion #region 打印支持纸张 /// /// 打印支持纸张 /// public class Kinds { private ArrayList Controls; public int Count { get { return this.Controls.Count; } } public Kinds() { Controls = new ArrayList(); } #region IList 成员 public int IndexOf(Kind item) { return Controls.IndexOf(item); } public void Insert(int index, Kind item) { Controls.Insert(index, item); } public void RemoveAt(int index) { Controls.RemoveAt(index); } public Kind this[int index] { get { return (Kind)Controls[index]; } set { Controls[index] = value; } } #endregion #region ICollection 成员 public void Add(Kind item) { Controls.Add(item); } public bool Contains(Kind item) { return Controls.Contains(item); } public void CopyTo(Kind[] array, int arrayIndex) { Controls.CopyTo(array, arrayIndex); } public void Clear() { Controls.Clear(); } public bool IsReadOnly { get { return false; } } public void Remove(Kind item) { Controls.Remove(item); } #endregion #region IEnumerable 成员 public System.Collections.IEnumerator GetEnumerator() { return Controls.GetEnumerator(); } #endregion } #endregion #region 纸张类型 public enum KindType { /// /// 297毫米*420毫米 /// A3 = 0, /// /// 210毫米*297毫米 /// A4 = 1, /// /// 148毫米*210毫米 /// A5 = 2, /// /// 250毫米*353毫米 /// B4 = 3, /// /// 176毫米*250毫米 /// B5 = 4, /// /// 层叠1/2(139毫米*210毫米) /// Cascade2 = 5, /// /// 层叠1/3(93毫米*210毫米) /// Cascade3 = 6, /// /// 层叠1/4(69毫米*210毫米) /// Cascade4 = 7, /// /// 自定义 /// Custom = 8 } #endregion #region 页面绘制方式 public enum PageLayoutType { /// /// 默认 /// GridLayout = 0, /// /// 流模式(从上而下) /// FlowLayout = 1, } #endregion #region 图片对象 public class ImageObject { private int m_Height; /// /// 高度(以毫米为单位) /// public int Height { get { return m_Height; } set { m_Height = value; } } private int m_Width; /// /// 宽度(以毫米为单位) /// public int Width { get { return m_Width; } set { m_Width = value; } } private int m_Top; /// /// 顶点坐标(以毫米为单位) /// public int Top { get { return m_Top; } set { m_Top = value; } } private int m_Left; /// /// 左边坐标(以毫米为单位) /// public int Left { get { return m_Left; } set { m_Left = value; } } private byte[] m_MemoryByte; /// /// 图片数据 /// public byte[] MemoryByte { get { return m_MemoryByte; } set { m_MemoryByte = value; } } private bool m_IsZoom; /// /// 是否缩放 /// public bool IsZoom { get { return m_IsZoom; } set { m_IsZoom = value; } } /// /// 图片数据 /// public Image MemoryImage { get { if (MemoryByte == null) return null; MemoryStream ms = new MemoryStream(); ms.Write(MemoryByte, 0, MemoryByte.Length); Image img = Image.FromStream(ms); ms.Close(); return img; } } public ImageObject(string imagePath, int left, int top) { if (File.Exists(imagePath) == false) throw new Exception("文件不存在"); this.MemoryByte = GetPhoto(imagePath); this.Left = left; this.Top = top; this.Width = 0; this.Height = 0; this.IsZoom = false; } public ImageObject(string imagePath, int left, int top, int width, int height) { if (File.Exists(imagePath) == false) throw new Exception("文件不存在"); this.MemoryByte = GetPhoto(imagePath); this.Left = left; this.Top = top; this.Width = width; this.Height = height; this.IsZoom = true; } public ImageObject() { } private byte[] GetPhoto(string filePath) { Bitmap bmp = new Bitmap(filePath); MemoryStream ms = new MemoryStream(); bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); bmp.Dispose(); byte[] photo = new byte[ms.Length - 1]; ms.Position = 0; ms.Read(photo, 0, photo.Length - 1); ms.Close(); return photo; } public void Zoom() { //Image.GetThumbnailImageAbort myCallback = new Image.GetThumbnailImageAbort(ThumbnailCallback); //Image myThumbnail = MemoryImage.GetThumbnailImage(Width, Height, myCallback, IntPtr.Zero); //MemoryByte = GetPhoto(myThumbnail); //myThumbnail.Dispose(); //MemoryImage.Dispose(); System.Drawing.Image initImage = CustomIO.GetPhoto(MemoryByte); //缩略图对象 System.Drawing.Image resultImage = new System.Drawing.Bitmap(Width, Height); System.Drawing.Graphics resultG = System.Drawing.Graphics.FromImage(resultImage); //设置质量 resultG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; resultG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //用指定背景色清空画布 resultG.Clear(Color.White); //绘制缩略图 resultG.DrawImage(initImage, new System.Drawing.Rectangle(0, 0, Width, Height), new System.Drawing.Rectangle(0, 0, initImage.Width, initImage.Height), System.Drawing.GraphicsUnit.Pixel); MemoryByte = GetPhoto(resultImage); //释放缩略图资源 resultG.Dispose(); resultImage.Dispose(); //释放原始图片资源 initImage.Dispose(); } bool ThumbnailCallback() { return false; } #region 图片转成bate数组,MemoryStream流示例 public byte[] GetPhoto(System.Drawing.Image img) { Bitmap bmp = new Bitmap(img); MemoryStream ms = new MemoryStream(); bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); bmp.Dispose(); byte[] photo = new byte[ms.Length - 1]; ms.Position = 0; ms.Read(photo, 0, photo.Length - 1); ms.Close(); return photo; } #endregion } #endregion #region 图片的集合 [Serializable] public class ImageObjects { private ArrayList Controls; public int Count { get { return this.Controls.Count; } set { int a = value; } } public ImageObjects() { Controls = new ArrayList(); } #region IList 成员 public int IndexOf(ImageObject item) { return Controls.IndexOf(item); } public void Insert(int index, ImageObject item) { Controls.Insert(index, item); } public void RemoveAt(int index) { Controls.RemoveAt(index); } public Line this[int index] { get { return (Line)Controls[index]; } set { Controls[index] = value; } } #endregion #region ICollection 成员 public void Add(ImageObject item) { Controls.Add(item); } public bool Contains(ImageObject item) { return Controls.Contains(item); } public void CopyTo(ImageObject[] array, int arrayIndex) { Controls.CopyTo(array, arrayIndex); } public void Clear() { Controls.Clear(); } public bool IsReadOnly { get { return false; } } public void Remove(ImageObject item) { Controls.Remove(item); } #endregion #region IEnumerable 成员 public System.Collections.IEnumerator GetEnumerator() { return Controls.GetEnumerator(); } #endregion } #endregion #region 报表文档 public class ReportDocument { private int m_ID; /// /// ID /// public virtual int ID { get { return m_ID; } set { m_ID = value; } } private string m_Name = "默认文档"; /// /// 文档名称 /// public virtual string Name { get { return m_Name; } set { m_Name = value; } } private string m_FileName; /// /// 报表文件名称+路径 /// public virtual string FileName { get { return m_FileName; } set { m_FileName = value; } } private int m_State; /// /// 打印状态(1未打印2正在打印3已打印) /// public virtual int State { get { return m_State; } set { m_State = value; } } private string m_GuID; /// /// 唯一码 /// public virtual string GuID { get { return m_GuID; } set { m_GuID = value; } } private int m_Copies = 1; /// /// 打印份数 /// public int Copies { get { return m_Copies; } set { m_Copies = value; } } private int m_CopiesCount = 1; /// /// 打印份数 /// public int CopiesCount { get { return m_CopiesCount; } set { m_CopiesCount = value; } } private int m_CurrentCopies; /// /// 当前打印份数 /// public int CurrentCopies { get { return m_CurrentCopies; } set { m_CurrentCopies = value; } } private PrintReport m_PrReport; /// /// 打印报表 /// public PrintReport PrReport { get { return m_PrReport; } set { m_PrReport = value; } } private string m_Url; /// /// webSerice地址 /// public virtual string Url { get { return m_Url; } set { m_Url = value; } } public ReportDocument() { } public ReportDocument(int id, string Name, string FileName, int Copies) { this.ID = id; this.Name = Name; this.FileName = FileName; this.Copies = Copies; this.GuID = Guid.NewGuid().ToString(); } } #endregion #region 报表文档的集合 [Serializable] public class ReportDocuments { private ArrayList Controls; public int Count { get { return this.Controls.Count; } set { int a = value; } } public ReportDocuments() { Controls = new ArrayList(); } #region 返回提交打印文档 public void RetuenReportDocuments(DataSet ds) { ReportDocument doc = null; foreach (DataRow row in ds.Tables[0].Rows) { doc = new ReportDocument(); doc.Copies = Convert.ToInt16(row["Copies"]); doc.CopiesCount = Convert.ToInt16(row["Copies"]); doc.CurrentCopies = 0; doc.GuID = Convert.ToString(row["GuID"]); doc.ID = Convert.ToInt32(row["ID"]); doc.Name = Convert.ToString(row["Name"]); doc.State = Convert.ToInt32(row["State"]); doc.FileName = Convert.ToString(row["FileName"]); doc.Url = Convert.ToString(row["Url"]); this.Add(doc); } } #endregion #region 提取打印状态 /// /// 返回打印状态 /// /// /// public static ReportDocument GetPrintState(int id) { string filePath = System.AppDomain.CurrentDomain.BaseDirectory + "ReportFile/PrintState.xml"; if (System.IO.File.Exists(filePath) == false) return null; DataSet ds = new DataSet(); ds.ReadXml(filePath, XmlReadMode.ReadSchema); DataRow[] drow = ds.Tables[0].Select("id=" + id.ToString()); if (drow.Length == 0) return null; ReportDocument doc = new ReportDocument(); doc.Copies = Convert.ToInt16(drow[0]["Copies"]); doc.CopiesCount = Convert.ToInt16(drow[0]["CopiesCount"]); doc.CurrentCopies = Convert.ToInt16(drow[0]["CurrentCopies"]); doc.GuID = Convert.ToString(drow[0]["GuID"]); doc.ID = Convert.ToInt32(drow[0]["ID"]); doc.Name = Convert.ToString(drow[0]["Name"]); doc.State = Convert.ToInt32(drow[0]["State"]); return doc; } #endregion #region IList 成员 public int IndexOf(ReportDocument item) { return Controls.IndexOf(item); } public void Insert(int index, ReportDocument item) { Controls.Insert(index, item); } public void RemoveAt(int index) { Controls.RemoveAt(index); } public ReportDocument this[int index] { get { return (ReportDocument)Controls[index]; } set { Controls[index] = value; } } #endregion #region ICollection 成员 public void Add(ReportDocument item) { Controls.Add(item); } public bool Contains(ReportDocument item) { return Controls.Contains(item); } public void CopyTo(ReportDocument[] array, int arrayIndex) { Controls.CopyTo(array, arrayIndex); } public void Clear() { Controls.Clear(); } public bool IsReadOnly { get { return false; } } public void Remove(ReportDocument item) { Controls.Remove(item); } #endregion #region IEnumerable 成员 public System.Collections.IEnumerator GetEnumerator() { return Controls.GetEnumerator(); } #endregion } #endregion #region 按坐标画字符串 public class StringObject { public string Name { get; set; } public int Top { get; set; } public int Left { get; set; } public string Text { get; set; } public Font Font { get; set; } public bool Visible { get; set; } public string FormatText { get; set; } public string TextAlign { get; set; } public float Width { get; set; } public float Height { get; set; } } #endregion #region 按坐标画字符串 public class StringObjects : List { public StringObject this[string Name] { get { StringObject model = null; foreach (var item in this) { if (item.Name == Name) model = item; } return model; } } } #endregion }