本文介紹c#應用FastReport開發報表,因此首先附該工具下載地址:http://download.csdn.net/detail/hws1058648831a/6378499
下載解壓后可以直接使用,里面還有相關實例供參考。
下面看看一個簡單的例子:
1 打開visual studio 2010創建一個工程名稱為WfmFastReport,編輯界面,然后添加FastReport.dll ,FastReport.Bars.dll,FastReport.Editor.dll的引用。
FastReport.dll ,FastReport.Bars.dll,FastReport.Editor.dll在下載包里有的。
代碼非常簡單,如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using FastReport;
using FastReport.Editor;
namespace WfmFastReport
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private DataSet data;
private void button1_Click(object sender, EventArgs e)
{
string conStr = @"server=***;user id=**;password=***;initial catalog=****";
try
{
SqlConnection con = new SqlConnection(conStr);
con.Open();
SqlCommand sqlcmd = new SqlCommand();
sqlcmd.Connection = con;
sqlcmd.CommandText = "select top 100 * from item";
SqlDataAdapter sda = new SqlDataAdapter(sqlcmd);
data = new DataSet();
sda.Fill(data);
con.Close();
sda.Dispose();
MessageBox.Show("加載數據成功!");
}
catch (Exception err)
{
MessageBox.Show(err.Message);
}
}
private void button2_Click(object sender, EventArgs e)
{
try
{
FastReport.Report report = new FastReport.Report();
string filename = @"C:\Documents and Settings\Administrator\桌面\檢查項目.frx";
report.Load(filename);
report.RegisterData(data);
report.GetDataSource(data.Tables[0].TableName).Enabled = true;
report.Show();
}
catch (Exception err)
{
MessageBox.Show(err.Message);
}
}
}
}
檢查項目.frx文件就是FastReport設計出的報表模板,非常簡單,有興趣的朋友可以試一試。
下面看看效果: