using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing.Printing;
using System.Drawing;
using System.Windows.Forms;
using System.Windows;
using System.Data;
using Microsoft.Office;
namespace KingBoy
{
    class MyDataGrid:DataGridView
    {
        #region var
        //用户可自定义 
        private Font tableFont=new Font("宋体", 9);  // '当前要打印文本的字体及字号 
        private Font headFont=new Font("黑体", 12, FontStyle.Regular);//表头字体 
        private Font subHeadFont=new Font("楷体_GB2312", 10, FontStyle.Regular); //副表头字体 
        private String headText="" ; //表头文字 
        private String subHeadLeftText="";  //副表头左文字
        private String subHeadRightText="";  //副表头右文字 
        private int headHeight=40;  //表头高度 
        private int subHeadHeight=30;  //副表头高度 
        private Font footFont=new Font("黑体", 12, FontStyle.Underline); //表脚字体
        private Font subFootFont=new Font("楷体_GB2312", 10, FontStyle.Regular);//副表脚字体 
        private String footText=""; ////表脚文字 
        private String subFootLeftText="" ;  //副表脚左文字 
        private String subFootRightText="" ;  //副表脚右文字
        private int footHeight=40; //表脚高度
        private int subFootHeight=30;  //副表脚高度 
        private String worker="";  //'制表人
        private double sum=0; //汇总数据
        private int x_unit=0; //表的基本单位
        private int y_unit= 24;

//以下为模块内部使用      
        private PrintDocument printDocument1;     
        private DataRow dataGridRow;
        private DataTable dataTable1;
        private int Cols; //                          '当前要打印的列                          
        private int ColsCount; //                    '当前DATAGRID共有多少列       
        private int pageRecordNumber; //                 '当前要所要打印的记录行数,由计算得到. 
        private int printingPageNumber=0;//           '正要打印的页号 
        private int pageNumber;//                       '共需要打印的页数
        private int printRecordLeave;//                 '当前还有多少页没有打印 
        private int printRecordComplete=0;  //        '已经打印完的记录数 
        private int pLeft,pTop,pRight,pBottom,pWidth,pHeight;
        private SolidBrush drawBrush = new SolidBrush(System.Drawing.Color.Black);//   '当前画笔颜色 
        private int pRecordNumber=0  ;//'每页打印的记录条数 
        private int totalPage;  //总共应该打印的页数        
        #endregion
        #region property
         
        /// <summary>
        ///用户自定义字体及字号
        /// </summary>
        public Font setTableFont
        {
         set{tableFont=value;}
        }
        /// <summary>
        /// 设置表头字体
        /// </summary>
       public Font setHeadFont
       {
        set{headFont =value;}
       }
        public Font setFootFont
        {
         set{footFont=value;}
        }
       public Font setSubFootFont
       {
        set{subFootFont =value;}
       }
      public string setHeadText
      {
       set{headText =value;}
      }
      public string setSubHeadLeftText
      {
        set{subHeadLeftText =value;}
      }
        public string setSubHeadRightText
        {
         set{subHeadRightText =value;}
        }
       public string setFootText
       {
         set{footText =value;}
       }

public string setSubFootLeftText
       {
         set{subFootLeftText =value;}
       }
       public string setSubFootRightText
       {
         set{subFootRightText =value;}
       }
       public int setHeadHeight
       {
        set{headHeight =value;}
       }
public int setSubHeadHeight
{
 set{
   subHeadHeight =value;
 }
}
        public int setFootHeight
        {
         set{footHeight =value;}
        }
       public int setSubFootHeight
       {
         set{subFootHeight =value;}
       }
        public int setCellHeight
        {
          set{y_unit =value;}
        }
        /// <summary>
        /// 汇总数据
        /// </summary>
        public double setSum
        {
         set{sum=value;}
        }
        /// <summary>
        /// 设置制表人
        /// </summary>
      public string setWorker
      {
       set{worker =value;}
      }
       
        #endregion
        #region method
        /// <summary>
        /// 根据列明统计该列值总和
        /// </summary>
        /// <param name="colomnName">列明</param>
        /// <returns>如果该列明是数值型,则返回统计结果,否则返回0</returns>
        public double addColumnDGV(string colomnName)
        {
          try
          {
              int i=0; //循环下标
              double sum=0;//用来保存统计临时结果
              for(i=0;i<this.Rows.Count ;i++)
                  try
                  { sum=sum+Convert.ToDouble(this.Rows[i].Cells[colomnName].Value); }//获取i行colomnName列值
                  catch (Exception)
                  { }                  
              return sum;
          }
            catch(Exception )
          {
            return 0;
            }
        }
        public double addColumnDGV(string colomnName,char ch)
        {
            try
            {
                int i = 0; //循环下标
                double sum = 0;//用来保存统计临时结果
                for (i = 0; i < this.Rows.Count; i++)
                    try
                    { sum = sum + Convert.ToDouble(this.Rows[i].Cells[colomnName].Value.ToString().Substring(1, this.Rows[i].Cells[colomnName].Value.ToString().Length-1)); }//获取i行colomnName列值
                    catch (Exception)
                    { }
                return sum;
            }
            catch (Exception)
            {
                return 0;
            }
        }
        /// <summary>
        /// 统计某行从intStartCol列到intEndCol和
        /// </summary>
        /// <param name="row">行值</param>
        /// <param name="intStartCol">起始列索引值</param>
        /// <param name="intEndCol">终止列索引值</param>
        /// <returns>合法统计结果,非法返回0</returns>
        public double addRowDGV(int row,int intStartCol,int intEndCol)
        {
          try
          {
              //判断列的起始和终止索引是否有效,行值在有效的范围内
            if (this.Columns.Count >0 && intStartCol >=0 && intEndCol <this.Columns.Count && row<this.Rows.Count ) 
            {
              int i;
                double tmpDec=0;
                //循环统计该行起始列到终止列累加和,从intStartCol到intEndCol列都是数值型数据
                for (i=intStartCol;i<intEndCol;i++)
                    try
                    {tmpDec=tmpDec +(double)(this.Rows[row].Cells[i].Value); }
                    catch (Exception)
                    { }
                    
                return tmpDec;
            }
            else
                return 0;
          }
            catch(Exception)
          {
            return 0;
            }
        }    
        /// <summary>
        /// 导出数据到Excel
        /// <param name="startComlumn">起始列,0表示第一列,依次类推</param>
        /// <param name="endColumn" >终止列,1表示第一列,依次类推</param>
        /// </summary>
        public void loadDataToExcel(int startComlumn,int endColumn)
        {
          if (this.Rows.Count <=0)
          { MessageBox.Show("没有数据,无法导出!");
            return ;
          }
          if (startComlumn > endColumn)
          {
              MessageBox.Show("起始列不能大于终止列!");
              return;
          }
          if (startComlumn>=this.Columns.Count || endColumn>=this.Columns.Count)
          {
              MessageBox.Show("起始列和终止列都不能大于总列数!");
              return;
          }
          //创建Excel中的新表         
          Excel.ApplicationClass oExcel = new Excel.ApplicationClass();
          oExcel.Application.Workbooks.Add(true);   
          
          int n=0;
          int i=0;         
          //填充表头
          for (i = startComlumn; i < endColumn; i++)
          { 
          oExcel.Cells[1, i+1] = this.Columns[i].HeaderText;
          }
          for(n=0;n<this.Rows.Count;n++)
              for (i = startComlumn; i <endColumn; i++)
              { 
               oExcel.Cells[n+2, i+1] = this.Rows[n].Cells[i].Value.ToString();
             
              }
          oExcel.Visible = true;        
          oExcel.Quit();
          oExcel = null;
            GC.Collect();
              
        }

含有打印、统计DataGridView(1)的更多相关文章

  1. 含有打印、统计DataGridView(2)

    /// <summary>        /// 导出数据到Excel                /// </summary>        public void loa ...

  2. C语言字母频率统计

    在进行密码破解时有时候需要得到字母出现的频率信息,下面我将简单的使用C语言来读取一个文件,然后统计该文件内的字母出现的频率. 1.在D盘下新建一个文本文件(文件名为"A.txt") ...

  3. Linux - wc统计文件行数、单词数或字节数

    一 wc简单介绍 wc命令用来打印文件的文本行数.单词数.字节数等(print the number of newlines, words, and bytes in files).在Windows的 ...

  4. shell的wc命令统计 head tail命令详解

    Linux系统中的wc(Word Count)命令的功能为统计指定文件中的字节数.字数.行数,并将统计结果显示输出. 1.命令格式: wc [选项]文件... 2.命令功能: 统计指定文件中的字节数. ...

  5. C# 使用PrintDocument 绘制表格 完成 打印预览

    C# 使用PrintDocument 绘制表格 完成 打印预览 DataTable   经过不断的Google与baidu,最终整理出来的打印类 主要是根据两个参考的类组合而成,稍微修改了一下,参考代 ...

  6. C# 使用PrintDocument 绘制表格 完成 打印预览 DataTable

    经过不断的Google与baidu,最终整理出来的打印类 主要是根据两个参考的类组合而成,稍微修改了一下,参考代码及来源见最后(其中一份是VB语言的) 其中遇到的一些问题也已经得到了解决(分页,打印预 ...

  7. C# WinForm开发系列 - DataGrid/DataGridView

    在WinForm开发中,DataGrid/DataGridView被广泛使用于绑定数据库中数据进行呈现.整理一些关于DataGrid/DataGridView使用的文章,涉及DataGrid/Data ...

  8. Linux命令-统计文件中的字节数、字数、行数:wc

    Linux系统中的wc(Word Count)命令的功能为统计指定文件中的字节数.字数.行数,并将统计结果显示输出. 1.命令格式: wc [选项]文件... 2.命令功能: 统计指定文件中的字节数. ...

  9. C# PrintDocument 打印表格

    1.封装好的辅助类: using System; using System.Data; using System.Drawing; using System.Drawing.Printing; usi ...

随机推荐

  1. 洛谷P3355 骑士共存问题

    题目描述 在一个 n*n个方格的国际象棋棋盘上,马(骑士)可以攻击的棋盘方格如图所示.棋盘上某些方格设置了障碍,骑士不得进入 对于给定的 n*n 个方格的国际象棋棋盘和障碍标志,计算棋盘上最多可以放置 ...

  2. C# double保留四位小数

    2.保留N位,四舍五入 . decimal d= decimal.Round(decimal.Parse("0.55555"),4); 3.保留N位四舍五入 Math.Round( ...

  3. SSM(spring,springMVC,Mybatis)框架的整合

    这几天想做一个小项目,所以搭建了一个SSM框架. 1.基本概念 1.1.Spring   Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Joh ...

  4. sublime 3 删除当前行

    sublime 3 删除当前行 sublime 默认的是 “ctrl+shift+k” ,然而当你点击时,会出现希腊字母的软键盘,与windows默认快捷键有冲突 不用急 不用慌 这时打开: “Pre ...

  5. 7.cocos精灵创建和绘制

    创建Layer层的类 T2LayerSprite.h #pragma once #include "cocos2d.h" USING_NS_CC; class T2LayerSpr ...

  6. 网络最大流算法—Dinic算法及优化

    前置知识 网络最大流入门 前言 Dinic在信息学奥赛中是一种最常用的求网络最大流的算法. 它凭借着思路直观,代码难度小,性能优越等优势,深受广大oier青睐 思想 $Dinic$算法属于增广路算法. ...

  7. 突破极限 解决大硬盘上安装Unix新思路

    一.问题提出 硬盘越做越大,然我喜欢让我忧.10年前就遇到过在586电脑BIOS不认识超过8.4G容量硬盘的问题,以及Windows Nt操作系统不认大硬盘(容量超过8.4G)的问题,对于Linux ...

  8. 完全背包模板 51Nod 1101

    N元钱换为零钱,有多少不同的换法?币值包括1 2 5分,1 2 5角,1 2 5 10 20 50 100元. 例如:5分钱换为零钱,有以下4种换法: 1.5个1分 2.1个2分3个1分 3.2个2分 ...

  9. VS Code 关于SFTP上传文件到多服务器的配置

    工欲善其事,必先利其器! 刚学前端的时候一直用的DW来编写代码,其功能非常强大,但在Linux下不能用,所以就转VS Code了. 但是刚开始使用VS Code的时候,很多DW上的功能需要自己安装扩展 ...

  10. mycat 不得不说的缘分(转)

    ,尾声,左兄与任正非.leader-us与马云 新成立的公司里面,有个左兄,很传奇,大一在大学入伍,然后复员专业,来上海学IT,年纪轻轻,睡在地铁站,苦心专研数据库.系统.中间件,现在已经成为了业界大 ...