frmExcelPrint.cs
资源名称:GOLDPRINT.rar [点击查看]
上传用户:jx_fiona
上传日期:2014-03-08
资源大小:1387k
文件大小:9k
源码类别:
打印编程
开发平台:
Others
- using System;
- using System.Drawing;
- using System.Collections;
- using System.ComponentModel;
- using System.Windows.Forms;
- using System.Data;
- using GoldPrinter;
- namespace MisGoldPrinterTest
- {
- /// <summary>
- /// Excel输出、Excel打印预览、Excel模板定制 -- 通打天下报表
- /// </summary>
- public class frmExcelPrint : System.Windows.Forms.Form
- {
- #region Windows 窗体设计器生成的代码
- private System.Windows.Forms.Button btnExcelTemplate;
- private System.Windows.Forms.Button btnExcel;
- private System.Windows.Forms.Button btnExcelPreview;
- private System.Windows.Forms.DataGrid dataGrid1;
- /// <summary>
- /// 必需的设计器变量。
- /// </summary>
- private System.ComponentModel.Container components = null;
- public frmExcelPrint()
- {
- //
- // Windows 窗体设计器支持所必需的
- //
- InitializeComponent();
- //
- // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
- //
- }
- /// <summary>
- /// 清理所有正在使用的资源。
- /// </summary>
- protected override void Dispose( bool disposing )
- {
- if( disposing )
- {
- if(components != null)
- {
- components.Dispose();
- }
- }
- base.Dispose( disposing );
- }
- /// <summary>
- /// 设计器支持所需的方法 - 不要使用代码编辑器修改
- /// 此方法的内容。
- /// </summary>
- private void InitializeComponent()
- {
- this.btnExcelTemplate = new System.Windows.Forms.Button();
- this.btnExcel = new System.Windows.Forms.Button();
- this.btnExcelPreview = new System.Windows.Forms.Button();
- this.dataGrid1 = new System.Windows.Forms.DataGrid();
- ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
- this.SuspendLayout();
- //
- // btnExcelTemplate
- //
- this.btnExcelTemplate.Location = new System.Drawing.Point(392, 8);
- this.btnExcelTemplate.Name = "btnExcelTemplate";
- this.btnExcelTemplate.Size = new System.Drawing.Size(312, 23);
- this.btnExcelTemplate.TabIndex = 7;
- this.btnExcelTemplate.Text = "Excel模板套打、格式、统计分析报表、图形分析";
- this.btnExcelTemplate.Click += new System.EventHandler(this.btnExcelTemplate_Click);
- //
- // btnExcel
- //
- this.btnExcel.Location = new System.Drawing.Point(8, 8);
- this.btnExcel.Name = "btnExcel";
- this.btnExcel.Size = new System.Drawing.Size(180, 23);
- this.btnExcel.TabIndex = 6;
- this.btnExcel.Text = "Excel输出";
- this.btnExcel.Click += new System.EventHandler(this.btnExcel_Click);
- //
- // btnExcelPreview
- //
- this.btnExcelPreview.Location = new System.Drawing.Point(200, 8);
- this.btnExcelPreview.Name = "btnExcelPreview";
- this.btnExcelPreview.Size = new System.Drawing.Size(180, 23);
- this.btnExcelPreview.TabIndex = 8;
- this.btnExcelPreview.Text = "Excel打印预览";
- this.btnExcelPreview.Click += new System.EventHandler(this.btnExcelPreview_Click);
- //
- // dataGrid1
- //
- this.dataGrid1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
- | System.Windows.Forms.AnchorStyles.Left)
- | System.Windows.Forms.AnchorStyles.Right)));
- this.dataGrid1.CaptionText = "MIS金质打印通 通打天下报表 作者:长江支流 下载:www.webmis.com.cn";
- this.dataGrid1.DataMember = "";
- this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
- this.dataGrid1.Location = new System.Drawing.Point(0, 40);
- this.dataGrid1.Name = "dataGrid1";
- this.dataGrid1.ReadOnly = true;
- this.dataGrid1.Size = new System.Drawing.Size(712, 408);
- this.dataGrid1.TabIndex = 9;
- //
- // frmExcelPrint
- //
- this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
- this.ClientSize = new System.Drawing.Size(712, 446);
- this.Controls.Add(this.dataGrid1);
- this.Controls.Add(this.btnExcelTemplate);
- this.Controls.Add(this.btnExcel);
- this.Controls.Add(this.btnExcelPreview);
- this.Name = "frmExcelPrint";
- this.Text = "Excel输出、Excel打印预览、Excel模板定制 -- 通打天下报表 下载:www.webmis.com.cn";
- this.Load += new System.EventHandler(this.frmExcelPrint_Load);
- ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
- this.ResumeLayout(false);
- }
- #endregion
- //用Excel打印
- private void btnExcel_Click(object sender, System.EventArgs e)
- {
- this.Cursor = Cursors.WaitCursor;
- ImportPrint(false);
- this.Cursor = Cursors.Default;
- }
- //直接调用Excel打印预览,用户可以调整列宽、设置页面、页边距、页眉页脚、调整居中方式
- private void btnExcelPreview_Click(object sender, System.EventArgs e)
- {
- this.Cursor = Cursors.WaitCursor;
- ImportPrint(true);
- this.Cursor = Cursors.Default;
- }
- //用Excel定制模板打印,所有的格式、报表统计、图形分析等等借用Excel强大功能
- private void btnExcelTemplate_Click(object sender, System.EventArgs e)
- {
- this.Cursor = Cursors.WaitCursor;
- ExcelTemplatePrint();
- this.Cursor = Cursors.Default;
- }
- private void frmExcelPrint_Load(object sender, System.EventArgs e)
- {
- this.dataGrid1.DataSource = PrintDataSource.GetDataSource(10);
- }
- private void ImportPrint(bool IsPrintPreview)
- {
- DataTable dt = (DataTable)this.dataGrid1.DataSource;
- System.Drawing.Font font = new Font("黑体",21,FontStyle.Bold);
- //用Excel打印,步骤为:打开、写数据、打印预览、关闭
- GoldPrinter.ExcelAccess excel = new GoldPrinter.ExcelAccess();
- excel.Open();
- excel.FormCaption = dataGrid1.CaptionText; //"MIS金质打印通 通打天下报表";
- excel.MergeCells(1,1,1,dt.Columns.Count); //合并单元格写标题,并设置字体
- excel.SetFont(1,1,1,dt.Columns.Count,font);
- excel.SetCellText(1,1,1,dt.Columns.Count,"为灾区募捐,为中国程序员呐喊");
- //打印网格及网格线
- excel.SetCellText(dt,3,1,true);
- //可以在打印预览时单击“页边距”,调整每列的宽。单击“设置页边距”,选择水平居中方式
- if (IsPrintPreview)
- {
- excel.PrintPreview();
- excel.Close();
- }
- else
- {
- excel.ShowExcel();
- }
- font.Dispose();
- }
- //用Excel模板打印,比较适合套打、格式、统计分析报表、图形分析、自定义打印
- private void ExcelTemplatePrint()
- {
- #region 用Excel打印,步骤为:打开、写数据、打印预览、关闭
- DataTable dt = (DataTable)this.dataGrid1.DataSource; //少打几行看看,哈,打多少也行,只要你不闲慢
- GoldPrinter.ExcelAccess excel = new GoldPrinter.ExcelAccess();
- string strExcelTemplateFile = System.IO.Path.GetFullPath(@"....ExcelTemplateexample.xlt");
- excel.Open(strExcelTemplateFile); //用模板文件
- excel.IsVisibledExcel = true;
- excel.FormCaption = dataGrid1.CaptionText; //"MIS金质打印通 通打天下报表";
- int intDataTemplateRowIndex = 16; //15、16、17为模板数据行,在中间插入,则SUM(E15:E17)会自动更新如SUM(E15:E18)
- int intDataStartRowIndex = 18; //写数据起始行,这里应用了Excel本身的技巧,把格式和合计留给Excel本身去做。
- //大家可以打开Excel文件,其中模板定义了三个数据行即15、16、17行(定义格式如小数位数、对齐),还有一个合计行18行,合计行定义了形如 =SUM(E15:E17) 。
- //为什么定义三行数据行呢?因为定义多于两行的话,插入复制单元格后Excel会自动把SUM(E15:E17)中间的合计行也会增加一行
- //如拷贝第16行,并在16行插入复制单元格后,数据行会成为4行,而合计=SUM(E15:E17)自动变成=SUM(E15:E18)、
- //同理,图形统计也利用了这一原理
- //***哈哈,大家注意了,以上是Excel本身的技术,与程序无关啊,我只是在制作Excel模板的时候用了Excel本身的技术,大家如果看不明白我上面的解释,不用管。
- #region 打印网格上面说明
- excel.SetCellText(12,"B","010-81120172");
- //excel.SetCellText(12,"E","go@webmis.com.cn");
- #endregion
- //打印网格及网格线,可以直接调用
- //excel.SetCellText(dt,intDataStartRowIndex,1,true);
- #region 打印网格下面说明
- excel.SetCellText(19,"B","010-81120172");
- excel.SetCellText(20,"B","flygoldfish@sina.com");
- excel.SetCellText(19,"E","150439795");
- excel.SetCellText(20,"E",System.DateTime.Now.ToLongDateString());
- excel.SetCellText(21,"A","定制模板打印真方便啊,可以把好多事情如格式、计算、图表等让Excel自己做。");
- excel.SetCellText(22,"A","不信?打开" + strExcelTemplateFile);
- #endregion
- #region 打印数据网格
- //这里用另一种方法,直接输出到模板文件指定的数据行上,不影响其它数据
- for(int i=0;i< dt.Rows.Count;i++)
- {
- excel.InsertRow(intDataTemplateRowIndex,intDataTemplateRowIndex); //拷贝插入模板行并设置行高
- excel.SetRowHeight(intDataTemplateRowIndex,14.25f);
- }
- for(int i=0;i< dt.Rows.Count;i++)
- {
- for(int j=0;j<dt.Columns.Count;j++)
- {
- excel.SetCellText(intDataStartRowIndex + i , j+1 ,dt.Rows[i][j].ToString());
- }
- }
- //画边框及网格线
- excel.SetBordersEdge(intDataStartRowIndex,1,intDataStartRowIndex + dt.Rows.Count - 1,dt.Columns.Count,false);
- #endregion
- #region 删除模板数据行
- excel.DeleteRow(17);
- excel.DeleteRow(16);
- excel.DeleteRow(15);
- #endregion
- //excel.Print(); //打印
- excel.PrintPreview(); //预览
- excel.Close(); //关闭并释放
- #endregion
- }
- }//End Class
- }//End Namespace