4,816
社区成员
发帖
与我相关
我的任务
分享
MyReport.PrintToPrinter(1, true, 0, 0);
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using CrystalDecisions.Shared;
using CrystalDecisions.CrystalReports.Engine;
using System.Reflection;
using System.Diagnostics;
public partial class WebForms_Hospital_Default :PageBase
{
protected void Page_Load(object sender, EventArgs e)
{
try
{
string strPath = Request["Path"].ToString().Trim();
DownloadFile(Page.Response, strPath);
}
catch (Exception ex)
{
this.ShowMessage(ex.Message);
}
}
#region 下载服务器上的文件
/// <summary>
/// 下载服务器上的文件
/// </summary>
/// <param name="PageResponse">程序中可以设置参数:HttpResponse ht=Page.Response;</param>
/// <param name="serverPath">服务器上的文件路径</param>
public void DownloadFile(HttpResponse response, string serverPath)
{
FileStream fs = null;
try
{
fs = File.OpenRead(serverPath);
byte[] buffer = new byte[1024];
long count = 1024;
response.Buffer = true;
response.AddHeader("Connection", "Keep-Alive");
response.ContentType = "application/octet-stream";
response.AddHeader("Content-Disposition", "attachment;filename=" + Path.GetFileName(serverPath));//下载时要保存的默认文件名
response.AddHeader("Content-Length", fs.Length.ToString());
while (count == 1024)
{
count = fs.Read(buffer, 0, 1024);
response.BinaryWrite(buffer);
}
}
catch
{
}
finally
{
fs.Close();
}
}
#endregion
}
MyReport.PrintToPrinter(1, true, 0, 0);
myReport.ExportToDisk(ExportFormatType.PortableDocFormat, @"d:\ae0000.pdf");
//将处理好的报表对象缓存
Session["myRpt"]=MyReport;
CrystalReportViewer1.ReportSource = MyReport;
if (Session["myRpt"]!=null)
{
CrystalReportViewer1.ReportSource = (ReportDocument)Session["myRpt"];
}
MyReport.PrintToPrinter(1, true, 0, 0);
myReport.ExportToDisk(ExportFormatType.PortableDocFormat, @"d:\ae0000.pdf");
//定义载入报表按钮Button_LoadReport的Click事件
protected void Button_LoadReport_Click(object sender, EventArgs e)
{
if (DropDownList_City.SelectedValue == "" || DropDownList_Company.SelectedValue == "" || DropDownList_Year.SelectedValue == "" || DropDownList_Month.SelectedValue == "")
{
Response.Write("<script language='javascript'>window.alert('请选择正确的城市、企业及年月');</script>");
}
else
{
GypPriceStatisticReport ReturnGypPriceStatisticReport = new GypPriceStatisticReport();
string ssdh = DropDownList_City.SelectedValue.Trim();
string qydh = DropDownList_Company.SelectedValue.Trim();
string StatisticTableID = DropDownList_StatisticReportName.SelectedValue.Trim();
string InputYearMonth = DropDownList_Year.SelectedValue.Trim() + DropDownList_Month.SelectedValue.Trim();
string GypYsDataTableName="";
if (StatisticTableID == "V201") //注意此处的V201的大小写要与数据库中的值一致
{
GypYsDataTableName = "GypCys";
}
else
{
GypYsDataTableName = "GypGys";
}
ReturnGypPriceStatisticReport = conn.GypGetStatisticReport(ssdh, qydh, StatisticTableID, "0001", InputYearMonth, GypYsDataTableName);
ReportDocument MyReport = new ReportDocument();
string ReportPath = Server.MapPath("../GypPrice/GypPriceStatisticReport.rpt");
MyReport.Load(ReportPath);
MyReport.SetDataSource(ReturnGypPriceStatisticReport);
//将报表中的年、月文本对象赋予DropDownList_Year和DropDownList_Month选择的值
((TextObject)MyReport.ReportDefinition.ReportObjects["Text_InputYear"]).Text = DropDownList_Year.SelectedValue.Trim();
((TextObject)MyReport.ReportDefinition.ReportObjects["Text_InputMonth"]).Text = DropDownList_Month.SelectedValue.Trim();
MyReport.PrintToPrinter(1, true, 0, 0);
CrystalReportViewer1.ReportSource = MyReport;
//CrystalReportViewer1.DataBind();
}
}