各位在winform中怎么实现报表的打印预览和打印功能??

Dominic 2002-12-12 09:11:50
有什么方法吗?我第一次做这个~~

我看过ms的例子,是打印文本文件的,难道要先生成文本文件,再打印?有没有办法直接把生成的数据输出到打印机?这种方法的预览功能怎么做??关键还有表格线啊,怎么表示?

我看过使用crystal report的一些文章,但我有个疑问,既然是第三方组件,有没有注册啊,过期的可能,别做给客户的报表,2个月后不能用了,岂不是很麻烦??

有没有其他免费的组件?c#又很容易调用的那种。现在要求有预览,分页和打印功能~~~

急!!!
...全文
315 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Dominic 2002-12-12
  • 打赏
  • 举报
回复
laser0126(风铃草) ,谢谢你,有没有办法把公司的标志之类的图片插入?另外,这种方法不能实现预览功能,还要客户自己再处理一遍,你有没有做过直接输出到打印机的那种??

henryfan1(henry) ,使用crystal report,我没有用过,刚才我简单看了一下,好象和vf中自定义报表差不多,我听说这个组件要注册,我如果现在写好给客户,会不会出现过期之类的问题?你有没有具体应用的例子介绍介绍??
henryfan1 2002-12-12
  • 打赏
  • 举报
回复
我确定得crystal report会比EXECL好,使用起来都简单.
laser0126 2002-12-12
  • 打赏
  • 举报
回复
具体程序代码如下:
//连接数据库,准备循环取得每行记录的内容
string Strconnection;
SqlCommand mySqlcommand;
SqlConnection sqlconn;
SqlDataAdapter Dbread;

Dbread= new SqlDataAdapter();

Strconnection="data Source=localhost;initial catalog=Plan;persist security info=False;User id=sa;workstation id=localhost;packet size=4096";
sqlconn= new SqlConnection(Strconnection);

try
{
sqlconn.Open();

}
catch(Exception a)
{
MessageBox.Show(a.ToString());
}

mySqlcommand=new SqlCommand("select * from TJGL ",sqlconn);
DataSet DS=new DataSet();
Dbread.SelectCommand=mySqlcommand;
Dbread.SelectCommand.ExecuteNonQuery();
Dbread.Fill(DS,"TJGL");

//建立EXCEL对象,对EXCEL进行操作
Excel.Application oXL;
Excel._Workbook oWB;
Excel._Worksheet oSheet;
Excel.Range oRng;
try
{
//开始设置Excel对象
oXL= new Excel.ApplicationClass();
oXL.Visible=true;

//Get a new workbook
oWB= (Excel._Workbook)(oXL.Workbooks.Add(Missing.Value));
oSheet=(Excel._Worksheet)oWB.ActiveSheet;

//填写表头
oSheet.Cells[1,1]="工作项目";
oSheet.Cells[1,2]="技术要求";
oSheet.Cells[1,3]="时间";
oSheet.Cells[1,4]="劳动标准(天/人)";
oSheet.Cells[1,5]="次数";
oSheet.Cells[1,6]="金额(元/亩)";
oSheet.Cells[1,7]="其他";

//格式化表格内容
oSheet.get_Range("A1","G1").Font.Bold=true;
oSheet.get_Range("A1","G1").VerticalAlignment=Excel.XlVAlign.xlVAlignCenter;
oRng= oSheet.get_Range("A1","G1");
oRng.EntireColumn.AutoFit();

//填写数据库中的内容到相应的字段下


for( int i=2;i<DS.Tables["TJGL"].Rows.Count+1; i++)
{

string[,] saNames= new string[1,7];


oSheet.Cells[i,1]=DS.Tables["TJGL"].Rows[i].ItemArray[1].ToString();
oSheet.Cells[i,2]=DS.Tables["TJGL"].Rows[i].ItemArray[3].ToString();
oSheet.Cells[i,3]=DS.Tables["TJGL"].Rows[i].ItemArray[4].ToString();
oSheet.Cells[i,4]=DS.Tables["TJGL"].Rows[i].ItemArray[5].ToString();
oSheet.Cells[i,5]=DS.Tables["TJGL"].Rows[i].ItemArray[6].ToString();
oSheet.Cells[i,6]=DS.Tables["TJGL"].Rows[i].ItemArray[7].ToString();
oSheet.Cells[i,7]=DS.Tables["TJGL"].Rows[i].ItemArray[8].ToString();

//oRng= oSheet.get_Range("C2","C6"); 使用EXCEL里面得运算格式
//oRng.Formula="=A2&\"\"& B2";

oRng=oSheet.get_Range("F2","F"+DS.Tables["TJGL"].Rows.Count);
oRng.NumberFormat="¥0.00";
oXL.Visible=true;
oXL.UserControl=true;

}
}
catch (Exception theException)
{
String errorMessage;
errorMessage="Error:";
errorMessage=String.Concat(errorMessage,theException.Message);
errorMessage=String.Concat(errorMessage,"Line:");
errorMessage=String.Concat(errorMessage,theException.Source);

MessageBox.Show(errorMessage,"Error");
}
laser0126 2002-12-12
  • 打赏
  • 举报
回复
我是把数据导入到EXCEL文件中,用EXCEL打印的

111,116

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧