简单的reportViewer开发 是由三个部分组成
1: 数据源
2:页面模板层次
3:asp显示嵌入
1:固定模板开发(列不会变化)
可以用visual studio
-->new 一个.xsd 里面放置数据源 其实很像一个视图
你查询数据填充进去
-->new 一个.rdlc
把数据源和.xsd 绑定 显示在对应的位置 并作一些显示行列调整
注:其中rdlc里面的函数 是以vb的语法为基础的 eg:iff..
可以用它作一些页面数据显示的操作 eg:日期转换,数据为空处理
footer里面做求和...什么的
-->new 一个asp 把对应的ReportViewer 嵌套进去
<rsweb:ReportViewer ID="ProjectTestAssessByUserRV" runat="server" Width="100%" Height="100%"
ShowPrintButton="False" ShowRefreshButton="False" ShowFindControls="False"
BackColor="#DBE6F0" BorderColor="#BBBCB1" BorderWidth="1px"
Font-Names="Verdana" Font-Size="8pt">
<LocalReport ReportPath="你命名的.rdlc">
</LocalReport>
</rsweb:ReportViewer>
在.aspx.cs中
DataTable dt = new DataTable(); 你取得的数据 //注意列名和数据源里面一样
ReportDataSource ds = new ReportDataSource("数据源名称", dt);
ProjectTestAssessByBmRV.LocalReport.DataSources.Clear();
ProjectTestAssessByBmRV.LocalReport.DataSources.Add(ds);
ProjectTestAssessByBmRV.LocalReport.DisplayName = "报表名称";
ProjectTestAssessByBmRV.LocalReport.EnableHyperlinks = true;
ProjectTestAssessByBmRV.HyperlinkTarget = "_blank";
这样一个简单的报表就形成了 很简单 而且 reportviewer提供导出 打印等功能 你自己添加对应的属性 就OK了
2:列不固定 的动态报表
此时 你没法预定义数据源 和.rdlc
这样就必须你自己手动的创建了
实验了下
生成rdlc类
using System;
using System.Data;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
public class RdlGenerator
{
private List<string> m_allFields;
private List<string> m_selectedFields;
public List<string> AllFields
{
get { return m_allFields; }
set { m_allFields = value; }
}
public List<string> SelectedFields
{
get { return m_selectedFields; }
set { m_selectedFields = value; }
}
private string url;
public string Url
{
get { return url; }
set { url = value; }
}
private Rdl.Report CreateReport()
{
Rdl.Report report = new Rdl.Report();
report.Items = new object[]
{
CreateDataSources(),
CreateBody(),
CreateDataSets(),
"6.5in",
};
report.ItemsElementName = new Rdl.ItemsChoiceType37[]
{
Rdl.ItemsChoiceType37.DataSources,
Rdl.ItemsChoiceType37.Body,
Rdl.ItemsChoiceType37.DataSets,
Rdl.ItemsChoiceType37.Width,
};
return report;
}
private Rdl.DataSourcesType CreateDataSources()
{
Rdl.DataSourcesType dataSources = new Rdl.DataSourcesType();
dataSources.DataSource = new Rdl.DataSourceType[] { CreateDataSource() };
return dataSources;
}
private Rdl.DataSourceType CreateDataSource()
{
Rdl.DataSourceType dataSource = new Rdl.DataSourceType();
dataSource.Name = "DummyDataSource";
dataSource.Items = new object[] { CreateConnectionProperties() };
return dataSource;
}
private Rdl.ConnectionPropertiesType CreateConnectionProperties()
{
Rdl.ConnectionPropertiesType connectionProperties = new Rdl.ConnectionPropertiesType();
connectionProperties.Items = new object[]
{
"",
"SQL",
};
connectionProperties.ItemsElementName = new Rdl.ItemsChoiceType[]
{
Rdl.ItemsChoiceType.ConnectString,
Rdl.ItemsChoiceType.DataProvider,
};
return connectionProperties;
}
private Rdl.BodyType CreateBody()
{
Rdl.BodyType body = new Rdl.BodyType();
body.Items = new object[]
{
CreateReportItems(),
"1in",
};
body.ItemsElementName = new Rdl.ItemsChoiceType30[]
{
Rdl.ItemsChoiceType30.ReportItems,
Rdl.ItemsChoiceType30.Height,
};
return body;
}
private Rdl.ReportItemsType CreateReportItems()
{
Rdl.ReportItemsType reportItems = new Rdl.ReportItemsType();
TableRdlGenerator tableGen = new TableRdlGenerator(Url);
tableGen.Fields = m_selectedFields;
reportItems.Items = new object[] { tableGen.CreateTable() };
return reportItems;
}
private Rdl.DataSetsType CreateDataSets()
{
Rdl.DataSetsType dataSets = new Rdl.DataSetsType();
dataSets.DataSet = new Rdl.DataSetType[] { CreateDataSet() };
return dataSets;
}
private Rdl.DataSetType CreateDataSet()
{
Rdl.DataSetType dataSet = new Rdl.DataSetType();
dataSet.Name = "ReportData";
dataSet.Items = new object[] { CreateQuery(), CreateFields() };
return dataSet;
}
private Rdl.QueryType CreateQuery()
{
Rdl.QueryType query = new Rdl.QueryType();
query.Items = new object[]
{
"DummyDataSource",
"",
};
query.ItemsElementName = new Rdl.ItemsChoiceType2[]
{
Rdl.ItemsChoiceType2.DataSourceName,
Rdl.ItemsChoiceType2.CommandText,
};
return query;
}
private Rdl.FieldsType CreateFields()
{
Rdl.FieldsType fields = new Rdl.FieldsType();
fields.Field = new Rdl.FieldType[m_allFields.Count];
for (int i = 0; i < m_allFields.Count; i++)
{
fields.Field[i] = CreateField(m_allFields[i]);
}
return fields;
}
private Rdl.FieldType CreateField(String fieldName)
{
Rdl.FieldType field = new Rdl.FieldType();
field.Name = fieldName;
field.Items = new object[] { fieldName };
field.ItemsElementName = new Rdl.ItemsChoiceType1[] { Rdl.ItemsChoiceType1.DataField };
return field;
}
public void WriteXml(Stream stream)
{
XmlSerializer serializer = new XmlSerializer(typeof(Rdl.Report));
serializer.Serialize(stream, CreateReport());
}
}
对应的table生成类
using System;
using System.Data;
using System.Collections.Generic;
using System.Text;
public class TableRdlGenerator
{
private List<string> m_fields;
public List<string> Fields
{
get { return m_fields; }
set { m_fields = value; }
}
string hyperLinkUrl;
public TableRdlGenerator() { }
public TableRdlGenerator(string hyperLinkUrl)
: this()
{ this.hyperLinkUrl = hyperLinkUrl; }
public Rdl.TableType CreateTable()
{
Rdl.TableType table = new Rdl.TableType();
table.Name = "ReportTable";
table.Items = new object[]
{
CreateTableColumns(),
CreateHeader(),
CreateDetails(),
CreateFooter(),
};
table.ItemsElementName = new Rdl.ItemsChoiceType21[]
{
Rdl.ItemsChoiceType21.TableColumns,
Rdl.ItemsChoiceType21.Header,
Rdl.ItemsChoiceType21.Details,
Rdl.ItemsChoiceType21.Footer,
};
return table;
}
private Rdl.FooterType CreateFooter()
{
Rdl.FooterType footer = new Rdl.FooterType();
footer.Items = new object[]
{
CreateFooterTableRows(),
true,
};
footer.ItemsElementName = new Rdl.ItemsChoiceType20[]
{
Rdl.ItemsChoiceType20.TableRows,
Rdl.ItemsChoiceType20.RepeatOnNewPage,
};
return footer;
}
private Rdl.HeaderType CreateHeader()
{
Rdl.HeaderType header = new Rdl.HeaderType();
header.Items = new object[]
{
CreateHeaderTableRows(),
true,
};
header.ItemsElementName = new Rdl.ItemsChoiceType20[]
{
Rdl.ItemsChoiceType20.TableRows,
Rdl.ItemsChoiceType20.RepeatOnNewPage,
};
return header;
}
private Rdl.TableRowsType CreateFooterTableRows()
{
Rdl.TableRowsType footerTableRows = new Rdl.TableRowsType();
footerTableRows.TableRow = new Rdl.TableRowType[] { CreateFooterTableRow() };
return footerTableRows;
}
private Rdl.TableRowsType CreateHeaderTableRows()
{
Rdl.TableRowsType headerTableRows = new Rdl.TableRowsType();
headerTableRows.TableRow = new Rdl.TableRowType[] { CreateHeaderTableRow() };
return headerTableRows;
}
private Rdl.TableRowType CreateFooterTableRow()
{
Rdl.TableRowType footerTableRow = new Rdl.TableRowType();
footerTableRow.Items = new object[] { CreateFooterTableCells(), "0.25in" };
return footerTableRow;
}
private Rdl.TableRowType CreateHeaderTableRow()
{
Rdl.TableRowType headerTableRow = new Rdl.TableRowType();
headerTableRow.Items = new object[] { CreateHeaderTableCells(), "0.25in" };
return headerTableRow;
}
private Rdl.TableCellsType CreateFooterTableCells()
{
Rdl.TableCellsType footerTableCells = new Rdl.TableCellsType();
footerTableCells.TableCell = new Rdl.TableCellType[m_fields.Count];
for(int i=0;i<m_fields.Count;i++)
{
footerTableCells.TableCell[i] = CreateFooterTableCell(m_fields[i]);
}
return footerTableCells;
}
private Rdl.TableCellsType CreateHeaderTableCells()
{
Rdl.TableCellsType headerTableCells = new Rdl.TableCellsType();
headerTableCells.TableCell = new Rdl.TableCellType[m_fields.Count];
for (int i = 0; i < m_fields.Count; i++)
{
headerTableCells.TableCell[i] = CreateHeaderTableCell(m_fields[i]);
}
return headerTableCells;
}
private Rdl.TableCellType CreateFooterTableCell(string fieldName)
{
Rdl.TableCellType footerTableCell = new Rdl.TableCellType();
footerTableCell.Items = new object[] { CreateFooterTableCellReportItems(fieldName) };
return footerTableCell;
}
private Rdl.TableCellType CreateHeaderTableCell(string fieldName)
{
Rdl.TableCellType headerTableCell = new Rdl.TableCellType();
headerTableCell.Items = new object[] { CreateHeaderTableCellReportItems(fieldName) };
return headerTableCell;
}
private Rdl.ReportItemsType CreateFooterTableCellReportItems(string fieldName)
{
Rdl.ReportItemsType footerTableCellReportItems = new Rdl.ReportItemsType();
footerTableCellReportItems.Items = new object[] { CreateFooterTableCellTextbox(fieldName) };
return footerTableCellReportItems;
}
private Rdl.ReportItemsType CreateHeaderTableCellReportItems(string fieldName)
{
Rdl.ReportItemsType headerTableCellReportItems = new Rdl.ReportItemsType();
headerTableCellReportItems.Items = new object[] { CreateHeaderTableCellTextbox(fieldName) };
return headerTableCellReportItems;
}
private Rdl.TextboxType CreateFooterTableCellTextbox(string fieldName)
{
Rdl.TextboxType footerTableCellTextbox = new Rdl.TextboxType();
footerTableCellTextbox.Name = fieldName + "_Footer";
for(int i=0;i<m_fields.Count;i++)
if (fieldName.Contains("日期"))
{
footerTableCellTextbox.Items = new object[]
{
"合计:",
CreateFooterTableCellTextboxStyle(),
true,
};
footerTableCellTextbox.ItemsElementName = new Rdl.ItemsChoiceType14[]
{
Rdl.ItemsChoiceType14.Value,
Rdl.ItemsChoiceType14.Style,
Rdl.ItemsChoiceType14.CanGrow,
};
}
else
{
footerTableCellTextbox.Items = new object[]
{
"=Sum(CDbl(Fields!"+fieldName+".Value))",
CreateFooterTableCellTextboxStyle(),
true,
};
footerTableCellTextbox.ItemsElementName = new Rdl.ItemsChoiceType14[]
{
Rdl.ItemsChoiceType14.Value,
Rdl.ItemsChoiceType14.Style,
Rdl.ItemsChoiceType14.CanGrow,
};
}
return footerTableCellTextbox;
}
private Rdl.TextboxType CreateHeaderTableCellTextbox(string fieldName)
{
Rdl.TextboxType headerTableCellTextbox = new Rdl.TextboxType();
headerTableCellTextbox.Name = fieldName + "_Header";
headerTableCellTextbox.Items = new object[]
{
fieldName,
CreateHeaderTableCellTextboxStyle(),
true,
};
headerTableCellTextbox.ItemsElementName = new Rdl.ItemsChoiceType14[]
{
Rdl.ItemsChoiceType14.Value,
Rdl.ItemsChoiceType14.Style,
Rdl.ItemsChoiceType14.CanGrow,
};
return headerTableCellTextbox;
}
private Rdl.StyleType CreateFooterTableCellTextboxStyle()
{
Rdl.StyleType footerTableCellTextboxStyle = new Rdl.StyleType();
footerTableCellTextboxStyle.Items = new object[]
{
CreateBorderStyle(),
"700",
"9pt",
"Center",
"Middle",
"White",
"宋体",
};
footerTableCellTextboxStyle.ItemsElementName = new Rdl.ItemsChoiceType5[]
{
Rdl.ItemsChoiceType5.BorderStyle,
Rdl.ItemsChoiceType5.FontWeight,
Rdl.ItemsChoiceType5.FontSize,
Rdl.ItemsChoiceType5.TextAlign,
Rdl.ItemsChoiceType5.VerticalAlign,
Rdl.ItemsChoiceType5.BackgroundColor,
Rdl.ItemsChoiceType5.FontFamily,
};
return footerTableCellTextboxStyle;
}
private Rdl.StyleType CreateHeaderTableCellTextboxStyle()
{
Rdl.StyleType headerTableCellTextboxStyle = new Rdl.StyleType();
headerTableCellTextboxStyle.Items = new object[]
{
CreateBorderStyle(),
"700",
"9pt",
"Center",
"Middle",
"WhiteSmoke",
"SimSun",
};
headerTableCellTextboxStyle.ItemsElementName = new Rdl.ItemsChoiceType5[]
{
Rdl.ItemsChoiceType5.BorderStyle,
Rdl.ItemsChoiceType5.FontWeight,
Rdl.ItemsChoiceType5.FontSize,
Rdl.ItemsChoiceType5.TextAlign,
Rdl.ItemsChoiceType5.VerticalAlign,
Rdl.ItemsChoiceType5.BackgroundColor,
Rdl.ItemsChoiceType5.FontFamily,
};
return headerTableCellTextboxStyle;
}
private Rdl.DetailsType CreateDetails()
{
Rdl.DetailsType details = new Rdl.DetailsType();
details.Items = new object[] { CreateTableRows() };
return details;
}
private Rdl.TableRowsType CreateTableRows()
{
Rdl.TableRowsType tableRows = new Rdl.TableRowsType();
tableRows.TableRow = new Rdl.TableRowType[] { CreateTableRow() };
return tableRows;
}
private Rdl.TableRowType CreateTableRow()
{
Rdl.TableRowType tableRow = new Rdl.TableRowType();
tableRow.Items = new object[] { CreateTableCells(), "0.25in" };
return tableRow;
}
private Rdl.TableCellsType CreateTableCells()
{
Rdl.TableCellsType tableCells = new Rdl.TableCellsType();
tableCells.TableCell = new Rdl.TableCellType[m_fields.Count];
for (int i = 0; i < m_fields.Count; i++)
{
tableCells.TableCell[i] = CreateTableCell(m_fields[i], i);
}
return tableCells;
}
private Rdl.TableCellType CreateTableCell(string fieldName, int index)
{
Rdl.TableCellType tableCell = new Rdl.TableCellType();
tableCell.Items = new object[] { CreateTableCellReportItems(fieldName, index) };
return tableCell;
}
private Rdl.ReportItemsType CreateTableCellReportItems(string fieldName, int index)
{
Rdl.ReportItemsType reportItems = new Rdl.ReportItemsType();
Rdl.TextboxType textbox = new Rdl.TextboxType();
if (index == 0 && !string.IsNullOrEmpty(hyperLinkUrl))
{
Rdl.ActionType action = new Rdl.ActionType();
action.Items = new object[] {"=" + hyperLinkUrl + "&Fields!" + fieldName + ".Value"};
action.ItemsElementName = new Rdl.ItemsChoiceType8[] { Rdl.ItemsChoiceType8.Hyperlink };
textbox.Name = fieldName;
textbox.Items = new object[]
{
"=Fields!" + fieldName + ".Value",
CreateTableCellTextboxStyle(true),
true,
action
};
textbox.ItemsElementName = new Rdl.ItemsChoiceType14[]
{
Rdl.ItemsChoiceType14.Value,
Rdl.ItemsChoiceType14.Style,
Rdl.ItemsChoiceType14.CanGrow,
Rdl.ItemsChoiceType14.Action
};
}
else
{
//"=Fields!" + fieldName + ".Value",
textbox.Name = fieldName;
if (fieldName.Equals("日期"))
{
textbox.Items = new object[]
{
"=IIF(Fields!日期.Value <> \"\", new DateTime(new DateTime(1970,1,1,8,0,0).Ticks + Fields!日期.Value * 10000).ToString(\"yyyy年MM月dd日\"),\"\")",
CreateTableCellTextboxStyle(false),
true
};
textbox.ItemsElementName = new Rdl.ItemsChoiceType14[]
{
Rdl.ItemsChoiceType14.Value,
Rdl.ItemsChoiceType14.Style,
Rdl.ItemsChoiceType14.CanGrow
};
}
else
{
//"=IIF(Fields!" + fieldName + ".Value <> \"\",Trim(Fields!" + fieldName + ".Value),\"0\")",
//"=Fields!" + fieldName + ".Value",
textbox.Items = new object[]
{
"=IIF(Fields!" + fieldName + ".Value is Nothing,0,Fields!" + fieldName + ".Value)",
CreateTableCellTextboxStyle(false),
true
};
textbox.ItemsElementName = new Rdl.ItemsChoiceType14[]
{
Rdl.ItemsChoiceType14.Value,
Rdl.ItemsChoiceType14.Style,
Rdl.ItemsChoiceType14.CanGrow
};
}
}
reportItems.Items = new object[] { textbox };
return reportItems;
}
private Rdl.StyleType CreateTableCellTextboxStyle(bool isLinek)
{
Rdl.StyleType style = new Rdl.StyleType();
string color = "Black";
if (isLinek) color = "Blue";
style.Items = new object[]
{
// "=iif(RowNumber(Nothing) mod 2, \"AliceBlue\", \"White\")",
"White",
"Center",
"Middle",
CreateBorderStyle(),
"SimSun",
color
};
style.ItemsElementName = new Rdl.ItemsChoiceType5[]
{
Rdl.ItemsChoiceType5.BackgroundColor,
Rdl.ItemsChoiceType5.TextAlign,
Rdl.ItemsChoiceType5.VerticalAlign,
Rdl.ItemsChoiceType5.BorderStyle,
Rdl.ItemsChoiceType5.FontFamily,
Rdl.ItemsChoiceType5.Color
};
return style;
}
private Rdl.BorderColorStyleWidthType CreateBorderStyle()
{
Rdl.BorderColorStyleWidthType bstyle = new Rdl.BorderColorStyleWidthType();
bstyle.Items = new object[]
{
"Solid",
};
bstyle.ItemsElementName = new Rdl.ItemsChoiceType3[]
{
Rdl.ItemsChoiceType3.Default,
};
return bstyle;
}
private Rdl.TableColumnsType CreateTableColumns()
{
Rdl.TableColumnsType tableColumns = new Rdl.TableColumnsType();
tableColumns.TableColumn = new Rdl.TableColumnType[m_fields.Count];
for (int i = 0; i < m_fields.Count; i++)
{
tableColumns.TableColumn[i] = CreateTableColumn();
}
return tableColumns;
}
private Rdl.TableColumnType CreateTableColumn()
{
Rdl.TableColumnType tableColumn = new Rdl.TableColumnType();
tableColumn.Items = new object[] { "2in" };
return tableColumn;
}
}
这两个类的作用 就是动态生成rdlc并绑定数据源的核心代码 相信看的懂java和c#的都可以明白
再我们在asp里面还是一样加一个reportviewer
核心代码
//显示页面 在pageLoad可以调用它 传入你对应的数据
private void ShowReport(DataTable dt)
{
this.rv.Reset();
this.rv.LocalReport.LoadReportDefinition(memoryStream);
this.Prv.LocalReport.DataSources.Add(new ReportDataSource("ReportData", dt));
rv.LocalReport.Refresh();
rv.LocalReport.EnableHyperlinks = true;
rv.HyperlinkTarget = "_blank";
rv.LocalReport.DisplayName = "test";
}
//生成rdlc
private MemoryStream GenerateRdl(List<string> allFields, List<string> selectedFields, string url)
{
MemoryStream ms = new MemoryStream();
RdlGenerator gen = new RdlGenerator();
gen.Url = "";
gen.AllFields = allFields;
gen.SelectedFields = selectedFields;
gen.WriteXml(ms);
ms.Position = 0;
return ms;
}
只要你有心拼凑 肯定可以做出一个动态的报表
因为我搞.net只是随意学习 (一直作java方面开发) 所以很多东西只是作个代码的展示 但是我想 要是有人做这方面的东西 还是有点用的 嘿嘿