我是把数据导入到EXCEL文件中,用EXCEL打印的

解决方案 »

  1.   

    具体程序代码如下:
    //连接数据库,准备循环取得每行记录的内容
    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");
    }
      

  2.   

    我确定得crystal report会比EXECL好,使用起来都简单.
      

  3.   

    laser0126(风铃草) ,谢谢你,有没有办法把公司的标志之类的图片插入?另外,这种方法不能实现预览功能,还要客户自己再处理一遍,你有没有做过直接输出到打印机的那种??henryfan1(henry) ,使用crystal report,我没有用过,刚才我简单看了一下,好象和vf中自定义报表差不多,我听说这个组件要注册,我如果现在写好给客户,会不会出现过期之类的问题?你有没有具体应用的例子介绍介绍??