public partial class MainForm : Form
{
XRDesignFormEx designForm = null;
XtraReport report = null;
public MainForm()
{
InitializeComponent();
designForm = new XRDesignFormEx();
report = new XtraReport();
InitReport();
this.WindowState = FormWindowState.Maximized;
}
//加载报表
private void InitReport()
{
report.DataSource = null;
int index = AppDomain.CurrentDomain.BaseDirectory.IndexOf(@"\OutPut");
string FileSaveFilePath = AppDomain.CurrentDomain.BaseDirectory.Substring(0, index);
FileSaveFilePath = Path.Combine(FileSaveFilePath, "Report");
if (!Directory.Exists(FileSaveFilePath))
{
Directory.CreateDirectory(FileSaveFilePath);
}
int index = AppDomain.CurrentDomain.BaseDirectory.IndexOf(@"\OutPut");
string FileSaveFilePath = AppDomain.CurrentDomain.BaseDirectory.Substring(0, index);
FileSaveFilePath = Path.Combine(FileSaveFilePath, "Report");
if (!Directory.Exists(FileSaveFilePath))
{
Directory.CreateDirectory(FileSaveFilePath);
}
var defaultFile = Path.Combine(FileSaveFilePath, tag.ModelDict.ModelContent);
if (!File.Exists(defaultFile))
{
Report.DefaultReport Default = new DefaultReport();
Default.SaveLayout(defaultFile);
Default.Dispose();
}
report.LoadLayout(defaultFile);
if (!File.Exists(defaultFile))
{
Report.DefaultReport Default = new DefaultReport();
Default.SaveLayout(defaultFile);
Default.Dispose();
}
report.LoadLayout(defaultFile);
report.DataSource = CreateDataTable(); //此处是一个datatable自己创建
BindTableData(CreateDataTable());
report.ReportUnit = DevExpress.XtraReports.UI.ReportUnit.TenthsOfAMillimeter; //设置单位为mm
report.PaperKind = System.Drawing.Printing.PaperKind.Custom; //纸张类型自定义
//隐藏按钮
designForm.DesignPanel.SetCommandVisibility(new ReportCommand[]{
ReportCommand.NewReport,
ReportCommand.SaveFileAs,
ReportCommand.OpenFile
}, CommandVisibility.None);
//自动保存
designForm.ReportStateChanged += designForm_ReportStateChanged;
// 加载报表.
designForm.OpenReport(report);
designForm.TopLevel = false;
designForm.FormBorderStyle = FormBorderStyle.None;
designForm.Dock = DockStyle.Fill;
this.splitContainer1.Panel2.Controls.Add(designForm);
//保存模板到指定位置
designForm.FileName = Path.Combine(FileSaveFilePath, "DefaultReport.repx");
designForm.Show();
BindTableData(CreateDataTable());
report.ReportUnit = DevExpress.XtraReports.UI.ReportUnit.TenthsOfAMillimeter; //设置单位为mm
report.PaperKind = System.Drawing.Printing.PaperKind.Custom; //纸张类型自定义
//隐藏按钮
designForm.DesignPanel.SetCommandVisibility(new ReportCommand[]{
ReportCommand.NewReport,
ReportCommand.SaveFileAs,
ReportCommand.OpenFile
}, CommandVisibility.None);
//自动保存
designForm.ReportStateChanged += designForm_ReportStateChanged;
// 加载报表.
designForm.OpenReport(report);
designForm.TopLevel = false;
designForm.FormBorderStyle = FormBorderStyle.None;
designForm.Dock = DockStyle.Fill;
this.splitContainer1.Panel2.Controls.Add(designForm);
//保存模板到指定位置
designForm.FileName = Path.Combine(FileSaveFilePath, "DefaultReport.repx");
designForm.Show();
}
//自动保存
void designForm_ReportStateChanged(object sender, ReportStateEventArgs e)
{
//只要报表发生改变就立即将状态设置为保存
//避免系统默认保存对话框的出现
if (e.ReportState == ReportState.Changed)
{
((XRDesignFormEx)sender).DesignPanel.ReportState = ReportState.Saved;
}
}
{
//只要报表发生改变就立即将状态设置为保存
//避免系统默认保存对话框的出现
if (e.ReportState == ReportState.Changed)
{
((XRDesignFormEx)sender).DesignPanel.ReportState = ReportState.Saved;
}
}
//中有tablecell绑定数据
private void BindTableData(DataSet ds)
{
//界面上必须统一弄两个tableCell
XRTableCell XRTableCell1 = report.FindControl("tableCell1", true) as XRTableCell;
XRTableCell XRTableCell2 = report.FindControl("tableCell2", true) as XRTableCell;
if (XRTableCell1 != null && XRTableCell2!=null)
{
XRTableCell1.Multiline = true;
XRTableCell2.Multiline = true;
XRTableCell1.TextAlignment = DevExpress.XtraPrinting.TextAlignment.TopLeft;
XRTableCell2.TextAlignment = DevExpress.XtraPrinting.TextAlignment.TopLeft;
for (int i = 1; i < ds.Tables["Table"].Rows.Count; i++)
{
if (i > 11)
{
XRTableCell2.Text += "" + ds.Tables["Table"].Rows[i][0] + "\r\n";
}
else
{
XRTableCell1.Text += "" + ds.Tables["Table"].Rows[i][0] + "\r\n";
}
}
}
}
{
//界面上必须统一弄两个tableCell
XRTableCell XRTableCell1 = report.FindControl("tableCell1", true) as XRTableCell;
XRTableCell XRTableCell2 = report.FindControl("tableCell2", true) as XRTableCell;
if (XRTableCell1 != null && XRTableCell2!=null)
{
XRTableCell1.Multiline = true;
XRTableCell2.Multiline = true;
XRTableCell1.TextAlignment = DevExpress.XtraPrinting.TextAlignment.TopLeft;
XRTableCell2.TextAlignment = DevExpress.XtraPrinting.TextAlignment.TopLeft;
for (int i = 1; i < ds.Tables["Table"].Rows.Count; i++)
{
if (i > 11)
{
XRTableCell2.Text += "" + ds.Tables["Table"].Rows[i][0] + "\r\n";
}
else
{
XRTableCell1.Text += "" + ds.Tables["Table"].Rows[i][0] + "\r\n";
}
}
}
}