C# 动态生成RDLC报表文件

本文介绍了如何利用C#动态生成RDLC报表,指出RDLC报表的便利性和局限性,特别是其XML格式的基础,允许通过修改数据库中的报表定义实现动态生成。通过将RDLC报表数据存储在数据库并按需转换为XML,实现了报表的灵活管理和创建。
摘要由CSDN通过智能技术生成

Visual Studio 中的报表有两种,水晶报表和微软自己的RDLC报表,水晶报表太复杂,一直不用。感觉rdlc的报表很好用,跟着web一起发布也很简单(只可惜只能在IE中使用)。不好的是太繁琐,每一张报表都必须先定义才能引用。不能动态生成。如果一个应用软件中有上百张报表,哪就够呛了。后来发现RDLC的报表文件其实是XML格式的文件,可以手工修改,哪就简单了。以下代码是我把rdlc报表格式保存在数据库,需要时还原成xml格式的RDLC报表,以达到动态生成报表的目的,报表的增删改,只需要改数据中的表就行了。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;

namespace customprint
{
    public class customprint
    {
        private DataRow rt { get; set; } //表定义行
        private DataSet ds { get; set; }
        private decimal tableheight { set; get; } //表格高度
        private string sql_str { set; get; } //connection string
        public customprint(string sql_str)
        {
            this.sql_str = sql_str;
            this.ds = new DataSet();
        }
        private void gene_ds(int tid)
        {
            //0:表定义 1行定义 2:更定义 3:表单元格定义 4:参数定义 5:页眉页脚其它单元格定义
            try
            {
                using (SqlConnection conn = new SqlConnection(sql_str))
                {
                    if (conn.State == ConnectionState.Closed) conn.Open();
                    string constr = string.Format("select * from a_t where id={0};select * from a_rows where tid={0} order by sh;select * from a_columns where tid={0} order by sh;select a.* from a_cells a join a_columns b on a.cid=b.id where a.tid={0} order by b.sh;select * from a_para where tid={0};select * from a_phpf where tid={0}", tid);
                    SqlDataAdapter dad = new SqlDataAdapter(constr, conn);
                    dad.Fill(ds);
                }
            }
            catch (Exception err) { throw err; }

        }
        public string get_xml(int tid)
        {
            gene_ds(tid);
            this.tableheight = get_bodyheight(ds.Tables[1]);
            this.rt = ds.Tables[0].Rows[0];
            StringBuilder cqw = new StringBuild
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值