using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using DevExpress.XtraReports.UserDesigner; using DevExpress.XtraReports.UI; using DevExpress.XtraReports.UserDesigner.Native; using System.IO; using DevExpress.Data; using System.Drawing.Design; using DevExpress.XtraEditors; using DevExpress.XtraPrinting; using System.Data.Linq; using System.Linq; using System.Text.RegularExpressions; using ICSSoft.Base.Config.AppConfig; using ICSSoft.Base.Lable.Entity; using ICSSoft.Base.Config.DBHelper; namespace ICSSoft.Base.Lable.PrintTool { public partial class FormPrintDesigner : XRDesignFormExBase { private DataSet _DtSource { get; set; } private string _PrintFortmatText { get; set; } private string _LinkCols = ""; private string _PrintId = ""; private string _UserID = ""; public FormPrintDesigner(string tempId) { InitializeComponent(); _PrintId = tempId; _UserID = AppConfig.UserId; Sys_Lables obj = GetPrintTemp(); Sys_LableDataSource dsource=GetLableDataSource(obj.LableSourceID); string tempSql = dsource.SqlTxt; if (tempSql.Contains("[AppConfig.WorkPointCode]")) { tempSql = tempSql.Replace("[AppConfig.WorkPointCode]", AppConfig.WorkPointCode); } if (tempSql.Contains("[AppConfig.UserId]")) { tempSql = tempSql.Replace("[AppConfig.UserId]", AppConfig.UserId); } if (tempSql.Contains("[AppConfig.UserCode]")) { tempSql = tempSql.Replace("[AppConfig.UserCode]", AppConfig.UserCode); } if (tempSql.Contains("[AppConfig.UserName]")) { tempSql = tempSql.Replace("[AppConfig.UserName]", AppConfig.UserName); } if (tempSql.Contains("[AppConfig.WorkPointName]")) { tempSql = tempSql.Replace("[AppConfig.WorkPointName]", AppConfig.WorkPointName); } Dictionary dbDic = AppConfig.GetDataBaseNames(); foreach (string key in dbDic.Keys) { if (tempSql.Contains(key)) { tempSql = tempSql.Replace(key, dbDic[key]); } } string[] indexs = dsource.Paras.Split(','); List indexList = new List(); foreach (string index in indexs) { if (!string.IsNullOrEmpty(index)) { indexList.Add(index); } } if (indexList.Count > 0) { foreach (string index in indexList) { if (tempSql.Contains("'" + index + "'")) { tempSql = tempSql.Replace(index, ""); } else { tempSql = tempSql.Replace(index, "0"); } } } string conn = ""; if (dsource.SysDataFlag) { conn = AppConfig.FrameConnectString; } else { if (dsource.WorkPointDataFlag) { conn = AppConfig.AppConnectString; } else { conn= AppConfig.GetDataBaseConnectString(dsource.OtherDataBaseID); } } _DtSource = DBHelper.ExecuteDataset(conn,CommandType.Text,tempSql); _PrintFortmatText = obj==null?"":obj.LableXml; _LinkCols = dsource.LinkCols; this.xrDesignPanel.AddCommandHandler(new SaveCommandHandler(this.xrDesignPanel,_PrintId,AppConfig.FrameConnectString,_UserID) ); } /// /// 获取模板信息 /// /// private Sys_Lables GetPrintTemp() { using (LableDataContext db = new LableDataContext(AppConfig.FrameConnectString)) { Sys_Lables obj = db.Sys_Lables.SingleOrDefault(a => a.ID == _PrintId); return obj; } } private Sys_LableDataSource GetLableDataSource(string ID) { using (LableDataContext db = new LableDataContext(AppConfig.FrameConnectString)) { Sys_LableDataSource obj = db.Sys_LableDataSource.SingleOrDefault(a => a.ID == ID); return obj; } } /// /// Load the report into the designer /// public void BindReport() { XtraReport ltdReport = new XtraReport(); string msg = ""; if (_DtSource.Tables.Count == 1) { ltdReport.DataSource = _DtSource.Tables[0]; ltdReport.DataMember = _DtSource.Tables[0].TableName; } else if (_DtSource.Tables.Count > 1) { if (_LinkCols.Trim() == "") { msg += "没有关联字段!\n"; } else { for (int i = 0; i < _DtSource.Tables.Count; i++) { if (!_DtSource.Tables[i].Columns.Contains(_LinkCols)) { msg += "表:" + _DtSource.Tables[i] .TableName+ "没有关联字段!\n"; } } } DataColumn parentCol = _DtSource.Tables[0].Columns[_LinkCols]; if (msg == "") { for (int i = 0; i < _DtSource.Tables.Count; i++) { if (i == 0) continue; DataColumn sonCol = _DtSource.Tables[i].Columns[_LinkCols]; DataRelation relatioon = new DataRelation("R"+i.ToString(), parentCol, sonCol); if (!_DtSource.Relations.Contains("R"+i.ToString())) { _DtSource.Relations.Add(relatioon); } } ltdReport.DataMember = _DtSource.Tables[0].TableName; ltdReport.DataSource = _DtSource; } } else { msg += "列印数据源错误!\n"; } if (msg != "") { MessageBox.Show( msg); return; } if (!string.IsNullOrEmpty(_PrintFortmatText)) { byte[] bs2 = System.Text.Encoding.UTF8.GetBytes(_PrintFortmatText); MemoryStream steam = new MemoryStream(bs2); ltdReport.LoadLayout(steam); ltdReport.CreateDocument(); } xrDesignPanel.OpenReport( ltdReport ); commandBarItem32.Enabled = false; } ///// ///// Hide data source properties ///// ///// ///// //void LTDReport_FilterControlProperties( object sender, FilterControlPropertiesEventArgs e ) //{ // if ( e.Properties.Contains( "DataSource" )) // e.Properties.Remove( "DataSource" ); // if ( e.Properties.Contains( "DataMember" ) ) // e.Properties.Remove( "DataMember" ); // if ( e.Properties.Contains( "DataAdapter" ) ) // e.Properties.Remove( "DataAdapter" ); //} private void commandBarItem31_ItemClick( object sender, DevExpress.XtraBars.ItemClickEventArgs e ) { BindReport(); } private void xrDesignPanel_DesignerHostLoaded( object sender, DesignerLoadedEventArgs e ) { IToolboxService ts = ( IToolboxService )e.DesignerHost.GetService( typeof( IToolboxService ) ); ToolboxItem devExpressSubReport = null; // Add a custom control. ToolboxItemCollection toolboxItems = ts.GetToolboxItems(); foreach( ToolboxItem toolboxItem in toolboxItems ) { if ( toolboxItem.TypeName == "DevExpress.XtraReports.UI.XRSubreport" ) { devExpressSubReport = toolboxItem; break; } } if ( devExpressSubReport != null ) ts.RemoveToolboxItem( devExpressSubReport ); } private void FormRepDesign_Load(object sender, EventArgs e) { BindReport(); } private void FormRepDesign_FormClosed(object sender, FormClosedEventArgs e) { //MemoryStream layoutStream = new MemoryStream(); //xrDesignPanel.Report.SaveLayout(layoutStream); //xrDesignPanel.ReportState = ReportState.Saved; //// StreamReader readStream = new StreamReader(layoutStream, Encoding.UTF8); //string txt = System.Text.Encoding.UTF8.GetString(layoutStream.ToArray()); ////readStream.Close(); //_PrintFortmatText = txt; } } /// /// Command Handler to overwrite the save event /// public class SaveCommandHandler : DevExpress.XtraReports.UserDesigner.ICommandHandler { XRDesignPanel panel; private string _PrintId = ""; private string _ConnectString = ""; private string _UserCode = ""; public SaveCommandHandler( XRDesignPanel panel,string pId,string connectString ,string userCode) { this.panel = panel; _PrintId = pId; _ConnectString = connectString; _UserCode = userCode; } public void HandleCommand(ReportCommand command, object[] args) { bool useNexHadle = true ; if (!CanHandleCommand(command, ref useNexHadle)) return; // Save a report. if ( panel.ReportState == ReportState.Changed && command == ReportCommand.Closing ) { Save(); } else if (command != ReportCommand.Closing) { if ( panel.ReportState == ReportState.Changed ) Save(); } // handled = true; } public bool CanHandleCommand(ReportCommand command, ref bool useNextHandler) { // This handler is used for SaveFile, SaveFileAs and Closing commands. return command == ReportCommand.SaveFile || command == ReportCommand.SaveFileAs || command == ReportCommand.Closing; } void Save() { if (MessageBox.Show( "是否保存修改?","提示",MessageBoxButtons.YesNo,MessageBoxIcon.Information) == DialogResult.No) return; LableDataContext db = new LableDataContext(_ConnectString); db.Connection.Open(); db.Transaction = db.Connection.BeginTransaction(); try { var temp = db.Sys_Lables.SingleOrDefault(a => a.ID == _PrintId); if (temp == null) { MessageBox.Show("模板不存在数据库中,无法保存更新!"); } else { string txt = ""; using (MemoryStream layoutStream = new MemoryStream()) { panel.Report.SaveLayout(layoutStream); txt = System.Text.Encoding.UTF8.GetString(layoutStream.ToArray()); } temp.LableXml = txt; temp.CreateDate =DateTime.Now; temp.CreateUserID =_UserCode; db.SubmitChanges(); db.Transaction.Commit(); panel.ReportState = ReportState.Saved; } } catch (Exception ex) { MessageBox.Show( "异常:" + ex.Message); db.Transaction.Rollback(); } } #region ICommandHandler Members #endregion } }