当前位置: 代码迷 >> ASP.NET >> 怎么打印 DataTable
  详细解决方案

怎么打印 DataTable

热度:5133   发布时间:2013-02-25 00:00:00.0
如何打印 DataTable
就是当我点击Button1的时候,我想在这个按钮的点击事件 将 DataTablet 中的内容打印出来,如何实现??

------解决方案--------------------------------------------------------
http://blog.csdn.net/lanwilliam/archive/2008/05/13/2440073.aspx
------解决方案--------------------------------------------------------
同为网上找来的 看是否有用

//以表格形式打印DataTable内容

//定义全局变量count,储存当前打印的行数
int count=0;

//定义一个方法,接收一个DataTable类型参数及PrintDocument的PrintPage事件传入的参数e以方便操作

private void PrintTable(DataTable dt, System.Drawing.Printing.PrintPageEventArgse)


//取得对应的Graphics对象
Graphicsg = e.Graphics;

//获得相关页面X坐标、Y坐标、打印区域宽度、长度
intx = e.PageSettings.Margins.Left;
inty = e.PageSettings.Margins.Top;
intwidth = e.PageSettings.PaperSize.Width -e.PageSettings.Margins.Left- e.PageSettings.Margins.Right;
intheight = e.PageSettings.PaperSize.Height -e.PageSettings.Margins.Top -e.PageSettings.Margins.Bottom;

//定义打印字体
Fontfont = new Font("宋体",15);

//rowCount是除去打印过的行数后剩下的行数
introwCount = dt.Rows.Count-count;

//maxPageRow是当前设置下该页面可以打印的最大行数
intmaxPageRow=height/(int)font.GetHeight();

//因为是表格,先画一条水平直线
g.DrawLine(newPen(Brushes.Black, 1), new Point(x, y), new Point(x +(dt.Columns.Count) * 135, y));

//再画出表格各列的列标题
for(int i = 0; i < dt.Columns.Count; i++)
{
stringhead = dt.Columns[i].ColumnName;
g.DrawString(head,font, Brushes.Black, x + i * 135, y);
}

//画完标题,再画一条直线
g.DrawLine(newPen(Brushes.Black, 1), new Point(x, y + (int)font.GetHeight()), newPoint(x + (dt.Columns.Count) * 135, y +(int)font.GetHeight()));

//判断,如果剩下的行数小于可打印的最大行数,则执行下列代码
if(maxPageRow >=rowCount)
{
//当前行数小于Table内总行数时,循环
while(count < dt.Rows.Count)
{

//内循环打印Table内行数据
intcolumnCount = 0;
while(columnCount < dt.Columns.Count)
{
stringtemp = dt.Rows[count][columnCount].ToString();

//打印每个单元格内的数据
g.DrawString(temp,font, Brushes.Black, x + columnCount * 135, y + (count %maxPageRow) * (int)font.GetHeight() +(int)font.GetHeight());

//打印完一行后,继续打印一条直线
g.DrawLine(newPen(Brushes.Black, 1), new Point(x, y + (count % maxPageRow) *(int)font.GetHeight() + 2 * (int)font.GetHeight()), new Point(x +(dt.Columns.Count) * 135, y + (count % maxPageRow) *(int)font.GetHeight() + 2 * (int)font.GetHeight()));
columnCount++;
}
count++;
}

//所有数据打印完毕后,打印垂直直线

for (int i = 0; i <= dt.Columns.Count; i++)
{
g.DrawLine(newPen(Brushes.Black), new Point(x + i * 135, y), new Point(x + i *135, y + rowCount * (int)font.GetHeight() +(int)font.GetHeight()));
}
}

//判断,如果剩下的行数大于可打印的最大行数,则执行下列代码
else
{
do
{

//与上面类似,注意下面while的条件
intcolumnCount = 0;
while(columnCount < dt.Columns.Count)
{
stringtemp = dt.Rows[count][columnCount].ToString();

//打印每个单元格
g.DrawString(temp,font, Brushes.Black, x + columnCount * 135, y + (count %maxPageRow) * (int)font.GetHeight() +(int)font.GetHeight());

//打印水平直线
g.DrawLine(newPen(Brushes.Black, 1), new Point(x, y + (count % maxPageRow) *(int)font.GetHeight() + 2 * (int)font.GetHeight()), new Point(x +(dt.Columns.Count)*135, y + (count % maxPageRow) *(int)font.GetHeight() + 2 * (int)font.GetHeight()));
columnCount++;
}
count++;
}while ((count % maxPageRow >0));

//打印垂直直线
for(int i = 0; i <= dt.Columns.Count;i++ )
{
g.DrawLine(newPen(Brushes.Black), new Point(x + i * 135, y), new Point(x + i *135, y + height + (int)font.GetHeight()));
}

}

//指定HasMorePages值,如果页面最大行数小于剩下的行数,则返回true(还有),否则返回false
if(maxPageRow<rowCount)
{
e.HasMorePages= true;
}
else
{
e.HasMorePages= false;
count= 0;
}
}

//-----------------------------------------------------------------------------------------

//打印预览

private void button2_Click(object sender, EventArgs e)
{

//注意指定其Document属性
this.printPreviewDialog1.Document = this.printDocument1;
this.printPreviewDialog1.ShowDialog();


//打印

 private void button1_Click(object sender, EventArgs e)
{

//同样注意指定Document属性
this.printDialog1.Document = this.printDocument1;
if (this.printDialog1.ShowDialog() == DialogResult.OK)
{

//触发PrintDocument的PrintPage事件
this.printDocument1.Print();
}
}




------解决方案--------------------------------------------------------
http://topic.csdn.net/u/20070131/18/bdca6315-430a-4a21-a436-8ff75e096f52?1747598582
------解决方案--------------------------------------------------------
UP
------解决方案--------------------------------------------------------
顶,不太清楚,但先导出再打印是绝对没有问题滴
------解决方案--------------------------------------------------------
可以利用 水晶报表 打印,方便直观。
1)dataset ds=new dataset();
再构建table tb
 再把table 添加到ds中。 ds.add.tb....
2)再把 报表绑定到 ds. 就可打印了。
  相关解决方案