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