原創|其它|編輯:郝浩|2011-10-25 11:43:00.000|閱讀 1273 次
概述:通常情況下,使用默認打印設置,一張紙上面只能打印電子表格中的一個工作表,但是你可以配置多種布局,例如,自定義打印設置單頁紙上面打印多個工作表。在下面的示例程序,窗體上放置了兩個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
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:慧都控件網