Asp.Net中使用水晶報(bào)表(下)
2024-07-10 12:56:24
供稿:網(wǎng)友
 
使用push模式 
  我們采用下面的幾步使用push模式執(zhí)行水晶報(bào)表:
  1. 設(shè)計(jì)一個(gè)dataset
  2. 創(chuàng)建一個(gè).rpt文件同時(shí)將其指定給上一步建立的dataset。
  3. 在aspx頁面中拖放一個(gè)crystalreportviewer控件同時(shí)將其與前面的rpt文件建立聯(lián)系。
  4. 在代碼中訪問數(shù)據(jù)庫并把數(shù)據(jù)存入dataset
  5. 調(diào)用databind方法。
   設(shè)計(jì)一個(gè)dataset
  1) 右擊“解決方案瀏覽器”,選擇“添加”--“添加新項(xiàng)”-->“數(shù)據(jù)集”
  2) 從“服務(wù)器資源管理器”中的“sql server”中拖放“stores”表(位于pubs數(shù)據(jù)庫中)。
 
  3) 此時(shí)在數(shù)據(jù)集中就會(huì)有一個(gè)stores表的結(jié)構(gòu)圖。
  - .xsd文件中僅僅包含一個(gè)結(jié)構(gòu)圖,但是不會(huì)有任何數(shù)據(jù)在里面。
  創(chuàng)建 .rpt 文件 :
  4) 使用上面的介紹過的方法創(chuàng)建此文件,唯一的不同就是使用數(shù)據(jù)集來代替前面的直接連接數(shù)據(jù)。
  5)建立.rpt文件之后,右擊“詳細(xì)資料”-->"添加/刪除數(shù)據(jù)庫“
  6) 在”數(shù)據(jù)庫專家“窗口中,展開”項(xiàng)目數(shù)據(jù)“(代替以前的oledb),展開“ado.net數(shù)據(jù)集”--"dataset1“,選擇”stores“表。
  7) 將”stores"表添加到“選定的表”中,點(diǎn)擊“ok”
  8) 使用pull模式下的方法,建立一個(gè)webform
  建立一個(gè)crystal report viewer 控件
  9) 建立一個(gè)crystal report viewer 控件,并設(shè)定其屬性,此處與pull模式下是一致的。
  code behind 代碼:
  10) 在page_load方法中使用下面的子函數(shù):
vb.net代碼:
  sub bindreport()
    dim myconnection as new sqlclient.sqlconnection() 
    myconnection.connectionstring= "server= (local)/netsdk;database=pubs;trusted_connection=yes"
    dim mycommand as new sqlclient.sqlcommand()
    mycommand.connection = myconnection
    mycommand.commandtext = "select * from stores"
    mycommand.commandtype = commandtype.text
    dim myda as new sqlclient.sqldataadapter()
    myda.selectcommand = mycommand
    dim myds as new dataset1()
    '這就是我們在設(shè)計(jì)模式上使用的dataset 
    myda.fill(myds, "stores") 
    '你不得不使用與你前面dataset相同名字。
    dim orpt as new crystalreport1()
    ' 水晶報(bào)表綁定
    orpt.setdatasource(myds)
    ' 設(shè)定水晶報(bào)表的reportsource
    crystalreportviewer1.reportsource = orpt
  end sub
 
c#代碼:
private void bindreport()
{
  string strprovider = "server=(local);database=pubs;uid=sa;pwd=";
  crystalreport1 ocr = new crystalreport1();
  dataset1 ds = new dataset1();
  sqlconnection myconn = new sqlconnection(strprovider);
  myconn.open();
  string strsel = "select * from stores";
  sqldataadapter myadapter = new sqldataadapter(strsel,myconn);
  myadapter.fill(ds,"stores");
  ocr.setdatasource(ds);
  this.crystalreportviewer1.reportsource = ocr;
}
 
  注意:在上面的代碼中,你得注意一下orpt是"strongly typed"的報(bào)表文件。如果你需要使用"untyped"報(bào)表,你得使用reportdocument對象,然后再調(diào)用報(bào)表文件。
  運(yùn)行你的程序。 
  11) 運(yùn)行你的程序
  將報(bào)表文件導(dǎo)出成為其它格式
  你能夠?qū)?bào)表文件導(dǎo)出成為下列格式:
      1. pdf (portable document format) 
      2. doc (ms word document) 
      3. xls (ms excel spreadsheet) 
      4. html (hyper text markup language – 3.2 or 4.0 compliant) 
      5. rtf (rich text format) 
  使用pull模式導(dǎo)出報(bào)表
  當(dāng)導(dǎo)出使用pull模式創(chuàng)建的文件時(shí),水晶報(bào)表準(zhǔn)確地打開所需要的數(shù)據(jù),下面是執(zhí)行導(dǎo)出功能的代碼:
c#代碼:
vb.net代碼:
private sub button1_click(byval sender as system.object, byval e as system.eventargs) handles button1.click
  dim myreport as crystalreport1 = new crystalreport1()
  '注意:這里我們建立一個(gè)strong-typed的水晶報(bào)表實(shí)例。
    dim diskopts as crystaldecisions.shared.diskfiledestinationoptions = new crystaldecisions.shared.diskfiledestinationoptions()
    myreport.exportoptions.exportdestinationtype = crystaldecisions.[shared].exportdestinationtype.diskfile
  ' 導(dǎo)出成為其它文件時(shí)也需要這個(gè)選項(xiàng)
  ' 如microsoft exchange, mapi等. 
    myreport.exportoptions.exportformattype = crystaldecisions. [shared].exportformattype.portabledocformat
  '這里我們導(dǎo)出成為.pdf格式文件,你也能選擇上面的其它類型文件
    diskopts.diskfilename = "c:/output.pdf"
  '如果你不指定確切的目錄,那么文件就會(huì)保存到[windows]/system32目錄中去了
    myreport.exportoptions.destinationoptions = diskopts
  '水晶報(bào)表文件不包含直接的filename屬性,因此你不能直接指定保存的文件名
  '所以你不得不使用diskfiledestinationoptions對象,設(shè)置它的diskfilename屬性
  '為你想要的路徑,最后將水晶報(bào)表的destinationsoptions屬性指定為上面的diskfiledestinationoption
    myreport.export()
  '上面的代碼將完成導(dǎo)出工作。
end sub
 
  使用push模式導(dǎo)出水晶報(bào)表
  當(dāng)導(dǎo)出的報(bào)表是由push模式建立的時(shí),第一步就是通過編程建立連接并組裝dataset,設(shè)置報(bào)表的的setdatasource屬性。再下面的步驟就有pull模式一樣的了。