FastReport Net 自动把excel数据文件转为打印模版
给FastReport Net报表工具补充了一个功能。自动生成模版,然后再用Designer精细调整。很方便。
private void button5_Click(object sender, EventArgs e)
{
pReport = new Report(); //实例化一个Report报表
// register all data tables and relations
pReport.RegisterData(ds);
// enable the "Employees" table to use it in the report
pReport.GetDataSource("Table1").Enabled = true;
// add report page
ReportPage page = new ReportPage();
pReport.Pages.Add(page);
// always give names to objects you create. You can use CreateUniqueName method to do this;
// call it after the object is added to a report.
page.CreateUniqueName();
// create title band
page.ReportTitle = new ReportTitleBand();
// native FastReport unit is screen pixel, use conversion
page.ReportTitle.Height = Units.Centimeters 1.5f;
page.ReportTitle.CreateUniqueName();
// create title text
TextObject titleText = new TextObject();
titleText.Parent = page.ReportTitle;
titleText.CreateUniqueName();
titleText.Bounds = new RectangleF(Units.Centimeters * 5, 0, Units.Centimeters * 10, Units.Centimeters * 1);
titleText.Font = new Font("黑体", 14, FontStyle.Bold);
titleText.Text = System.IO.Path.GetFileNameWithoutExtension(curr_xls_file);//取数据文件的名字
titleText.HorzAlign = HorzAlign.Center;
int i = 0;
foreach (DataColumn mDc in ds.Tables[0].Columns)
{
TextObject atitleText = new TextObject();
atitleText.Parent = page.ReportTitle;
atitleText.CreateUniqueName();
atitleText.Bounds = new RectangleF(i * Units.Centimeters * 4, Units.Centimeters * 1, Units.Centimeters * 4, Units.Centimeters * 0.5f);
atitleText.Font = new Font("宋体", 11, FontStyle.Bold);
atitleText.Text = "" + mDc.ColumnName.ToString() + "";
// atitleText.HorzAlign = HorzAlign.Center;
i++;
}
// create data band
DataBand dataBand = new DataBand();
page.Bands.Add(dataBand);
dataBand.CreateUniqueName();
dataBand.DataSource = pReport.GetDataSource("Table1");
dataBand.Height = Units.Centimeters * 0.5f;
i =0;
foreach (DataColumn mDc in ds.Tables[0].Columns)
{
TextObject aText = new TextObject();
// aText.Name= mDc.ColumnName.ToString();
aText.Parent = dataBand;
aText.CreateUniqueName();
aText.Bounds = new RectangleF(i Units.Centimeters * 4, 0, Units.Centimeters * 4, Units.Centimeters * 0.5f);
aText.Text = "[Table1." + mDc.ColumnName.ToString() + "]";
i++;
}
// format value as date
// DateFormat format = new DateFormat();
//format.Format = "yyyy-MM-dd";
// empBirthDateText.Format = format;
// System.IO.Path.GetFileNameWithoutExtension(curr_xls_file);//取数据文件的名字
curr_reportFile = System.IO.Path.GetFileNameWithoutExtension(curr_xls_file)+ "_自建模版.frx";
// run report designer
pReport.Save(curr_reportFile);
toolStripStatusLabel2.Text = "已自动创建模版文件:" + curr_reportFile + "";
}
}