C#可以控制打印机么?谁有教程或者代码
想用C#写个程序,用户点击某个按钮,可以打印相关数据,查了一下,VC++,VB的有,不知道C#可不可以,我没有学过VC++和VB,谁有教程或者代码可以分享一下么.......
想用C#写个程序,用户点击某个按钮,可以打印相关数据,查了一下,VC++,VB的有,不知道C#可不可以,我没有学过VC++和VB,谁有教程或者代码可以分享一下么....
展开
展开全部
C#中打印控件的使用
在同学的帮助下终于把,打印的程序搞定了^_^.弄出来共享一下吧. 首先弄一个printDocument控件,然后在打印的按钮中直接调用printDocument1.print()事件. 再次嘛就是写printDocument的PrintPag事件了. 下面是我昨天弄好的一个弄出来看看吧。
private void button1_Click(object sender, EventArgs e)
{
printDocument1.Print();
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
Font tabelTextFont = new Font("宋体", 10);
if (dataGridView1.DataBindings != null)
{
int[] columnsWidth = new int[dataGridView1.Columns.Count];//得到所有列的个数
int[] columnsLeft=new int[dataGridView1.Columns.Count]; //
for (int c = 0; c < columnsWidth.Length; c++)//得到列标题的宽度
{
columnsWidth[c] = (int)e.Graphics.MeasureString(dataGridView1.Columns[c].HeaderText , tabelTextFont).Width;
}
for (int rowIndex = 0; rowIndex < dataGridView1.Rows.Count; rowIndex++)//rowindex当前行
{
for (int columnIndex = 0; columnIndex < dataGridView1.Columns.Count; columnIndex++)//当前列
{
int w = (int)e.Graphics.MeasureString(dataGridView1.Columns[columnIndex].Name , tabelTextFont).Width; columnsWidth[columnIndex] = w > columnsWidth[columnIndex] ? w : columnsWidth[columnIndex];
}
}
int rowHidth = 20;
int tableLeft=60;
int tableTop=70;
columnsLeft[0]=tableLeft;
for (int i=1;i<=columnsWidth.Length -1;i++)
{
columnsLeft[i] = columnsLeft[i - 1] + columnsWidth[i - 1]+15;
}
StringFormat sf=new StringFormat ();
sf.Alignment=StringAlignment.Center ;//居中打印
e.Graphics.DrawString("欢迎石印死了开的交流!", new Font("宋体", 15), Brushes.Black, new Point(e.PageBounds.Width / 2, 20),sf );//打印标题
for (int c = 0; c < columnsWidth.Length; c++)//打印表中的列名
{
e.Graphics.DrawString(dataGridView1.Columns[c].HeaderText,new Font ("宋体",10,FontStyle.Bold), Brushes.Black, new Point(columnsLeft[c], tableTop)); e.Graphics.DrawLine(Pens.Black, new Point(columnsLeft[c]-5, tableTop - 5), new Point(columnsLeft[c]-5, tableTop + (dataGridView1 .Rows .Count+1)*rowHidth));
}
e.Graphics.DrawLine(Pens.Black, new Point(columnsLeft[dataGridView1.Columns.Count - 1] + columnsWidth[dataGridView1.Columns.Count - 1], tableTop - 5), new Point(columnsLeft[dataGridView1.Columns.Count - 1] + columnsWidth[dataGridView1.Columns.Count - 1], tableTop + (dataGridView1.Rows.Count + 1) * rowHidth));//画最后面的线
e.Graphics.DrawLine(Pens.Black, new Point(columnsLeft[0] - 5, tableTop - 5), new Point(columnsLeft[dataGridView1.Columns.Count - 1] + columnsWidth[dataGridView1.Columns.Count - 1], tableTop - 5)); for (int rowIndex = 0; rowIndex < dataGridView1.Rows.Count; rowIndex++)//打印表中的内容
{
for (int columnIndex = 0; columnIndex < dataGridView1.Columns.Count; columnIndex++)
{
e.Graphics.DrawString(dataGridView1.Rows[rowIndex].Cells[columnIndex].Value.ToString(), tabelTextFont, Brushes.Black, new Point(columnsLeft[columnIndex], tableTop + rowHidth * (rowIndex + 1)));
}
e.Graphics.DrawLine(Pens.Black, new Point(columnsLeft[0]-5, tableTop + (rowIndex +1) * rowHidth-5), new Point(columnsLeft[dataGridView1.Columns.Count - 1] + columnsWidth[dataGridView1.Columns.Count - 1], tableTop + (rowIndex +1)*rowHidth-5));//循环画行
}
}
另外要是想有打印预览的话,那就还要一个printPreviewDialog控件了。要把该控件的document事件和PrintDocument关联起来,就可以了。
在同学的帮助下终于把,打印的程序搞定了^_^.弄出来共享一下吧. 首先弄一个printDocument控件,然后在打印的按钮中直接调用printDocument1.print()事件. 再次嘛就是写printDocument的PrintPag事件了. 下面是我昨天弄好的一个弄出来看看吧。
private void button1_Click(object sender, EventArgs e)
{
printDocument1.Print();
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
Font tabelTextFont = new Font("宋体", 10);
if (dataGridView1.DataBindings != null)
{
int[] columnsWidth = new int[dataGridView1.Columns.Count];//得到所有列的个数
int[] columnsLeft=new int[dataGridView1.Columns.Count]; //
for (int c = 0; c < columnsWidth.Length; c++)//得到列标题的宽度
{
columnsWidth[c] = (int)e.Graphics.MeasureString(dataGridView1.Columns[c].HeaderText , tabelTextFont).Width;
}
for (int rowIndex = 0; rowIndex < dataGridView1.Rows.Count; rowIndex++)//rowindex当前行
{
for (int columnIndex = 0; columnIndex < dataGridView1.Columns.Count; columnIndex++)//当前列
{
int w = (int)e.Graphics.MeasureString(dataGridView1.Columns[columnIndex].Name , tabelTextFont).Width; columnsWidth[columnIndex] = w > columnsWidth[columnIndex] ? w : columnsWidth[columnIndex];
}
}
int rowHidth = 20;
int tableLeft=60;
int tableTop=70;
columnsLeft[0]=tableLeft;
for (int i=1;i<=columnsWidth.Length -1;i++)
{
columnsLeft[i] = columnsLeft[i - 1] + columnsWidth[i - 1]+15;
}
StringFormat sf=new StringFormat ();
sf.Alignment=StringAlignment.Center ;//居中打印
e.Graphics.DrawString("欢迎石印死了开的交流!", new Font("宋体", 15), Brushes.Black, new Point(e.PageBounds.Width / 2, 20),sf );//打印标题
for (int c = 0; c < columnsWidth.Length; c++)//打印表中的列名
{
e.Graphics.DrawString(dataGridView1.Columns[c].HeaderText,new Font ("宋体",10,FontStyle.Bold), Brushes.Black, new Point(columnsLeft[c], tableTop)); e.Graphics.DrawLine(Pens.Black, new Point(columnsLeft[c]-5, tableTop - 5), new Point(columnsLeft[c]-5, tableTop + (dataGridView1 .Rows .Count+1)*rowHidth));
}
e.Graphics.DrawLine(Pens.Black, new Point(columnsLeft[dataGridView1.Columns.Count - 1] + columnsWidth[dataGridView1.Columns.Count - 1], tableTop - 5), new Point(columnsLeft[dataGridView1.Columns.Count - 1] + columnsWidth[dataGridView1.Columns.Count - 1], tableTop + (dataGridView1.Rows.Count + 1) * rowHidth));//画最后面的线
e.Graphics.DrawLine(Pens.Black, new Point(columnsLeft[0] - 5, tableTop - 5), new Point(columnsLeft[dataGridView1.Columns.Count - 1] + columnsWidth[dataGridView1.Columns.Count - 1], tableTop - 5)); for (int rowIndex = 0; rowIndex < dataGridView1.Rows.Count; rowIndex++)//打印表中的内容
{
for (int columnIndex = 0; columnIndex < dataGridView1.Columns.Count; columnIndex++)
{
e.Graphics.DrawString(dataGridView1.Rows[rowIndex].Cells[columnIndex].Value.ToString(), tabelTextFont, Brushes.Black, new Point(columnsLeft[columnIndex], tableTop + rowHidth * (rowIndex + 1)));
}
e.Graphics.DrawLine(Pens.Black, new Point(columnsLeft[0]-5, tableTop + (rowIndex +1) * rowHidth-5), new Point(columnsLeft[dataGridView1.Columns.Count - 1] + columnsWidth[dataGridView1.Columns.Count - 1], tableTop + (rowIndex +1)*rowHidth-5));//循环画行
}
}
另外要是想有打印预览的话,那就还要一个printPreviewDialog控件了。要把该控件的document事件和PrintDocument关联起来,就可以了。
参考资料: http://blog.sina.com.cn/s/blog_60b45f230100edgi.html
展开全部
Excel.Application xApp = new Excel.ApplicationClass();
xApp.Visible = true;
Excel.Workbook xBook = xApp.Workbooks._Open(path,
Missing.Value, Missing.Value, Missing.Value, Missing.Value
, Missing.Value, Missing.Value, Missing.Value, Missing.Value
, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
xBook.PrintPreview(true);
xBook.PrintOut( Missing.Value ,Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value);
xSheet = null;
xBook = null;
xApp.Quit();
xApp = null;
xApp.Visible = true;
Excel.Workbook xBook = xApp.Workbooks._Open(path,
Missing.Value, Missing.Value, Missing.Value, Missing.Value
, Missing.Value, Missing.Value, Missing.Value, Missing.Value
, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
xBook.PrintPreview(true);
xBook.PrintOut( Missing.Value ,Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value);
xSheet = null;
xBook = null;
xApp.Quit();
xApp = null;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
c#使用控件就可以了 很简单实现
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
有关于打印机的 控件 可以试试
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
当然可以
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询