frmExcelPrint.cs
上传用户:jx_fiona
上传日期:2014-03-08
资源大小:1387k
文件大小:9k
源码类别:

打印编程

开发平台:

Others

  1. using System;
  2. using System.Drawing;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Windows.Forms;
  6. using System.Data;
  7. using GoldPrinter;
  8. namespace MisGoldPrinterTest
  9. {
  10. /// <summary>
  11. /// Excel输出、Excel打印预览、Excel模板定制 -- 通打天下报表
  12. /// </summary>
  13. public class frmExcelPrint : System.Windows.Forms.Form
  14. {
  15. #region Windows 窗体设计器生成的代码
  16. private System.Windows.Forms.Button btnExcelTemplate;
  17. private System.Windows.Forms.Button btnExcel;
  18. private System.Windows.Forms.Button btnExcelPreview;
  19. private System.Windows.Forms.DataGrid dataGrid1;
  20. /// <summary>
  21. /// 必需的设计器变量。
  22. /// </summary>
  23. private System.ComponentModel.Container components = null;
  24. public frmExcelPrint()
  25. {
  26. //
  27. // Windows 窗体设计器支持所必需的
  28. //
  29. InitializeComponent();
  30. //
  31. // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
  32. //
  33. }
  34. /// <summary>
  35. /// 清理所有正在使用的资源。
  36. /// </summary>
  37. protected override void Dispose( bool disposing )
  38. {
  39. if( disposing )
  40. {
  41. if(components != null)
  42. {
  43. components.Dispose();
  44. }
  45. }
  46. base.Dispose( disposing );
  47. }
  48. /// <summary>
  49. /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  50. /// 此方法的内容。
  51. /// </summary>
  52. private void InitializeComponent()
  53. {
  54. this.btnExcelTemplate = new System.Windows.Forms.Button();
  55. this.btnExcel = new System.Windows.Forms.Button();
  56. this.btnExcelPreview = new System.Windows.Forms.Button();
  57. this.dataGrid1 = new System.Windows.Forms.DataGrid();
  58. ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
  59. this.SuspendLayout();
  60. // 
  61. // btnExcelTemplate
  62. // 
  63. this.btnExcelTemplate.Location = new System.Drawing.Point(392, 8);
  64. this.btnExcelTemplate.Name = "btnExcelTemplate";
  65. this.btnExcelTemplate.Size = new System.Drawing.Size(312, 23);
  66. this.btnExcelTemplate.TabIndex = 7;
  67. this.btnExcelTemplate.Text = "Excel模板套打、格式、统计分析报表、图形分析";
  68. this.btnExcelTemplate.Click += new System.EventHandler(this.btnExcelTemplate_Click);
  69. // 
  70. // btnExcel
  71. // 
  72. this.btnExcel.Location = new System.Drawing.Point(8, 8);
  73. this.btnExcel.Name = "btnExcel";
  74. this.btnExcel.Size = new System.Drawing.Size(180, 23);
  75. this.btnExcel.TabIndex = 6;
  76. this.btnExcel.Text = "Excel输出";
  77. this.btnExcel.Click += new System.EventHandler(this.btnExcel_Click);
  78. // 
  79. // btnExcelPreview
  80. // 
  81. this.btnExcelPreview.Location = new System.Drawing.Point(200, 8);
  82. this.btnExcelPreview.Name = "btnExcelPreview";
  83. this.btnExcelPreview.Size = new System.Drawing.Size(180, 23);
  84. this.btnExcelPreview.TabIndex = 8;
  85. this.btnExcelPreview.Text = "Excel打印预览";
  86. this.btnExcelPreview.Click += new System.EventHandler(this.btnExcelPreview_Click);
  87. // 
  88. // dataGrid1
  89. // 
  90. this.dataGrid1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
  91. | System.Windows.Forms.AnchorStyles.Left) 
  92. | System.Windows.Forms.AnchorStyles.Right)));
  93. this.dataGrid1.CaptionText = "MIS金质打印通  通打天下报表  作者:长江支流  下载:www.webmis.com.cn";
  94. this.dataGrid1.DataMember = "";
  95. this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
  96. this.dataGrid1.Location = new System.Drawing.Point(0, 40);
  97. this.dataGrid1.Name = "dataGrid1";
  98. this.dataGrid1.ReadOnly = true;
  99. this.dataGrid1.Size = new System.Drawing.Size(712, 408);
  100. this.dataGrid1.TabIndex = 9;
  101. // 
  102. // frmExcelPrint
  103. // 
  104. this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  105. this.ClientSize = new System.Drawing.Size(712, 446);
  106. this.Controls.Add(this.dataGrid1);
  107. this.Controls.Add(this.btnExcelTemplate);
  108. this.Controls.Add(this.btnExcel);
  109. this.Controls.Add(this.btnExcelPreview);
  110. this.Name = "frmExcelPrint";
  111. this.Text = "Excel输出、Excel打印预览、Excel模板定制 -- 通打天下报表  下载:www.webmis.com.cn";
  112. this.Load += new System.EventHandler(this.frmExcelPrint_Load);
  113. ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
  114. this.ResumeLayout(false);
  115. }
  116. #endregion
  117. //用Excel打印
  118. private void btnExcel_Click(object sender, System.EventArgs e)
  119. {
  120. this.Cursor = Cursors.WaitCursor;
  121. ImportPrint(false);
  122. this.Cursor = Cursors.Default;
  123. }
  124. //直接调用Excel打印预览,用户可以调整列宽、设置页面、页边距、页眉页脚、调整居中方式
  125. private void btnExcelPreview_Click(object sender, System.EventArgs e)
  126. {
  127. this.Cursor = Cursors.WaitCursor;
  128. ImportPrint(true);
  129. this.Cursor = Cursors.Default;
  130. }
  131. //用Excel定制模板打印,所有的格式、报表统计、图形分析等等借用Excel强大功能
  132. private void btnExcelTemplate_Click(object sender, System.EventArgs e)
  133. {
  134. this.Cursor = Cursors.WaitCursor;
  135. ExcelTemplatePrint();
  136. this.Cursor = Cursors.Default;
  137. }
  138. private void frmExcelPrint_Load(object sender, System.EventArgs e)
  139. {
  140. this.dataGrid1.DataSource = PrintDataSource.GetDataSource(10);
  141. }
  142. private void ImportPrint(bool IsPrintPreview)
  143. {
  144. DataTable dt = (DataTable)this.dataGrid1.DataSource;
  145. System.Drawing.Font font = new Font("黑体",21,FontStyle.Bold); 
  146. //用Excel打印,步骤为:打开、写数据、打印预览、关闭
  147. GoldPrinter.ExcelAccess excel = new GoldPrinter.ExcelAccess();
  148. excel.Open();
  149. excel.FormCaption = dataGrid1.CaptionText; //"MIS金质打印通  通打天下报表";
  150. excel.MergeCells(1,1,1,dt.Columns.Count); //合并单元格写标题,并设置字体
  151. excel.SetFont(1,1,1,dt.Columns.Count,font);
  152. excel.SetCellText(1,1,1,dt.Columns.Count,"为灾区募捐,为中国程序员呐喊");
  153. //打印网格及网格线
  154. excel.SetCellText(dt,3,1,true);
  155. //可以在打印预览时单击“页边距”,调整每列的宽。单击“设置页边距”,选择水平居中方式
  156. if (IsPrintPreview)
  157. {
  158. excel.PrintPreview();
  159. excel.Close();
  160. }
  161. else
  162. {
  163. excel.ShowExcel();
  164. }
  165. font.Dispose();
  166. }
  167.  
  168. //用Excel模板打印,比较适合套打、格式、统计分析报表、图形分析、自定义打印
  169. private void ExcelTemplatePrint()
  170. {
  171. #region 用Excel打印,步骤为:打开、写数据、打印预览、关闭
  172. DataTable dt = (DataTable)this.dataGrid1.DataSource; //少打几行看看,哈,打多少也行,只要你不闲慢
  173. GoldPrinter.ExcelAccess excel = new GoldPrinter.ExcelAccess();
  174. string strExcelTemplateFile = System.IO.Path.GetFullPath(@"....ExcelTemplateexample.xlt");
  175. excel.Open(strExcelTemplateFile); //用模板文件
  176. excel.IsVisibledExcel = true;
  177. excel.FormCaption = dataGrid1.CaptionText; //"MIS金质打印通  通打天下报表";
  178. int intDataTemplateRowIndex = 16; //15、16、17为模板数据行,在中间插入,则SUM(E15:E17)会自动更新如SUM(E15:E18)
  179. int intDataStartRowIndex = 18; //写数据起始行,这里应用了Excel本身的技巧,把格式和合计留给Excel本身去做。
  180. //大家可以打开Excel文件,其中模板定义了三个数据行即15、16、17行(定义格式如小数位数、对齐),还有一个合计行18行,合计行定义了形如 =SUM(E15:E17) 。
  181. //为什么定义三行数据行呢?因为定义多于两行的话,插入复制单元格后Excel会自动把SUM(E15:E17)中间的合计行也会增加一行
  182. //如拷贝第16行,并在16行插入复制单元格后,数据行会成为4行,而合计=SUM(E15:E17)自动变成=SUM(E15:E18)、
  183. //同理,图形统计也利用了这一原理
  184. //***哈哈,大家注意了,以上是Excel本身的技术,与程序无关啊,我只是在制作Excel模板的时候用了Excel本身的技术,大家如果看不明白我上面的解释,不用管。
  185. #region 打印网格上面说明
  186. excel.SetCellText(12,"B","010-81120172");
  187. //excel.SetCellText(12,"E","go@webmis.com.cn");
  188. #endregion
  189. //打印网格及网格线,可以直接调用
  190. //excel.SetCellText(dt,intDataStartRowIndex,1,true);
  191. #region 打印网格下面说明
  192. excel.SetCellText(19,"B","010-81120172");
  193. excel.SetCellText(20,"B","flygoldfish@sina.com");
  194. excel.SetCellText(19,"E","150439795");
  195. excel.SetCellText(20,"E",System.DateTime.Now.ToLongDateString());
  196. excel.SetCellText(21,"A","定制模板打印真方便啊,可以把好多事情如格式、计算、图表等让Excel自己做。");
  197. excel.SetCellText(22,"A","不信?打开" + strExcelTemplateFile);
  198. #endregion
  199. #region 打印数据网格
  200. //这里用另一种方法,直接输出到模板文件指定的数据行上,不影响其它数据
  201. for(int i=0;i< dt.Rows.Count;i++)
  202. {
  203. excel.InsertRow(intDataTemplateRowIndex,intDataTemplateRowIndex); //拷贝插入模板行并设置行高
  204. excel.SetRowHeight(intDataTemplateRowIndex,14.25f);
  205. }
  206. for(int i=0;i< dt.Rows.Count;i++)
  207. {
  208. for(int j=0;j<dt.Columns.Count;j++)
  209. {
  210. excel.SetCellText(intDataStartRowIndex + i , j+1 ,dt.Rows[i][j].ToString());
  211. }
  212. }
  213. //画边框及网格线
  214. excel.SetBordersEdge(intDataStartRowIndex,1,intDataStartRowIndex + dt.Rows.Count - 1,dt.Columns.Count,false);
  215. #endregion
  216. #region 删除模板数据行
  217. excel.DeleteRow(17);
  218. excel.DeleteRow(16);
  219. excel.DeleteRow(15);
  220. #endregion
  221. //excel.Print(); //打印
  222. excel.PrintPreview(); //预览
  223. excel.Close(); //关闭并释放
  224. #endregion
  225. }
  226. }//End Class
  227. }//End Namespace