using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
namespace ********
{
public class DataSet
{
public DataTable CreatDataSet()
{
DataTable dt = new DataTable();
dt.Columns.Add("A");
dt.Columns.Add("B");
dt.Columns.Add("C");
return dt;
}
}
}
ReportViewer viewer = new ReportViewer();
viewer.ProcessingMode = ProcessingMode.Local;
viewer.LocalReport.ReportEmbeddedResource = "***.Page.Report.Report1.rdlc";
ReportDataSource rds_1 = new ReportDataSource("DataSet1", dtReport);//DataSet1為報表裏面的數據源名稱
viewer.LocalReport.DataSources.Add(rds_1);
ReportParameter rp1 = new ReportParameter("參數1","參數1的值" );//給參數賦值
ReportParameter rp2 = new ReportParameter("參數2","參數2的值" );
viewer.LocalReport.SetParameters(new ReportParameter[] {rp1, rp2 });
Warning[] warnings;
string[] streamIds;
string mimeType = string.Empty;
string encoding = string.Empty;
string extension = string.Empty;
byte[] bytes = viewer.LocalReport.Render("Excel", null, out mimeType, out encoding, out extension, out streamIds, out warnings);
//Excel ,PDF ,Word 等格式
// Now that you have all the bytes representing the PDF report, buffer it and send it to the client.
Response.Buffer = true;
Response.Clear();
Response.ContentType = mimeType;
Response.AddHeader("content-disposition", "attachment; filename=1_" + DateTime.Now.ToString("yyyyMMddhhssmm") + "" + "." + extension);
Response.BinaryWrite(bytes); // create the file
Response.Flush(); // send it to the client to download