彩票走势图

Spread for Windows Forms实现单页纸上打印多个工作表

原创|其它|编辑:郝浩|2011-10-25 11:43:00.000|阅读 1240 次

概述:通常情况下,使用默认打印设置,一张纸上面只能打印电子表格中的一个工作表,但是你可以配置多种布局,例如,自定义打印设置单页纸上面打印多个工作表。在下面的示例程序,窗体上放置了两个Spread控件,因此,只要指定打印时每个工作表各自的输出位置,就可以实现在同一张纸上面打印出两个工作表。需要创建一个子类,System.Drawing.Printing.PrintDocument类。

# 慧都年终大促·界面/图表报表/文档/IDE等千款热门软控件火热促销中 >>

  通常情况下,使用默认打印设置,一张纸上面只能打印电子表格中的一个工作表,但是你可以配置多种布局,例如,自定义打印设置单页纸上面打印多个工作表。在下面的示例程序,窗体上放置了两个Spread控件,因此,只要指定打印时每个工作表各自的输出位置,就可以实现在同一张纸上面打印出两个工作表。需要创建一个子类,System.Drawing.Printing.PrintDocument类。下图展示了一个窗体中的两个Spread控件,以及在单页纸上面的打印结果。


Two spreadsheet controls on a single Windows Form.


Result of both printed to a single page.

C# Code

//Create a new instance of PrintPreviewDialog control.
private PrintPreviewDialog PrintPreviewDialog1 = new PrintPreviewDialog();

private void Form1_Load(object sender, System.EventArgs e)
{

fpSpread1.ActiveSheet.RowCount = 5;
fpSpread1.ActiveSheet.ColumnCount = 6;
fpSpread1.ActiveSheet.DefaultStyle.CellType = new FarPoint.Win.Spread.CellType.NumberCellType();
fpSpread1.ActiveSheet.DefaultStyle.ForeColor = Color.Red;
fpSpread1.ActiveSheet.ColumnHeader.Rows[0].BackColor = Color.LightPink;

for (int i = 0; i <= fpSpread1.ActiveSheet.RowCount -1 ; i++)
{
for (int j = 0; j <= fpSpread1.ActiveSheet.ColumnCount -1 ; j++)
{
fpSpread1.ActiveSheet.SetValue(i, j, i + j);
}
}

fpSpread2.ActiveSheet.RowCount = 5;
fpSpread2.ActiveSheet.ColumnCount = 8;
fpSpread2.ActiveSheet.DefaultStyle.CellType = new FarPoint.Win.Spread.CellType.NumberCellType();
fpSpread2.ActiveSheet.DefaultStyle.ForeColor = Color.Blue;
fpSpread2.ActiveSheet.ColumnHeader.Rows[0].BackColor = Color.LightCyan;

for (int i = 0; i <= fpSpread2.ActiveSheet.RowCount -1 ; i++)
{
for (int j = 0; j <= fpSpread2.ActiveSheet.ColumnCount -1 ; j++)
{
fpSpread2.ActiveSheet.SetValue(i, j, i + j);
}
}

}

private void button1_Click(object sender, System.EventArgs e)
{

//Configure PrintInfo of respective sheets.
fpSpread1.ActiveSheet.PrintInfo.ShowColor = true;
fpSpread1.ActiveSheet.PrintInfo.ShowGrid = false;
fpSpread1.ActiveSheet.PrintInfo.ShowRowHeaders = false;
fpSpread2.ActiveSheet.PrintInfo.ShowColor = true;
fpSpread2.ActiveSheet.PrintInfo.ShowGrid = false;
fpSpread2.ActiveSheet.PrintInfo.ShowRowHeaders = false;

//Pass SPREAD controls of user defined printing class.
OwnerPrintDocument aDoc = new OwnerPrintDocument(fpSpread1, fpSpread2);

//Configure respective sheet titles.
aDoc.Title1 = "FpSpread1 title";
aDoc.Title2 = "FpSpread2 title";

//Configure user defined printing documents to PrintPreview.
PrintPreviewDialog1.Document = aDoc;
PrintPreviewDialog1.PrintPreviewControl.Zoom = 0.75;

//Display the print preview.
PrintPreviewDialog1.ShowDialog();

}

Classes for user-defined printing

[Serializable()] public class OwnerPrintDocument : System.Drawing.Printing.PrintDocument //Inherit a PrintDocument class.
{

//Two SPREAD controls to be printed
private FarPoint.Win.Spread.FpSpread op_Spread1;
private FarPoint.Win.Spread.FpSpread op_Spread2;

//Each title
public string Title1;
public string Title2;

public OwnerPrintDocument(FarPoint.Win.Spread.FpSpread Spread_1, FarPoint.Win.Spread.FpSpread Spread_2) : base()
{
op_Spread1 = Spread_1;
op_Spread2 = Spread_2;
}

protected override void OnBeginPrint(System.Drawing.Printing.PrintEventArgs ev)
{

//Override OnBeginPrint method.
base.OnBeginPrint(ev);

}

protected override void OnPrintPage(System.Drawing.Printing.PrintPageEventArgs e)
{

//Override OnPrintPage method.
base.OnPrintPage(e);

//************************
// Outputting FpSpread1
//************************
//Configure drawing positions.
Rectangle rect1 = new Rectangle(e.PageBounds.X + 30, e.PageBounds.Y + 40, e.PageBounds.Width / 2, e.PageBounds.Height / 2);

//Obtain the required numbers of pages.
int cnt1 = op_Spread1.GetOwnerPrintPageCount(e.Graphics, rect1, 0);

//Output only when pages to be printed exist.
if (cnt1 > 0)
{
op_Spread1.OwnerPrintDraw(e.Graphics, rect1, 0, cnt1);
e.HasMorePages = false;
}

//*******************************************
// Draw the title on the top of FpSpread1
//*******************************************
RectangleF drect1 = new RectangleF();
drect1.X = e.PageBounds.X + 30;
drect1.Y = e.PageBounds.Y + 10;
drect1.Width = e.PageBounds.Width / 2;
drect1.Height = e.PageBounds.Height / 2;
Brush b1 =new SolidBrush(Color.Red);
e.Graphics.DrawString(Title1, new Font("MS P Gothic", 14, FontStyle.Bold | FontStyle.Italic), b1, drect1);
b1.Dispose();

//************************
// Outputting FpSpread2
//************************
//Configure drawing positions.
Rectangle rect2 = new Rectangle(e.PageBounds.X + 30, e.PageBounds.Y + 210, e.PageBounds.Width - 100, e.PageBounds.Height / 2);

//Obtain the required numbers of pages.
int cnt2 = op_Spread2.G

下载《Spread for Windows Forms》


(慧都控件网版权所有,转载请注明出处,否则追究法律责任)
标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@pclwef.cn

文章转载自:慧都控件网

为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP