using Microsoft.Reporting.WinForms;
1 创建报表文件TagsReport.rdlc
2 从工具箱拖一个table放在报表上,会自动配置数据源,建立dataset,可以选数据库,连接表,也可以选程序里的对象如ControlEase.AI.DataRecord.Record
报表左侧的工具箱data source中就有了dataset,可以将每个字段拖到报表的表格中,也可以在报表的表格中智能感知添加列,此处的dataset的名字很重要,代码控制导出到excel时,名字不对就出错
3 选择报表单元格设置前景背景颜色,在弹出的配色窗口中选择公式,在其中填入“=Iif(RowNumber(Nothing) Mod 2, "PaleGreen", "White") "设置行颜色交替变化
4 代码控制输出excel
public void Export(IEnumerable<Tuple<string, object, DateTime, int>> tagValues, string path)
{
//Guard.ArgumentNotNull(values, "values")
// .ArgumentStringNull(Filename, "Filename")
// .ArgumentNotNull(Culture, "Culture");
IList<AI.DataRecord.Record> recordList = new List<AI.DataRecord.Record>();
foreach (var tupl in tagValues)
{
recordList.Add( AI.DataRecord.Record.FromTuple(tupl));
}
var values = recordList.GetEnumerator();
string temp = GetTempFile();
FileInfo fileInfo = new FileInfo(temp);
try
{
List<ReportParameter> parameters = new List<ReportParameter>();
parameters.Add(new ReportParameter("Name", "Name"));
parameters.Add(new ReportParameter("Date", "Date"));
LocalReport report = new LocalReport();
report.ReportEmbeddedResource = "ControlEase.Inspec.HistoryDataRecord.Presentation.TagsReport.rdlc";
report.SetParameters(parameters);
Warning[] warnings;
string[] streamids;
string mimeType;
string encoding;
string extension;
report.DataSources.Add(new ReportDataSource("TagsReport", values));
//bool ok = report.IsReadyForRendering;//true 才能Render
byte[] bytes = report.Render("EXCEL", null, out mimeType, out encoding, out extension,
out streamids, out warnings);
using (FileStream stream = new FileStream(temp, FileMode.CreateNew))
{
try
{
stream.Write(bytes, 0, bytes.Length);
stream.Close();
}
catch (Exception e)
{
e.ToString();
}
}
File.Copy(temp, path, true);
}
catch (Exception e)
{
//throw e;
}
finally
{
//if ( excel != null )
// excel.Dispose ( );
}
}
/// <summary>
/// Get temp excel file name
/// </summary>
/// <returns></returns>
static string GetTempFile()
{
string file = Path.GetRandomFileName();
file = Path.ChangeExtension(file, ".csv");
return Path.Combine(Path.GetTempPath(), file);
}
冻结表头
在报表设计界面--table边框上鼠标右键--Tablix property---弹出新窗口设置----rows header :keep rows header visible while scrolling