本帖最后由 Lenka.Guo 于 2016-5-16 14:52 编辑
ActiveReportsWeb 到处excel时报No data has been set. Please specify either a DataSet or a DataView to use SqlConnection myConn = new SqlConnection(ConfigurationManager.ConnectionStrings["connstr"].ConnectionString);
private DataSet dataSetData;
public DataSet DataSetData
{
get { return dataSetData; }
}
private DataSet LoadDataToDataSet()
{
string productSql = "select top 10 * From tb_borrowInfo";
dataSetData = new DataSet();
SqlCommand cmd = new SqlCommand(productSql, myConn);
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = cmd;
adapter.Fill(dataSetData, "tb_borrowInfo");
return dataSetData;
}
protected void Page_Load(object sender, EventArgs e)
{
GrapeCity.ActiveReports.PageReport report1 = new GrapeCity.ActiveReports.PageReport(new System.IO.FileInfo(Server.MapPath("PageReport1.rdlx")));
report1.Document.LocateDataSource += new GrapeCity.ActiveReports.LocateDataSourceEventHandler(WebViewer1_LocateDataSource1);//绑定数据
WebViewer1.PdfExportOptions.FitWindow = false;//全屏编辑
WebViewer1.PdfExportOptions.DisplayMode = GrapeCity.ActiveReports.Export.Pdf.Section.DisplayMode.Outlines;//显示模式
WebViewer1.Report = report1;
}
private void WebViewer1_LocateDataSource1(object sender, GrapeCity.ActiveReports.LocateDataSourceEventArgs args)
{
args.Data = LoadDataToDataSet();
}
/// <summary>
/// 导出excel
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button1_Click(object sender, EventArgs e)
{
GrapeCity.ActiveReports.PageReport _reportDef = new GrapeCity.ActiveReports.PageReport(new System.IO.FileInfo(Server.MapPath("PageReport1.rdlx")));
_reportDef.Report.DataSources[0].DataSourceReference = "";
string productSql = string.Format("SELECT top 10 * From tb_borrowInfo");
_reportDef.Report.DataSets[0].Query.CommandText = productSql;
GrapeCity.ActiveReports.Document.PageDocument _reportRuntime = new GrapeCity.ActiveReports.Document.PageDocument(_reportDef);
GrapeCity.ActiveReports.Export.Excel.Section.XlsExport XlsExport1 = new GrapeCity.ActiveReports.Export.Excel.Section.XlsExport();
System.IO.MemoryStream ms = new System.IO.MemoryStream();
XlsExport1.FileFormat = GrapeCity.ActiveReports.Export.Excel.Section.FileFormat.Xlsx;
XlsExport1.Export(_reportRuntime, ms);
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", Server.UrlPathEncode("attachment;filename=产品.xlsx"));
Response.BinaryWrite(ms.ToArray());
Response.End();
}
|
|