分享

动态生成rdlc 报表

 乐自垂帘 2012-03-03

动态生成rdlc 报表

2009-03-18 15:04:30 阅读(114) 发表评论

  因为公司需求 研究微软的Reportviewer 因为有许多特别要求所以动态调用 比较灵活

  我的需求是 根据数据不同的合并表头 (参考了随心所欲的博客文档 再次表示感谢)

双击代码全选
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
string cCount = "";
 
   string dCount = "";
 
   string jCount = "";
 
  
 
   protected void Page_Load(object sender, EventArgs e)
 
   {
 
       if (!IsPostBack)
 
       {
 
           string id = Request.QueryString["OrderID"] == null ? "1" : Request.QueryString["OrderID"].ToString();
 
           SqlConnection con = new SqlConnection("server=CHENZQ;uid=sa;pwd=luca623;database=luca");
 
           SqlDataAdapter sda = new SqlDataAdapter("select * from view_Order where C_orderID='" + id + "'", con);
 
           DataSet ds = new DataSet();
 
           sda.Fill(ds);
 
          
 
           cCount = ds.Tables[0].Rows[0]["C_CTime"].ToString();
 
           dCount = ds.Tables[0].Rows[0]["C_TTime"].ToString();
 
           jCount = ds.Tables[0].Rows[0]["C_DTime"].ToString();
 
          //这段代码是最重要
 
           ReportViewer1.Reset();
 
           this.ReportViewer1.LocalReport.LoadReportDefinition(GenerateRdlc());
 
           ReportViewer1.LocalReport.DataSources.Clear();          //Orders_DataTable1 数据源名字必须和此报表原绑定的数据源名相同
 
           this.ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("Orders_DataTable1", ds.Tables[0]));
 
           this.ReportViewer1.LocalReport.Refresh();
 
       }
 
    
 
   }
 
  //这个方法就是自定义报表的样式
 
   public MemoryStream GenerateRdlc()
 
   {
 
       XmlDocument sourceDoc = new XmlDocument();
 
 
 
       string path = AppDomain.CurrentDomain.BaseDirectory + "Orders.rdlc";
 
       //c_CTime = ds.Tables[0].Rows[0]["C_CTime"].ToString();
 
    
 
       sourceDoc.Load(path);
 
       //下面就是xml操作了 没必要看我的 根据自己的需求而做
 
        XmlNode xHeader = sourceDoc.ChildNodes.Item(1).ChildNodes.Item(13).ChildNodes.Item(1).ChildNodes.Item(0).ChildNodes.Item(4);
 
       XmlNode xCells = xHeader.ChildNodes.Item(0).ChildNodes.Item(0).ChildNodes.Item(0);
 
     
 
       //建设期
 
       XmlNode xmlCell = xCells.ChildNodes.Item(1);
 
       XmlElement xeCol = sourceDoc.CreateElement("ColSpan");
 
       xeCol.InnerText = cCount;
 
       xeCol.InnerXml = cCount;
 
       xmlCell.InnerXml += xeCol.OuterXml;
 
       XmlNode xmlCellValue = xmlCell.ChildNodes.Item(0).ChildNodes.Item(0).ChildNodes.Item(4);
 
       xmlCellValue.InnerXml = "建设期";
 
       xmlCellValue.InnerText = "建设期";
 
       XmlNode xnRemove;
 
       for (int i = 0; i <int.Parse(cCount) - 1; i++)
 
       {
 
           xnRemove = xCells.ChildNodes.Item(2);
 
           xCells.RemoveChild(xnRemove);
 
       }
 
       //投产期
 
       XmlNode xmlCellT = xCells.ChildNodes.Item(2);
 
       XmlElement xeColT = sourceDoc.CreateElement("ColSpan");
 
       xeColT.InnerText = dCount;
 
       xeColT.InnerXml = dCount;
 
       xmlCellT.InnerXml += xeColT.OuterXml;
 
       XmlNode xmlCellValueT = xmlCellT.ChildNodes.Item(0).ChildNodes.Item(0).ChildNodes.Item(4);
 
       xmlCellValueT.InnerXml = "投产期";
 
       xmlCellValueT.InnerText = "投产期";
 
       for (int j = 0; j < int.Parse(dCount) - 1; j++)
 
       {
 
           xnRemove = xCells.ChildNodes.Item(3);
 
           xCells.RemoveChild(xnRemove);
 
       }
 
       //生产期
 
       XmlNode xmlCellC = xCells.ChildNodes.Item(3);
 
       XmlElement xeColC = sourceDoc.CreateElement("ColSpan");
 
       xeColC.InnerText = jCount.ToString();
 
       xeColC.InnerXml = jCount.ToString();
 
       xmlCellC.InnerXml += xeColC.OuterXml;
 
       XmlNode xmlCellValueC = xmlCellC.ChildNodes.Item(0).ChildNodes.Item(0).ChildNodes.Item(4);
 
       xmlCellValueC.InnerXml = "生产期";
 
       xmlCellValueC.InnerText = "生产期";
 
       for (int j = 0; j < int.Parse(jCount) - 1; j++)
 
       {
 
           xnRemove = xCells.ChildNodes.Item(4);
 
           xCells.RemoveChild(xnRemove);
 
       }
 
       MemoryStream ms = new MemoryStream();
 
       XmlSerializer serializer = new XmlSerializer(typeof(XmlDocument));
 
       serializer.Serialize(ms, sourceDoc);
 
       ms.Position = 0;
 
       return ms;
   }

  技巧 可以先在报表里自己设计好需要的格式 用

双击代码全选
1
2
3
XmlDocument sourceDoc = new XmlDocument();
 
        string path = AppDomain.CurrentDomain.BaseDirectory + "Orders.rdlc";

  然后用sourceDoc .save()保存生成xml

  可以看到此xml你需要改的格式是哪个地方

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多