• 在C#中利用Excel做高级报表


    下面进行高级报表设计,该方法的原理为:首先打开Excel,按照要求排好版,保存为一个文件做为模板,然后在C#中将该文件复制为一个新文件,在指定位置填入数据就可以了,为了添加表格线,我们录制了一个宏,在C#中执行该宏即可。

    using System;

    using System.Drawing;

    using System.Collections;

    using System.ComponentModel;

    using System.Windows.Forms;

    using System.Data;

    using System.IO;

    using System.Reflection;

    namespace MyExcel

    {

         /// <summary>

         /// Form1 的摘要说明。

         /// </summary>

         public class Form1 : System.Windows.Forms.Form

         {

             private System.Windows.Forms.Button btnNormal;

             private System.Windows.Forms.Button btnAdvace;

             /// <summary>

             /// 必需的设计器变量。

             /// </summary>

             private System.ComponentModel.Container components = null;

             public Form1()

             {

                  //

                  // Windows 窗体设计器支持所必需的

                  //

                  InitializeComponent();

                  //

                  // TODO: 在 InitializeComponent 调用后添加任何构造函数代码

                  //

             }

             /// <summary>

             /// 清理所有正在使用的资源。

             /// </summary>

             protected override void Dispose( bool disposing )

             {

                  if( disposing )

                  {

                       if (components != null)

                       {

                           components.Dispose();

                       }

                  }

                  base.Dispose( disposing );

             }

             #region Windows Form Designer generated code

             /// <summary>

             /// 设计器支持所需的方法 - 不要使用代码编辑器修改

             /// 此方法的内容。

             /// </summary>

             private void InitializeComponent()

             {

                  this.btnNormal = new System.Windows.Forms.Button();

                  this.btnAdvace = new System.Windows.Forms.Button();

                  this.SuspendLayout();

                  //

                  // btnNormal

                  //

                  this.btnNormal.Location = new System.Drawing.Point(49, 55);

                  this.btnNormal.Name = "btnNormal";

                  this.btnNormal.TabIndex = 0;

                  this.btnNormal.Text = "普通报表";

                  this.btnNormal.Click += new System.EventHandler(this.btnNormal_Click);

                  //

                  // btnAdvace

                  //

                  this.btnAdvace.Location = new System.Drawing.Point(169, 55);

                  this.btnAdvace.Name = "btnAdvace";

                  this.btnAdvace.TabIndex = 1;

                  this.btnAdvace.Text = "高级报表";

                  this.btnAdvace.Click += new System.EventHandler(this.btnAdvace_Click);

                  //

                  // Form1

                  //

                  this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);

                  this.ClientSize = new System.Drawing.Size(292, 133);

                  this.Controls.AddRange(new System.Windows.Forms.Control[] {

                                                                                          this.btnAdvace,

                                                                                          this.btnNormal});

                  this.Name = "Form1";

                  this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;

                  this.Text = "Form1";

                  this.ResumeLayout(false);

             }

             #endregion

             /// <summary>

             /// 应用程序的主入口点。

             /// </summary>

             [STAThread]

             static void Main()

             {

                  Application.Run(new Form1());

             }

             private string [,] myData=

             {

                  {"车牌号","类 型","品 牌","型 号","颜 色","附加费证号","车架号"},

                  {"JAJAJS","危险品","货车","铁风SZG9220YY","白","323232","323232"},

                         };

              //普通报表,即单纯的文件导出功能

             private void btnNormal_Click(object sender, System.EventArgs e)

             {

                  //创建一个Excel文件

                  Excel.Application myExcel = new Excel.Application ( ) ;

                  myExcel.Application.Workbooks.Add ( true ) ;

                  //让Excel文件可见

                  myExcel.Visible=true;

                  //第一行为报表名称

                  myExcel.Cells[1,4]="普通报表";

                  //逐行写入数据,

                  for(int i=0;i<11;i++)

                  {

                       for(int j=0;j<7;j++)

                       {

                           //以单引号开头,表示该单元格为纯文本

                           myExcel.Cells[2+i,1+j]="'"+myData[i,j];

                       }

                  }

             }

             //高级报表,根据模板生成的报表

             private void btnAdvace_Click(object sender, System.EventArgs e)

             {

                  string filename="";

                  //将模板文件复制到一个新文件中

                  SaveFileDialog mySave=new SaveFileDialog();

                  mySave.Filter="Excel文件(*.XLS)|*.xls|所有文件(*.*)|*.*";

                  if(mySave.ShowDialog()!=DialogResult.OK)

                  {

                       return;

                  }

                  else

                  {

                       filename=mySave.FileName;

                       //将模板文件copy到新位置,建议实际开发时用相对路径,如Application.StartupPath.Trim()+"\\report\\normal.xls"

                      

                       string filenameold=mySave.FileName;

                       FileInfo mode=new FileInfo("d:\\normal.xls");

                       try

                       {

                           mode.CopyTo(filename,true);

                       }

                       catch(Exception ee)

                       {

                           MessageBox.Show(ee.Message);

                           return;

                       }

                  }

                  //打开复制后的文件

                  object missing=Missing.Value;

                  Excel.Application myExcel=new Excel.Application ( );

                  //打开新文件

                  myExcel.Application.Workbooks.Open(filename,

                       missing,

                       missing,

                       missing,

                       missing,

                       missing,

                       missing,

                       missing,

                       missing,

                       missing,

                       missing,

                       missing,

                       missing);

                  //将Excel显示出来

                  myExcel.Visible=true;

                  //逐行写入数据,数组中第一行我列标题,忽略

                  for(int i=1;i<11;i++)

                  {

                       for(int j=0;j<7;j++)

                       {

                           //以单引号开头,表示该单元格为纯文本

                           myExcel.Cells[4+i,1+j]="'"+myData[i,j];

                       }

                  }

                 

                  //将列标题和实际内容选中

                  Excel.Workbook myBook=myExcel.Workbooks[1];

                  Excel.Worksheet mySheet=(Excel.Worksheet)myBook.Worksheets[1];

                  Excel.Range r=mySheet.get_Range(mySheet.Cells[3,1],mySheet.Cells[14,7]);

                  r.Select();

                  //=====通过执行宏来格表格加边框=======//

                  try

                  {

                       myExcel.Run("宏1",missing,missing,

                           missing,missing,missing,missing,missing,missing,missing,

                           missing,missing,missing,missing,missing,missing,missing,

                           missing,missing,missing,missing,missing,missing,missing,

                           missing,missing,missing,missing,missing,missing,missing);

                  }

                  catch

                  {

                  }

                  //保存修改

                  myBook.Save();

             }

        

         }//end of form

    }

    在上述代码中,我们指定了选定范围:

    Excel.Range r=mySheet.get_Range(mySheet.Cells[3,1],mySheet.Cells[14,7]);

    具体开发时,我们可以根据数据库中的实际数据来计算范围,我们的列标题是从.Cells[3,1]开始的,在程序中定死了,为灵活使用,我们完全可以在模板的Cells[1,1]或者其他单元格填入一些基本信息,如实际数据起始位置等等,操作时,从该单元格读入数据,然后将该单元格内容替换成需要的内容。还有个问题,我们往单元格中写内容时假设某列应该放什么内容,为灵活起见,我们在得到了列标题起始位置后,读入该单元格内容(即该列应该是什么字段),再从数据库中找到相应的字段来填充该列,可以保证所填内容与设计的报表对应起来,还可以忽略数据库中无用的字段,也就是说同一个数据库表可以有许多种报表,只要有相应的模板就可以了,读入某单元格内容的代码如下:

         Excel.Range r;

         r=mySheet.get_Range(mySheet.Cells[2,1],mySheet.Cells[2,1]);  //取得值存放的区域

         string strValue=r.Value.ToString();

    一次只能读一个单元格,否则得不到相应的数据,即=mySheet.get_Range(mySheet.Cells[2,1],mySheet.Cells[2,1])中两个参数都必须是同一个单元格,本例中为mySheet.Cells[2,1]。

  • 相关阅读:
    滚动条美化插件 nicescroll
    百度地图api
    Echarts的重点
    3月20号课堂随笔
    循环for语句
    有关一些CSS的基本内容
    HTML基本标签和一些注释的问题
    2018年3月17号的随堂笔记
    03.15补习
    for 的相关用法
  • 原文地址:https://www.cnblogs.com/jhabb/p/1880666.html
Copyright © 2020-2023  润新知