asp.net 动态生成rdlc报表(原创)
程序员文章站
2024-03-07 14:19:03
复制代码 代码如下: string ccount = ""; string dcount = ""; string jcount = ""; protected void...
复制代码 代码如下:
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;
}
技巧 可以先在报表里自己设计好需要的格式 用
xmldocument sourcedoc = new xmldocument();
string path = appdomain.currentdomain.basedirectory + "orders.rdlc";
然后用sourcedoc .save()保存生成xml
可以看到此xml你需要改的格式是哪个地方
上一篇: Java 图片与byte数组互相转换实例
下一篇: php输出图像的方法实例分析