引用dll
DevExpress.Printing.v17.1.Core.dll
DevExpress.XtraReports.v17.1.dll
public void Print(PaperKind PagerKind, ReportModel rm, string fileName)
{
XtraReport r = new XtraReport();
string ls_temp = DirFileHelper.GetAbsolutePath(rm.templateName) ;//模板路径
if (!File.Exists(ls_temp))
{
//MessageBox.Show("未找到模板!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
r.LoadLayout(ls_temp);
r.DataAdapter = null;
r.DataMember = null;
r.PrintingSystem.ShowMarginsWarning = false;
r.PrintingSystem.ShowPrintStatusDialog = false;
r.ExportOptions.Xls.TextExportMode = DevExpress.XtraPrinting.TextExportMode.Text;
r.ExportOptions.Xlsx.TextExportMode = DevExpress.XtraPrinting.TextExportMode.Text;
r.DataSource = rm.List_Data;//明细数据
r.PrintingSystem.PageSettings.PaperKind = PagerKind;
r.PaperKind = PagerKind;//纸张大小
#region 绑定参数
if (rm.BaseParamter_DT != null && rm.BaseParamter_DT.Rows.Count > 0)//主表数据
{
for (int i = 0; i < rm.BaseParamter_DT.Columns.Count; i++)
{
if (r.Parameters[rm.BaseParamter_DT.Columns[i].ColumnName] == null)
{
continue;
}
try
{
r.Parameters[rm.BaseParamter_DT.Columns[i].ColumnName].Value
= rm.BaseParamter_DT.Rows[0][i] == null ? "" : rm.BaseParamter_DT.Rows[0][i];
}
catch (Exception ex)
{
// MessageBox.Show(ex.Message);
}
}
}
foreach (KeyValuePair<string, object> item_Ext in rm.BaseParamter_Ext)
{
if (r.Parameters[item_Ext.Key] == null)
continue;
r.Parameters[item_Ext.Key].Value = item_Ext.Value;
}
r.CreateDocument();
#endregion
#region 根据控件名绑定数据
if (!rm.BandDataByControlName.IsNullOrEmpty())
{
foreach (var item_control in rm.BandDataByControlName)
{
var ctl = r.FindControl(item_control.Key, true);
if (ctl != null)
{
if (ctl is DevExpress.XtraReports.UI.XRCheckBox)
{
((ctl as DevExpress.XtraReports.UI.XRCheckBox).Checked) = item_control.Value.IsTrue();
}
else
{
ctl.Text = item_control.Value.Cast<string>();
}
}
}
}
#endregion
#region 绑定复选框
if (rm.BandDataForCheckBox != null && rm.BandDataForCheckBox.Length > 0)
{
var val = rm.BandDataForCheckBox.Split('|');
string[] val_key = null;
foreach (var item_control in val)
{
val_key = item_control.Split(':');
dynamic ctl = r.FindControl(val_key[0], true);
if (ctl != null)
{
if (ctl is DevExpress.XtraReports.UI.XRCheckBox)
{
var param = new DevExpress.XtraReports.Parameters.Parameter();
param.Type = typeof(bool);
param.Value = val_key[1].IsTrue();
param.Name = val_key[0];
param.Visible = false;
r.Parameters.Add(param);
var bind = new XRBinding(param, "Checked", null);
ctl.DataBindings.Add(bind);
}
}
}
}
#endregion
string virtualPath = $"{DateTime.Now.Year.ToString()}/{DateTime.Now.Month.ToString()}/{DateTime.Now.ToString("yyyyMMddHHmmss")}_{fileName}";
string fullFileName = $"{Config.GetValue("AnnexesFile")}/{virtualPath}";//保存路径
r.ExportToImage(fullFileName);//保存为图片
}