使用FastReport.Net创建报告

时间:2016-06-30 11:38:55

标签: c# asp.net .net report fastreport

我已安装FastReport.Net来生成报告。

我收录了以下参考资料:

FastReport.dll , FastReport.Web.dll , FastReport.Bars.dll , FastReport.Editor.dll , FastReport.Service.dll

此外,我在ToolBox中创建了一个名为FastReport.Net的自定义Tab,并将FastReport控件/组件添加到选项卡和我拥有的控件:

Pointer and WebReport

我已经在我的.aspx页面上拖放了WebReport组件,这使我的web.config像这样:

<?xml version="1.0"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0">
      <assemblies>
        <add assembly="FastReport.Web, Version=2016.3.13.0, Culture=neutral, PublicKeyToken=DB7E5CE63278458C" />
        <add assembly="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
        <add assembly="FastReport, Version=2016.3.13.0, Culture=neutral, PublicKeyToken=DB7E5CE63278458C" />
      </assemblies>
    </compilation>
  </system.web>
  <connectionStrings>
    <add name="ConnectionString" connectionString="Data Source=WIN-SERVER;Initial Catalog=RndDatabase;User ID=development;Password=development" providerName="System.Data.SqlClient" />
  </connectionStrings>
  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <handlers>
      <add name="FastReportHandler" path="FastReport.Export.axd" verb="*" type="FastReport.Web.Handlers.WebExport" />
    </handlers>
  </system.webServer>
</configuration>

我的问题是如何使用FastReport从数据库获取数据并打印报告。

我在项目中创建了一个dataset.xsd但是当我点击FastReport设计中的Select DataSource...时( ctrl + Shift + F10 )它打开一个弹出窗口,没有可供选择的选项。

还有其他程序吗?

修改

我试过这个:

using System;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.SqlClient;
using System.Web.UI.WebControls;

using FastReport;
using FastReport.Web.Handlers;
using FastReport.Wizards;
using FastReport.Utils;
using FastReport.TypeConverters;
using FastReport.Code;
using FastReport.MSChart;
using FastReport.Data;
using FastReport.Design.StandardDesigner;


namespace fastreports4
{
    public partial class fastrepo : System.Web.UI.Page
    {
        string sql = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public DataSet BindData()
        {
            DataSet _dataSet = new DataSet();
            SqlConnection con = new SqlConnection(sql);
            SqlCommand cmd = new SqlCommand("select * from cities", con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(_dataSet);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
            return _dataSet;
        }


        protected void Button1_Click(object sender, EventArgs e)
        {
            DataSet _dataSet = new DataSet();
            _dataSet = BindData();
            Report report = new Report();
            // register the "Cities" table
            report.RegisterData(_dataSet.Tables[0], "Cities");
            // enable it to use in a report
            report.GetDataSource("Cities").Enabled = true;
            // create A4 page with all margins set to 1cm
            ReportPage page1 = new ReportPage();
            page1.Name = "Page1";
            report.Pages.Add(page1);
            // create ReportTitle band
            page1.ReportTitle = new ReportTitleBand();
            page1.ReportTitle.Name = "FastReport";
            // set its height to 1.5cm
            page1.ReportTitle.Height = Units.Centimeters * 1.5f;
            // create group header
            GroupHeaderBand group1 = new GroupHeaderBand();
            group1.Name = "Cities Data";
            group1.Height = Units.Centimeters * 1;
            // set group condition
            group1.Condition = "[Cities.CityName]";//[Cities.CityName].Substring(0, 1)
            // add group to the page.Bands collection
            page1.Bands.Add(group1);
            // create group footer
            group1.GroupFooter = new GroupFooterBand();
            group1.GroupFooter.Name = "GroupFooter1";
            group1.GroupFooter.Height = Units.Centimeters * 1;
            // create DataBand
            DataBand data1 = new DataBand();
            data1.Name = "Data1";
            data1.Height = Units.Centimeters * 0.5f;
            // set data source
            data1.DataSource = report.GetDataSource("Cities");
            // connect databand to a group
            group1.Data = data1;
            // create "Text" objects
            // report title
            TextObject text1 = new TextObject();
            text1.Name = "Text1";
            // set bounds
            text1.Bounds = new System.Drawing.RectangleF(0, 0,
            Units.Centimeters * 19, Units.Centimeters * 1);
            // set text
            text1.Text = "CitiesData";
            // set appearance
            text1.HorzAlign = HorzAlign.Center;
            text1.Font = new System.Drawing.Font("Tahoma", 14, FontStyle.Bold);
            // add it to ReportTitle
            page1.ReportTitle.Objects.Add(text1);
            // group
            TextObject text2 = new TextObject();
            text2.Name = "Text2";
            text2.Bounds = new RectangleF(0, 0,
            Units.Centimeters * 2, Units.Centimeters * 1);
            text2.Text = "[Cities.CityName]";//[Cities.CityName].Substring(0, 1)
            text2.Font = new Font("Tahoma", 10, FontStyle.Bold);
            // add it to GroupHeader
            group1.Objects.Add(text2);
            report.Show();
        }
    }
}

在这里,我想生成一个按钮点击报告,当点击按钮生成报告时,它会在行report.Show();上给我一个错误说:

  

DragDrop注册没有成功。

当我继续执行时,它会在report.Show();停止执行。

1 个答案:

答案 0 :(得分:2)

一种方法是可以使用名为WebReport的FastReport控件。

为此,您需要做的就是在ToolBox步骤中创建一个新的自定义Tab,如下所示:

  • 右键单击工具箱,然后单击创建名为FastReport的新选项卡(您可以给出任何名称)
  • 添加FastReport的dll
  • 在此拖放Webreport控件并创建报告之后。
  • 如果要从菜单中添加DataSource与数据库进行交互。

就是这样!