含有打印、统计DataGridView(2)
/// <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 name="startCol">起始列,0表示第一列,依次类推</param>
/// <param name="endCol">终止列,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++)
{
r = dt.NewRow(); //创建新行
for (j = 0; j < Columns.Count; 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;
}
}
public void printPreview()
{
try
{
dataTable1 = new DataTable();
dataTable1 = convertToDataTable();
if (dataTable1 == null)
{
MessageBox.Show("打印失败!");
return;
}
ColsCount = dataTable1.Columns.Count;
printDocument1 = new PrintDocument();
this.printDocument1.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.printDocument1_PrintPage);
PageSetupDialog pageSetup = new PageSetupDialog();
pageSetup.Document = printDocument1;
printDocument1.DefaultPageSettings = pageSetup.PageSettings;
if (pageSetup.ShowDialog() == DialogResult.Cancel)
{
return;
}
pLeft = printDocument1.DefaultPageSettings.Margins.Left;
pTop = printDocument1.DefaultPageSettings.Margins.Top;
pRight = printDocument1.DefaultPageSettings.Margins.Right;
pBottom = printDocument1.DefaultPageSettings.Margins.Bottom;
pWidth = printDocument1.DefaultPageSettings.Bounds.Width;
pHeight = printDocument1.DefaultPageSettings.Bounds.Height;
//将当前页分成基本的单元
x_unit = (pWidth - pLeft - pRight) / dataTable1.Columns.Count - 1;
pRecordNumber = (pHeight - pTop - pBottom - headHeight - subHeadHeight - footHeight - subFootHeight - y_unit) / y_unit;
if (dataTable1.Rows.Count > pRecordNumber)
if (dataTable1.Rows.Count % pRecordNumber == 0)
totalPage = dataTable1.Rows.Count / pRecordNumber;
else
totalPage = dataTable1.Rows.Count / pRecordNumber + 1;
else
totalPage = 1;
printDocument1.DocumentName = totalPage.ToString();
PrintPreviewDialog printPreview = new PrintPreviewDialog();
printPreview.Document = printDocument1;
printPreview.ShowDialog();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public void printPreview(int startCol, int endCol)
{
try
{
dataTable1 =new DataTable();
dataTable1=convertToDataTable(startCol,endCol);
if (dataTable1 == null)
{
MessageBox.Show("打印失败!");
return;
}
ColsCount =dataTable1.Columns.Count;
printDocument1=new PrintDocument();
this.printDocument1.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.printDocument1_PrintPage);
PageSetupDialog pageSetup=new PageSetupDialog();
pageSetup.Document =printDocument1;
printDocument1.DefaultPageSettings =pageSetup.PageSettings;
if (pageSetup.ShowDialog()==DialogResult.Cancel)
{
return ;
}
pLeft = printDocument1.DefaultPageSettings.Margins.Left;
pTop = printDocument1.DefaultPageSettings.Margins.Top;
pRight = printDocument1.DefaultPageSettings.Margins.Right;
pBottom = printDocument1.DefaultPageSettings.Margins.Bottom;
pWidth = printDocument1.DefaultPageSettings.Bounds.Width;
pHeight = printDocument1.DefaultPageSettings.Bounds.Height;
//将当前页分成基本的单元
x_unit=(pWidth -pLeft-pRight)/dataTable1.Columns.Count -1;
pRecordNumber=(pHeight -pTop -pBottom -headHeight -subHeadHeight -footHeight -subFootHeight -y_unit)/y_unit;
if (dataTable1.Rows.Count >pRecordNumber)
if (dataTable1.Rows.Count % pRecordNumber==0)
totalPage =dataTable1.Rows.Count /pRecordNumber;
else
totalPage =dataTable1.Rows.Count /pRecordNumber+1;
else
totalPage =1;
printDocument1.DocumentName =totalPage.ToString();
PrintPreviewDialog printPreview=new PrintPreviewDialog();
printPreview.Document =printDocument1;
printPreview.ShowDialog();
}catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public void print(int startCol, int endCol)
{
try
{
dataTable1 =new DataTable();
dataTable1=convertToDataTable(startCol,endCol);
if (dataTable1 == null)
{
MessageBox.Show("打印失败!");
return;
}
ColsCount =dataTable1.Columns.Count;
printDocument1=new PrintDocument();
this.printDocument1.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.printDocument1_PrintPage);
pLeft = printDocument1.DefaultPageSettings.Margins.Left;
pTop = printDocument1.DefaultPageSettings.Margins.Top;
pRight = printDocument1.DefaultPageSettings.Margins.Right;
pBottom = printDocument1.DefaultPageSettings.Margins.Bottom;
pWidth = printDocument1.DefaultPageSettings.Bounds.Width;
pHeight = printDocument1.DefaultPageSettings.Bounds.Height;
//将当前页分成基本的单元
x_unit=(pWidth -pLeft-pRight)/dataTable1.Columns.Count -1;
pRecordNumber=(pHeight -pTop -pBottom -headHeight -subHeadHeight -footHeight -subFootHeight -y_unit)/y_unit;
if (dataTable1.Rows.Count >pRecordNumber)
if (dataTable1.Rows.Count % pRecordNumber==0)
totalPage =dataTable1.Rows.Count /pRecordNumber;
else
totalPage =dataTable1.Rows.Count /pRecordNumber+1;
else
totalPage =1;
printDocument1.DocumentName =totalPage.ToString();
printDocument1.Print();
}catch(Exception)
{
MessageBox.Show("打印错误,请检查打印设置!");
}
}
public void print()
{
try
{
dataTable1 = new DataTable();
dataTable1 = convertToDataTable();
if (dataTable1 == null)
{
MessageBox.Show("打印失败!");
return;
}
ColsCount = dataTable1.Columns.Count;
printDocument1 = new PrintDocument();
this.printDocument1.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.printDocument1_PrintPage);
pLeft = printDocument1.DefaultPageSettings.Margins.Left;
pTop = printDocument1.DefaultPageSettings.Margins.Top;
pRight = printDocument1.DefaultPageSettings.Margins.Right;
pBottom = printDocument1.DefaultPageSettings.Margins.Bottom;
pWidth = printDocument1.DefaultPageSettings.Bounds.Width;
pHeight = printDocument1.DefaultPageSettings.Bounds.Height;
//将当前页分成基本的单元
x_unit = (pWidth - pLeft - pRight) / dataTable1.Columns.Count - 1;
pRecordNumber = (pHeight - pTop - pBottom - headHeight - subHeadHeight - footHeight - subFootHeight - y_unit) / y_unit;
if (dataTable1.Rows.Count > pRecordNumber)
if (dataTable1.Rows.Count % pRecordNumber == 0)
totalPage = dataTable1.Rows.Count / pRecordNumber;
else
totalPage = dataTable1.Rows.Count / pRecordNumber + 1;
else
totalPage = 1;
printDocument1.DocumentName = totalPage.ToString();
printDocument1.Print();
}
catch (Exception)
{
MessageBox.Show("打印错误,请检查打印设置!");
}
}
private void printDocument1_PrintPage(Object sender,PrintPageEventArgs ev)
{
printRecordLeave=dataTable1.Rows.Count -printRecordComplete; //还有多少条记录没有打印
if (printRecordLeave>0)
if (printRecordLeave % pRecordNumber ==0)
pageNumber =printRecordLeave/pRecordNumber;
else
pageNumber =printRecordLeave/pRecordNumber+1;
else
pageNumber =0;
//正在打印的页数
printingPageNumber =0; //因为每打印一个新页都要计算还有多少页没有打印所以以打印的页数初始为0
//计算,余下的记录条数是否还可以在一页打印,不满一页时为假
if (dataTable1.Rows.Count-printingPageNumber*pRecordNumber>=pRecordNumber )
pageRecordNumber =pRecordNumber;
else
pageRecordNumber=(dataTable1.Rows.Count-printingPageNumber*pRecordNumber )% pRecordNumber ;
StringFormat fmt=new StringFormat();
fmt.LineAlignment = StringAlignment.Center;//上下对齐
fmt.FormatFlags = StringFormatFlags.LineLimit;//自动换行
Rectangle rect=new Rectangle(); //打印区域
Pen pen=new Pen(Brushes.Black,1); //打印表格线格式
while (printingPageNumber<=pageNumber)
{
fmt.Alignment = StringAlignment.Center;//表头中间对齐
rect.Width = pWidth - pLeft - pRight ;//表头和副表头宽度等于设置区域宽度
rect.Height = headHeight;
rect.X = pLeft;
rect.Y = pTop;
ev.Graphics.DrawString(headText, headFont, Brushes.Black, rect, fmt);//打印表头
fmt.Alignment = StringAlignment.Near;// '副表头左对齐
rect.Width = (pWidth - pLeft - pRight) / 2 - 1;
rect.Height = subHeadHeight;
rect.Y = pTop + headHeight;
ev.Graphics.DrawString(subHeadLeftText, subHeadFont, Brushes.Black, rect, fmt);//打印副表头左
fmt.FormatFlags = StringFormatFlags.DirectionRightToLeft;//右副表头文字从右往左排列
fmt.Alignment = StringAlignment.Near ; //右副表头右对齐
rect.X = pLeft + (pWidth - pLeft - pRight) / 2;
ev.Graphics.DrawString(subHeadRightText, subHeadFont, Brushes.Black, rect, fmt);//打印副表头右
fmt.Alignment = StringAlignment.Center;
rect.X = pLeft;
rect.Y = pTop + headHeight + subHeadHeight + (pRecordNumber + 1) * (y_unit) + subFootHeight;
rect.Height = footHeight;
rect.Width = pWidth - pLeft - pRight;
ev.Graphics.DrawString(footText, footFont, Brushes.Black, rect, fmt);// 打印表脚
fmt.Alignment = StringAlignment.Far;// 副表左左对齐
rect.X = pLeft;
rect.Y = pTop + headHeight + subHeadHeight + (pRecordNumber + 1) * (y_unit);
rect.Height = subFootHeight;
rect.Width = (pWidth - pLeft - pRight) / 2 - 1;
ev.Graphics.DrawString(subFootLeftText, subFootFont, Brushes.Black, rect, fmt);//打印左表脚
fmt.Alignment = StringAlignment.Near; //副表头右对齐
rect.X = pLeft + (pWidth - pLeft - pRight) / 2;
if (dataTable1.Rows.Count == 0 )
subFootRightText = "第" + totalPage + "页,共" + totalPage + "页";
else
subFootRightText = "第" + (totalPage - pageNumber + 1).ToString() + "页,共" + totalPage+ "页";
ev.Graphics.DrawString(subFootRightText, subFootFont, Brushes.Black, rect, fmt);// '打印右表脚
//得到datatable的所有列名
fmt.Alignment = StringAlignment.Center;
string []ColumnText=new string[dataTable1.Columns.Count];
string []ColumnID=new string[dataTable1.Columns.Count];
//画一条线
ev.Graphics.DrawLine(Pens.Black, pLeft, pTop + headHeight + subHeadHeight, pWidth - pLeft, pTop + headHeight + subHeadHeight);
for (int Cols = 0;Cols<dataTable1.Columns.Count;Cols++)
{ ColumnText[Cols] = dataTable1.Columns[Cols].Caption;
ColumnID[Cols] = dataTable1.Columns[Cols].ColumnName;
rect.X = pLeft + x_unit * Cols;
rect.Y = pTop + headHeight + subHeadHeight;
rect.Width = x_unit;
rect.Height = y_unit;
//画表头
ev.Graphics.DrawString(ColumnText[Cols], new Font(tableFont, FontStyle.Bold), drawBrush, rect, fmt);
}
ev.Graphics.DrawLine(Pens.Black, pLeft, rect.Height + rect.Y - 5, pWidth - pLeft, rect.Height + rect.Y - 5);
//结束---------------------得到datatable的所有列名
int printingLine = 0 ; //当前页面已经打印的记录行数
while(printingLine<pageRecordNumber)
{
dataGridRow = dataTable1.Rows[printRecordComplete]; //确定要当前要打印的记录的行号
for (Cols = 0;Cols<dataTable1.Columns.Count ;Cols++)
{ rect.X = pLeft + x_unit * Cols;
rect.Y = pTop + headHeight + subHeadHeight + (printingLine + 1) * (y_unit);
rect.Width = x_unit;
rect.Height = y_unit;
RectangleF convertedRectangle = rect;
if (dataGridRow[ColumnID[Cols]].ToString()!="")
ev.Graphics.DrawString(dataGridRow[ColumnID[Cols]].ToString(), tableFont, drawBrush, convertedRectangle, fmt);
}
printingLine += 1;
printRecordComplete += 1;
if (printRecordComplete >= dataTable1.Rows.Count)
{ ev.Graphics.DrawLine(Pens.Black, pLeft, rect.Y + rect.Height + 10, pWidth - pLeft, rect.Y + rect.Height + 10);
if (sum > 0 )
{
ev.Graphics.DrawString("总计:人民币" + (new PublicClass.ChineseMoney(Convert.ToDecimal(sum))).getChineseMoney()+"("+sum+")", tableFont, drawBrush, pLeft, rect.Y + rect.Height + 12);
ev.Graphics.DrawLine(Pens.Black, pLeft, rect.Y + rect.Height + tableFont.Height + 20, pWidth - pLeft, rect.Y + rect.Height + tableFont.Height + 20);
}
ev.Graphics.DrawString("制表人:" + worker + " 打印日期:" +DateTime.Now.Date, tableFont, drawBrush, pLeft, rect.Y + rect.Height + tableFont.Height + 20 + 5);
ev.HasMorePages = false;
printRecordComplete = 0;
return;
}
}
printingPageNumber += 1;
if (printingPageNumber >= pageNumber)
ev.HasMorePages = false;
else
{ ev.HasMorePages = true;
break;
}
}
}
#endregion
}
}
含有打印、统计DataGridView(2)的更多相关文章
- 含有打印、统计DataGridView(1)
using System;using System.Collections.Generic;using System.Text;using System.Drawing.Printing;using ...
- C语言字母频率统计
在进行密码破解时有时候需要得到字母出现的频率信息,下面我将简单的使用C语言来读取一个文件,然后统计该文件内的字母出现的频率. 1.在D盘下新建一个文本文件(文件名为"A.txt") ...
- Linux - wc统计文件行数、单词数或字节数
一 wc简单介绍 wc命令用来打印文件的文本行数.单词数.字节数等(print the number of newlines, words, and bytes in files).在Windows的 ...
- shell的wc命令统计 head tail命令详解
Linux系统中的wc(Word Count)命令的功能为统计指定文件中的字节数.字数.行数,并将统计结果显示输出. 1.命令格式: wc [选项]文件... 2.命令功能: 统计指定文件中的字节数. ...
- C# 使用PrintDocument 绘制表格 完成 打印预览
C# 使用PrintDocument 绘制表格 完成 打印预览 DataTable 经过不断的Google与baidu,最终整理出来的打印类 主要是根据两个参考的类组合而成,稍微修改了一下,参考代 ...
- C# 使用PrintDocument 绘制表格 完成 打印预览 DataTable
经过不断的Google与baidu,最终整理出来的打印类 主要是根据两个参考的类组合而成,稍微修改了一下,参考代码及来源见最后(其中一份是VB语言的) 其中遇到的一些问题也已经得到了解决(分页,打印预 ...
- C# WinForm开发系列 - DataGrid/DataGridView
在WinForm开发中,DataGrid/DataGridView被广泛使用于绑定数据库中数据进行呈现.整理一些关于DataGrid/DataGridView使用的文章,涉及DataGrid/Data ...
- Linux命令-统计文件中的字节数、字数、行数:wc
Linux系统中的wc(Word Count)命令的功能为统计指定文件中的字节数.字数.行数,并将统计结果显示输出. 1.命令格式: wc [选项]文件... 2.命令功能: 统计指定文件中的字节数. ...
- C# PrintDocument 打印表格
1.封装好的辅助类: using System; using System.Data; using System.Drawing; using System.Drawing.Printing; usi ...
随机推荐
- sql中使用正则查询
- Codefroces B. Hamming Distance Sum
Genos needs your help. He was asked to solve the following programming problem by Saitama: The lengt ...
- 记真实自己,炫精彩人生---《爱记》app使用体验
真的有款神器吗,能找到合适的Ta,能秀出自己的新生活,能让自己的心情舒爽,有,体验了下.就是爱记.果粉的福利. [爱记]是集心情记录.分享.评价与交流于一体的工具,TA是你心灵休憩的港湾,也是你记忆放 ...
- Viewpager切换时pager页面的生命周期变化
总结1: 当我们把ViewPager和Fragment合用的时候,切换页面时生命周期会发生对应的变化.变化规律:载入当前页面.前一个页面和后一个页面.我们来看一个实际測试效果图 打开应用会载入第一个页 ...
- session timer(一)
功能介绍 SIP并没有为所建立的会话定义存活机制. 虽然用户代理能够通过会话特定的机制推断会话是否超时,可是代理server却做不到这点. 如此一来.代理server有时会无法推断会话是否还是活动的. ...
- js--11对象的创建方式
<html> <head> <title>Object</title> </head> <body> <script ty ...
- 一款开源Office软件---Lotus Symphony在Linux系统下的应用
点击下载观看试用录像 Linux系统下的办公软件有OpenOffice.永中Office.红旗Red Office.金山Wps Office及StarOffice等,今天我为大家介绍IBM推进军O ...
- 错误日志写入到本地磁盘(lock 队列)
今天照常在b站上看看编程:看到有讲错误日志处理的,自己没写过日志,就看看了看: 主要是在讲的时候牵扯到了队列和lock的知识点:这方面知识自己了解的更少,那就记录一下.
- jersey+jetty实现文件上传
服务配置与启动类 import org.glassfish.jersey.servlet.ServletContainer; import javax.ws.rs.Path; import org.e ...
- python学习日记-180823
列表 a=[ ] 1.负数下标:a=[-1]指的是列表a最后一个下标,-2指倒数第二个下标 2.切片——利用切片获得子列表 a[1:4]——'1'切片开始处的下标,‘4’切片结束处的下标(不包括此下标 ...