当前位置:首页 > Windows程序 > 正文

含有打印、统计DataGridView(2)

2021-03-19 Windows程序

/// <summary>
        /// 导出数据到Excel        
        /// </summary>
        public void loadDataToExcel()
        {
            if (this.Rows.Count <= 0)
            {
                MessageBox.Show("没有数据,无法导出!");
                return;
            }            
            //创建Excel中的新表         
            Excel.ApplicationClass oExcel = new Excel.ApplicationClass();
            oExcel.Application.Workbooks.Add(true);

int n = 0;
            int i = 0;
            //填充表头
            for (i = 0; i < this.Columns.Count; i++)
            {
                oExcel.Cells[1, i + 1] = this.Columns[i].HeaderText;
            }
            for (n = 0; n < this.Rows.Count; n++)
                for (i = 0; i < this.Columns.Count; i++)
                {
                    try
                    {
                     oExcel.Cells[n + 2, i + 1] = this.Rows[n].Cells[i].Value.ToString();
                    }
                    catch (Exception)
                    {
                        oExcel.Cells[n + 2, i + 1] = "";
                    }
                  

}
            oExcel.Visible = true;
            oExcel.Quit();
            oExcel = null;
            GC.Collect();

}
        /// <summary>
        /// 将datagridview的内容转换成DataTable
        /// <param>起始列,0表示第一列,依次类推</param>
        /// <param>终止列,1表示第一列,,依次类推</param>
        /// </summary>
        /// <returns>如果转换成功返回DataTable,否则返回nothing</returns>
       /// <remarks></remarks>       
        public DataTable convertToDataTable(int startCol,int endCol)
        {
         try
         {
             if (startCol > endCol)
             {
                 MessageBox.Show("起始列不能大于终止列!");
                 return null;
             }
             if (startCol >= this.Columns.Count || endCol >= this.Columns.Count)
             {
                 MessageBox.Show("起始列和终止列都不能大于总列数!");
                 return null;
             }
             //创建数据表格对象
          DataTable dt=new DataTable();
          int i,j;
          //获取列名
          for (i=startCol;i<endCol;i++)
          {
           dt.Columns.Add(Columns[i].ToString());
           dt.Columns[i].Caption=Columns[i].HeaderText;
          }
             //获取行值
           DataRow r;
           object[] rowArray = new object[endCol];
           for(i=0;i<Rows.Count;i++)
           {
              
             r=dt.NewRow(); //创建新行
             for (j = 0; j < endCol; j++)
                 rowArray[j]=this.Rows[i].Cells[j].Value; //获取j行中各列值
             r.ItemArray = rowArray;//设置新行r中的值                 
             dt.Rows.Add(r);//添加新行r到dt数据表格中
            
           }
             if (dt.Rows.Count >0)
                 return dt; //存在数据返回数据表格
             else
                 return null;
         }catch(Exception )
         {
            return null;
         }
        }
        /// <summary>
        /// 将datagridview的内容转换成DataTable
        /// </summary>
        /// <returns>如果转换成功返回DataTable,否则返回nothing</returns>
        /// <remarks></remarks>       
        public DataTable convertToDataTable()
        {
            try
            {
                
                //创建数据表格对象
                DataTable dt = new DataTable();
                int i, j;
                //获取列名
                for (i = 0; i < Columns.Count; i++)
                {
                    dt.Columns.Add(Columns[i].ToString());
                    dt.Columns[i].Caption = Columns[i].HeaderText;
                }
                //获取行值
                DataRow r;
                object[] rowArray = new object[Columns.Count];
                for (i = 0; i < Rows.Count; i++)
                {

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/63622.html