如题,我这个小程序是用的System.Drawing.Printing;空间里的pritDocument类,实现获取数据库里的一个names字段的所有名称(有3个)放在arraylist中;连续打印在A4纸的指定位置,每张纸上打印一个名称,有几个名字就打印几张纸,都在纸的指定位置。就类似于在请贴上打印客人名字一样的,多少个客人就打印多少张,每一张只有一个名字。呵呵,有点罗嗦,但小第真的很急,下面是我写的代码(当然了有问题无法正确输出,打印机用的是HP Deskjet 1000 J110 series,其实什么打印机无所谓的),跪求大虾赐教,也可以qq联系我:496319641.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;using System.Drawing.Printing;
using System.Data.SqlClient;
using System.Data;
using System.Collections;
using System.Drawing;
namespace WebApplication4
{
public partial class bei : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
} public ArrayList getarr() //该函数返回数据库中客人名单放置在arraylist中并返回
{
ArrayList arr = new ArrayList();
SqlConnection myConnection = new SqlConnection(@"server=192.168.2.60\sql2008;database=im;uid=sqluser;pwd=123456");
SqlDataAdapter myCommand = new SqlDataAdapter("select * from users", myConnection);
DataSet ds = new DataSet();
myCommand.Fill(ds);
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
string str;
str = ds.Tables[0].Rows[i][1].ToString();
arr.Add(str);
} myConnection.Close();
myCommand.Dispose();
return arr;
} protected void Button1_Click(object sender, EventArgs e) //单击该按钮开始打印
{
ArrayList arr=getarr();
if (arr.Count != 0)
{
try
{
PrintDocument pd = new PrintDocument();
PageSettings pageset = new PageSettings();
pageset.Landscape = false;
pageset.PaperSize.RawKind = 9; pd.DefaultPageSettings = pageset;
pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
pd.Print();
}
catch(Exception eww)
{
Response.Write(eww.Message);
}
}
} int num = 0;
private void pd_PrintPage(object sender, PrintPageEventArgs ev) //打印时间函数
{
ArrayList arra=getarr();
Font printFont = new Font("Arial", 14); ev.Graphics.DrawString(arra[num++].ToString(), printFont, Brushes.Black,
new PointF(20,20));
// If more lines exist, print another page.
if(num <= arra.Count)
ev.HasMorePages = true;
else
ev.HasMorePages = false;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;using System.Drawing.Printing;
using System.Data.SqlClient;
using System.Data;
using System.Collections;
using System.Drawing;
namespace WebApplication4
{
public partial class bei : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
} public ArrayList getarr() //该函数返回数据库中客人名单放置在arraylist中并返回
{
ArrayList arr = new ArrayList();
SqlConnection myConnection = new SqlConnection(@"server=192.168.2.60\sql2008;database=im;uid=sqluser;pwd=123456");
SqlDataAdapter myCommand = new SqlDataAdapter("select * from users", myConnection);
DataSet ds = new DataSet();
myCommand.Fill(ds);
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
string str;
str = ds.Tables[0].Rows[i][1].ToString();
arr.Add(str);
} myConnection.Close();
myCommand.Dispose();
return arr;
} protected void Button1_Click(object sender, EventArgs e) //单击该按钮开始打印
{
ArrayList arr=getarr();
if (arr.Count != 0)
{
try
{
PrintDocument pd = new PrintDocument();
PageSettings pageset = new PageSettings();
pageset.Landscape = false;
pageset.PaperSize.RawKind = 9; pd.DefaultPageSettings = pageset;
pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
pd.Print();
}
catch(Exception eww)
{
Response.Write(eww.Message);
}
}
} int num = 0;
private void pd_PrintPage(object sender, PrintPageEventArgs ev) //打印时间函数
{
ArrayList arra=getarr();
Font printFont = new Font("Arial", 14); ev.Graphics.DrawString(arra[num++].ToString(), printFont, Brushes.Black,
new PointF(20,20));
// If more lines exist, print another page.
if(num <= arra.Count)
ev.HasMorePages = true;
else
ev.HasMorePages = false;
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货