回复 22楼huyu1681的帖子
测试代码如下:
- public tx_linkage()
- {
- InitializeComponent();
- }
- TXTextControl.ApplicationFieldCollection afc;
- private void Form1_Load(object sender, EventArgs e)
- {
- TXTextControl.LoadSettings ls = new TXTextControl.LoadSettings();
- ls.ApplicationFieldFormat = TXTextControl.ApplicationFieldFormat.MSWord;
- ls.ApplicationFieldTypeNames = new string[] { "MERGEFIELD" };
- this.textControl1.Load(AppDomain.CurrentDomain.BaseDirectory + "\\resource\\病历模板的格式-入院录.doc", TXTextControl.StreamType.MSWord, ls);
- afc = this.textControl1.ApplicationFields;
- GetMergeField(this.textControl1.ApplicationFields.GetItem(0));
- BindData();
- }
- string text;
- int start = 0;
- int length= 0;
- private void GetMergeField(ApplicationField applicationField)
- {
- start = applicationField.Start;
- length = applicationField.Length;
- text = applicationField.Text;
- }
- private void AddMergeField()
- {
- MergeField mergeField = new MergeField();
- mergeField.Text = text;
- mergeField.ApplicationField.ShowActivated = true;
- mergeField.ApplicationField.DoubledInputPosition = true;
- textControl1.Focus();
- textControl1.Selection.Start = start-1;
- textControl1.ApplicationFields.Add(mergeField.ApplicationField);
- }
- private void BindData()
- {
- //连接数据库
- string dbconStr = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=resource\\txdb.mdb";
- OleDbConnection dbcon = new OleDbConnection(dbconStr);
- if (dbcon.State == ConnectionState.Closed)
- {
- dbcon.Open();
- }
- DataTable dt = new DataTable();
- OleDbDataAdapter dbAdapter = new OleDbDataAdapter("select * from persons", dbcon);
- dbAdapter.Fill(dt);
- mailMerge1.TextComponent = this.textControl1;
- mailMerge1.SearchPath = "";
- mailMerge1.TemplateFile = AppDomain.CurrentDomain.BaseDirectory + "\\resource\\病历模板的格式-入院录.doc";
- mailMerge1.Merge(dt, true);
-
- AddMergeField();
- }
复制代码
我这里默认需要添加 mergefield 为,需要再做些操作,比如先获取所有字段信息进行对比等。 |